[英]Java Swing Layouts - Which layout to use
为了执行您的那种布局,您可以使用GridBagLayout或GroupLayout。
这里是了解布局的有用链接: https : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
如图所示,您需要有一些列(每行2个)。
为此,
GridBagLayout通过将组件放置在单元格网格中来对齐它们。
而GroupLayout分别与水平和垂直布局一起使用。 分别为每个尺寸定义布局。
因此,对于您的示例,您必须定义3个面板(CustInfo,Lastorders和Notes),由Group或GridBagLayout定义
MigLayout-用于Java开发人员,他们希望手工编写GUI布局,而这些布局需要简单性和功能。
MigLayout
第一, 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);
});
}
}
屏幕截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.