【问题标题】:Is there a simpler way to display this Java UI?有没有更简单的方法来显示这个 Java UI?
【发布时间】:2011-12-13 07:46:12
【问题描述】:

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

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

【问题讨论】:

    标签: java swing layout user-interface awt


    【解决方案1】:

    使用 2x1 GridLayout

    【讨论】:

      【解决方案2】:

      我会选择类似的东西:

      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 的布局往往需要大量工作。

      【讨论】:

        【解决方案3】:

        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);
                    }
                });
            }
        }
        

        更新

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

        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);
                    }
                });
            }
        }
        

        【讨论】:

        • 这很好用,除了我需要两个组件在窗口最大化时保持垂直居中。
        • 我认为我的代码实现了该规范。 DYM 他们应该保持在一起并垂直居中吗?这很容易。将两个组件首先放入 GL 面板,然后将 that 面板添加到 GBL。如果您在大小更改之前和之后提供 GUI 的 ASCII 艺术,它将极大地帮助解决布局问题。
        • 非常感谢。我知道下次提供 ASCII 图像。但是,现在组件拉伸了窗口的宽度。是否可以缩小它们以适应?
        • 是的,是的。提供 ASCII 艺术,我会看看需要做什么。
        • "的哪一部分在大小变化之前提供了 GUI 的 ASCII 艺术" 您是否有理解困难?那是两个(数一下,2)ASCII艺术。 1有什么好处? OTOH 1)我随机猜测(我通常不这样做)你的意思正是我在第一条评论中提出的。 2) 对于之前标记为已回答的问题,我将提供的帮助(见编辑)已接近尾声。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多