有没有更简单的方法来显示这个 Java UI?

发布于 2024-12-11 09:19:16 字数 552 浏览 0 评论 0原文

我想显示一行文本,其下方有一个按钮,两者水平和垂直居中。我目前有一个 BoxLayout,其中包含一个 JLabel、一个用于间距的刚性区域,然后是 JPanel 内的一个 JButton。对于我想要做的事情来说,这似乎相当复杂,而且当窗口大小改变时,文本和按钮都不再居中。有更好的方法吗?

----------------------
|                    |
|                    |
|                    |
|      ________      |
|      | Text |      |
|      --------      |
|      ________      |
|      |Button|      |
|      --------      |
|                    |
|                    |
|                    |
----------------------

I want to display a line of text with a button underneath it, both of which are centered horizontally and vertically. I currently have a BoxLayout containing a JLabel, a rigid area for spacing and then a JButton within a JPanel. This seems quite complicated for what I want to do and also when the window size is changed, both the text and the button are no longer centered. Is there a better way to do this?

----------------------
|                    |
|                    |
|                    |
|      ________      |
|      | Text |      |
|      --------      |
|      ________      |
|      |Button|      |
|      --------      |
|                    |
|                    |
|                    |
----------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-12-18 09:19:17

我会选择这样的内容:

JButton btn = new JButton("I'm a Button");
JLabel lbl = new JLabel("I'm a Label");

JPanel pan = new JPanel();
pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

pan.add(lbl);
pan.add(btn);

JPanel pan2 = new JPanel();
pan2.setLayout(new BoxLayout(pan2, BoxLayout.X_AXIS));

pan2.add(Box.createHorizontalGlue());
pan2.add(pan);
pan2.add(Box.createHorizontalGlue());

setLayout(new BorderLayout());
add(pan2, BorderLayout.CENTER);

它仍然有点复杂,您可能想要为标签添加另一个面板,以便它居中,根据我的经验,UI 的布局往往需要大量工作。

I'd go with something like:

JButton btn = new JButton("I'm a Button");
JLabel lbl = new JLabel("I'm a Label");

JPanel pan = new JPanel();
pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

pan.add(lbl);
pan.add(btn);

JPanel pan2 = new JPanel();
pan2.setLayout(new BoxLayout(pan2, BoxLayout.X_AXIS));

pan2.add(Box.createHorizontalGlue());
pan2.add(pan);
pan2.add(Box.createHorizontalGlue());

setLayout(new BorderLayout());
add(pan2, BorderLayout.CENTER);

It's still a bit complex, and you'd probably want to add another panel for the label so that it's centered, UIs in my experience tend to be a lot of work to layout.

从此见与不见 2024-12-18 09:19:17

居中组件

import java.awt.*;
import javax.swing.*;

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                // the GBL allows us to center a component
                JPanel p = new JPanel(new GridBagLayout());
                p.add(new JButton("Centered Button"));
                gui.add(p);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(gui);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

更新

当窗口最大化时,我需要两个组件保持垂直居中

Centered Components 2

import java.awt.*;
import javax.swing.*;

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                // the GBL allows us to center a component..
                JPanel center = new JPanel(new GridBagLayout());
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                gui.add(new JButton("Centered Button"));
                // ..in this case the component is a panel
                center.add(gui);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(center);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

Centered Components

import java.awt.*;
import javax.swing.*;

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                // the GBL allows us to center a component
                JPanel p = new JPanel(new GridBagLayout());
                p.add(new JButton("Centered Button"));
                gui.add(p);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(gui);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

Update

I need both components to stay vertically centered when the window is maximised

Centered Components 2

import java.awt.*;
import javax.swing.*;

class CenteredComponents {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new GridLayout(0,1));
                // the GBL allows us to center a component..
                JPanel center = new JPanel(new GridBagLayout());
                JLabel l = new JLabel("Centered Text", SwingConstants.CENTER);
                gui.add(l);
                gui.add(new JButton("Centered Button"));
                // ..in this case the component is a panel
                center.add(gui);

                JFrame f = new JFrame("Centered Components");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(center);
                f.pack();
                f.setSize(350,150);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}
樱花坊 2024-12-18 09:19:16

使用 2x1 GridLayout

Use a 2x1 GridLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文