【问题标题】:What swing layouts structure to choose for an app?为应用程序选择什么样的摇摆布局结构?
【发布时间】:2013-01-30 11:40:25
【问题描述】:

这是我真正想在面板上显示的内容:

第一个逻辑块:

单选按钮 1       文本字段    图标按钮

单选按钮 2       文本字段    图标按钮

复选框

第二个逻辑块:

标签       微调器

        按钮

我的第一个决定是制作垂直框布局,并为每个逻辑块放置两个水平框布局。但问题在于这些块,选择什么布局来描述这种结构?我不喜欢 GridBagLayout - 它非常复杂且难以理解,尤其是当代码不是你的时。目前我看到可以使用流布局和网格布局。但是,例如 Grid Layout,将按钮拉伸到单元格的宽度,如果一个按钮只有它的图标,那么它看起来很奇怪。

希望你能给我一些建议。

【问题讨论】:

    标签: java swing layout layout-manager jcomponent


    【解决方案1】:

    对于第一种情况,您可以在JPanel 上使用简单的GridLayout3 Rows 每个都有一个单独的JPanelFlowLayout 有约束,FLowLayout.LEFT。看看这个代码示例:

    import java.awt.*;
    import javax.swing.*;
    
    public class ExampleLayout
    {
        private void displayGUI()
        {
            JFrame frame = new JFrame("Example Layout");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new GridLayout(0, 1, 5, 5));
    
            JPanel topPanel = new JPanel();
            topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
            JTextField tfield1 = new JTextField(10);
            JButton button1 = new JButton("Button 1");
    
            topPanel.add(rbut1);
            topPanel.add(tfield1);
            topPanel.add(button1);
    
            JPanel middlePanel = new JPanel();
            middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            JRadioButton rbut2 = new JRadioButton("RadioButton 2", false);
            JTextField tfield2 = new JTextField(10);
            JButton button2 = new JButton("Button 2");
    
            middlePanel.add(rbut2);
            middlePanel.add(tfield2);
            middlePanel.add(button2);
    
            JPanel bottomPanel = new JPanel();
            bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
            JCheckBox cbox = new JCheckBox("CheckBox 1", false);
            bottomPanel.add(cbox);
    
            contentPane.add(topPanel);
            contentPane.add(middlePanel);
            contentPane.add(bottomPanel);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new ExampleLayout().displayGUI();
                }
            });
        }
    }
    

    输出:

    对于第二种情况,只需将前两个组件添加到具有默认布局的JPanel。对于第三个组件,只需将组件添加到具有GridBagLayoutJPanel 上,没有任何限制。

    编辑#1:

    或者,您可以将这种方法用于您的第二块

    import java.awt.*;
    import javax.swing.*;
    
    public class ExampleLayout
    {
        private void displayGUI()
        {
            JFrame frame = new JFrame("Example Layout");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout(5, 5));
    
            JPanel basePanel = new JPanel();
            basePanel.setLayout(new GridLayout(0, 1, 5, 5));
    
            JPanel topPanel = new JPanel();
            //topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            JLabel label1 = new JLabel("Label 1", JLabel.CENTER);
            JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
    
            topPanel.add(label1);
            topPanel.add(rbut1);        
    
            JPanel middlePanel = new JPanel();
            middlePanel.setLayout(new GridBagLayout());
    
            JButton button1 = new JButton("Button 1");
    
            middlePanel.add(button1);
    
            basePanel.add(topPanel);
            basePanel.add(middlePanel);
    
            contentPane.add(basePanel, BorderLayout.PAGE_START);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new ExampleLayout().displayGUI();
                }
            });
        }
    }
    

    【讨论】:

    • 哇,太不可思议了。非常感谢,我想这是我需要的。但是说到 GridBagLayout,用居中对齐的 BoxLayout 代替它不是更好吗?关于第一种情况的按钮。如果我只使用没有文字的图标(例如,“打开文件夹”按钮),它的大小不会被拉伸吗?我的意思是 40x16 而不是 16x16(图标大小)。
    • @Dragon :实际上,指定GridBagLayout 没有约束,只需将组件放在容器的中心,我猜它可能适用于您的情况,而不是使用BoxLayout。从未尝试过BoxLayout 那么多,因为Nested Layout 方法或GridBagLayout 可以轻松完成所有任务:-) BoxLayout 根据组件的内容拉伸组件。所以有时它会导致对齐问题,恕我直言(这就是为什么我在很多情况下都克制自己不使用它)。
    • @GagandeepBali,在我看来,BoxLayout 足够灵活。嗯,也许我需要重新考虑一下。无论如何,非常感谢。
    • @Dragon :请看一下 mKorbel 的这个精彩的example,它为JButton 提供属性,这可能看起来不错。从你的角度来看,你可能是对的,因为我喜欢GridBagLayout,从你的角度来看,使用它有点不友好。其余的,你最欢迎并保持微笑:-)
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多