有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing布局要使用哪个布局

我正在努力学习Swing编程

我已经阅读了关于各种布局的Java文档。我也读了一些教程。但除了一个非常简单的对话框之外,我真的无法弄清楚什么样的布局可以用于任何事情。我想通过代码(而不是WindowBuilder Pro)来实现这一点,这样我就能掌握其中的诀窍

这是一个对话框,我想建立

enter image description here

除注释外,其他内容均不可编辑

这种对话框的最佳布局是什么


共 (3) 个答案

  1. # 1 楼答案

    第一,第二

    含有MigLayout的样品溶液:

    package com.zetcode;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import net.miginfocom.swing.MigLayout;
    
    /**
     * A practical example of MigLayout manager.
     * @author Jan Bodnar
     * Website: zetcode.com
     */
    public class CustomerDetailsMigLayoutEx extends JFrame {
    
        public CustomerDetailsMigLayoutEx() {
    
            initUI();
        }
    
        private void initUI() {
    
            JLabel custId1 = new JLabel("Cust Id");
            JLabel custId2 = new JLabel("A52501235");
    
            JLabel name1 = new JLabel("Name");
            JLabel name2 = new JLabel("Joe Beer");
    
            JLabel address1 = new JLabel("Address");
            JLabel address2 = new JLabel("112, 1st Street, City, State, Country");
    
            JLabel orders = new JLabel("<html><u style='font-size:13px'>Last 3 Orders</u></html>");
    
            JLabel date1 = new JLabel("11 Dec 2015");
            JLabel date2 = new JLabel("17 Dec 2015");
            JLabel date3 = new JLabel("19 Dec 2015");
    
            JTextArea area1 = new JTextArea(7, 28);
            area1.setBorder(BorderFactory.createEtchedBorder());
    
            JTextArea area2 = new JTextArea(7, 28);
            area2.setBorder(BorderFactory.createEtchedBorder());
    
            JTextArea area3 = new JTextArea(7, 28);
            area3.setBorder(BorderFactory.createEtchedBorder());
    
            JTextArea area4 = new JTextArea(7, 28);
            area4.setBorder(BorderFactory.createEtchedBorder());        
    
            JButton btn1 = new JButton("Submit");
            JButton btn2 = new JButton("Cancel");
    
            createLayout(custId1, custId2, name1, name2, address1, address2, 
                    orders, date1, area1, date2, area2, date3, area3, 
                    area4, btn1, btn2);
    
            setTitle("MigLayout example");
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        private void createLayout(JComponent... arg) {
    
            setLayout(new MigLayout("insets dialog, align 50% 50%, gap 5lp 7lp"));
    
            add(arg[0], "split 2, sgx");
            add(arg[1], "gapx 15lp, wrap");
            add(arg[2], "split 2, sgx");
            add(arg[3], "gapx 15lp, wrap");
            add(arg[4], "split 2, sgx");
            add(arg[5], "gapx 15lp, wrap");        
            add(arg[6], "gapy unrel, wrap");
            add(arg[7], "gapy rel, split 2");
            add(arg[8], "wrap");
            add(arg[9], "split 2");
            add(arg[10], "wrap");
            add(arg[11], "split 2");
            add(arg[12], "wrap");
            add(arg[13], "growx");        
            add(arg[14], "split 2, flowy");    
            add(arg[15]);    
            pack();
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(() -> {
                CustomerDetailsMigLayoutEx ex = new CustomerDetailsMigLayoutEx();
                ex.setVisible(true);
            });
        }
    }
    

    截图:

    Screenshot of the code example

  2. # 2 楼答案

    为了执行您的布局类型,可以使用GridBagLayout或GroupLayout

    这里有一个了解布局的有用链接:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

    如图所示,您需要有一些列(按行2列)

    为此,
    GridBagLayout通过将组件放置在单元格网格内来对齐组件
    而GroupLayout则分别处理水平和垂直布局。布局是为每个标注独立定义的

    因此,对于您的示例,您必须定义3个面板(CustInfo、Lastorders和Notes),由一个组或GridBagLayout定义

  3. # 3 楼答案

    MigLayout-适用于Java开发人员手工编写GUI布局,需要简单和强大的功能