Java Swing布局-使用哪种布局

Java Swing布局-使用哪种布局,java,swing,layout,layout-manager,Java,Swing,Layout,Layout Manager,我正在努力学习Swing编程 我已经阅读了关于各种布局的Java文档。我也读了一些教程。但除了一个非常简单的对话框之外,我真的无法弄清楚什么样的布局可以用于任何事情。我想通过代码(而不是WindowBuilder Pro)来实现这一点,这样我就能掌握其中的诀窍 这是一个对话框,我想建立 除注释外,其他内容均不可编辑 这种对话框的最佳布局是什么?要执行这种布局,可以使用GridBagLayout或GroupLayout 以下是了解布局的有用链接: 如图所示,您需要有一些列(按行2列) 为此, G

我正在努力学习Swing编程

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

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

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


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

要执行这种布局,可以使用GridBagLayout或GroupLayout

以下是了解布局的有用链接:

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

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


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

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

以下是了解布局的有用链接:

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

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


因此,对于您的示例,您必须定义3个面板(CustInfo、Lastorders和Notes),由Group或GridBagLayout定义。对于手工编写GUI布局的Java开发人员,您需要简单和强大。

-对于手工编写GUI布局的Java开发人员,您需要简单和强大。

首先,
GroupLayout
second

带有
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);
        });
    }
}
package com.zetcode;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextArea;
导入javax.swing.SwingUtilities;
导入net.miginfocom.swing.MigLayout;
/**
*MigLayout管理器的一个实用示例。
*@作者简·博德纳尔
*网站:zetcode.com
*/
公共类CustomerDetailsMigLayoutEx扩展了JFrame{
公共客户详细信息SMIGLAYOUTEX(){
initUI();
}
私有void initUI(){
JLabel custId1=新的JLabel(“客户Id”);
JLabel custId2=新JLabel(“A52501235”);
JLabel Name 1=新的JLabel(“名称”);
JLabel name 2=新JLabel(“乔·比尔”);
JLabel address1=新的JLabel(“地址”);
JLabel地址2=新JLabel(“市、州、国家第一街112号”);
JLabel订单=新的JLabel(“最后3个订单”);
JLabel date1=新JLabel(“2015年12月11日”);
JLabel date2=新JLabel(“2015年12月17日”);
JLabel date3=新JLabel(“2015年12月19日”);
JTextArea area1=新的JTextArea(7,28);
区域1.setBorder(BorderFactory.create蚀刻边界());
JTextArea Area 2=新的JTextArea(7,28);
区域2.setBorder(BorderFactory.create蚀刻边界());
JTextArea Area 3=新的JTextArea(7,28);
区域3.setBorder(BorderFactory.create蚀刻边界());
JTextArea Area 4=新的JTextArea(7,28);
区域4.setBorder(BorderFactory.create蚀刻边界());
JButton btn1=新JButton(“提交”);
JButton btn2=新JButton(“取消”);
createLayout(custId1、custId2、name1、name2、address1、address2、,
订单,日期1,区域1,日期2,区域2,日期3,区域3,
区域4、btn1、btn2);
setTitle(“布局示例”);
setLocationRelativeTo(空);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
私有void createLayout(JComponent…arg){
setLayout(新MIGLOAYOUT(“插入对话框,对齐50%50%,间隙5lp 7lp”);
添加(arg[0],“拆分2,sgx”);
添加(arg[1],“gapx 15lp,包装”);
添加(arg[2],“拆分2,sgx”);
添加(arg[3],“gapx 15lp,包装”);
添加(arg[4],“拆分2,sgx”);
添加(arg[5],“gapx 15lp,包装”);
添加(arg[6],“gapy unrel,wrap”);
添加(arg[7],“gapy rel,split 2”);
添加(arg[8],“包装”);
添加(arg[9],“拆分2”);
添加(arg[10],“包装”);
添加(arg[11],“拆分2”);
添加(arg[12],“包装”);
添加(arg[13],“growx”);
添加(arg[14],“分割2,流动”);
添加(arg[15]);
包装();
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(()->{
CustomerDetailsMigLayoutEx=新CustomerDetailsMigLayoutEx();
例如,setVisible(真);
});
}
}
截图:


migloayout
第一,
GroupLayout
第二

带有
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);
        });
    }
}
package com.zetcode;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextArea;
导入javax.swing.SwingUtilities;
导入net.miginfocom.swing.MigLayout;
/**
*MigLayout管理器的一个实用示例。
*@作者简·博德纳尔
*网站:zetcode.com
*/
公共类CustomerDetailsMigLayoutEx扩展了JFrame{
公共客户详细信息SMIGLAYOUTEX(){
initUI();
}
私有void initUI(){
JLabel custId1=新的JLabel(“客户Id”);
JLabel custId2=新JLabel(“A52501235”);
JLabel Name 1=新的JLabel(“名称”);
JLabel name 2=新JLabel(“乔·比尔”);
JLabel address1=新的JLabel(“地址”);
JLabel地址2=新JLabel(“市、州、国家第一街112号”);
JLabel订单=新的JLabel(“最后3个订单”);
JLabel date1=新JLabel(“2015年12月11日