📄 gridbaglayouttest.java
字号:
/**
* 网格组布局管理器测试
*/
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.BoxLayout;
public class GridBagLayoutTest
{
public static void main(String[] args)
{
GridBagLayoutFrame frame = new GridBagLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class GridBagLayoutFrame extends JFrame
{
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
public GridBagLayoutFrame()
{
setTitle("学生信息输入界面");
setSize(WIDTH, HEIGHT);
Container con = getContentPane();
con.add(new StudentJPanel(), BorderLayout.CENTER);
con.add(new ButtonPanel(), BorderLayout.SOUTH);
}
}
class StudentJPanel extends JPanel
{
public StudentJPanel()
{
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
//构造并添加组件
JLabel nameLabel = new JLabel("姓名: ");
JTextField nameTextField = new JTextField(10);
JLabel numberLabel = new JLabel("学号: ");
JTextField numberTextField = new JTextField(10);
JLabel sexLabel = new JLabel("性别: ");
JComboBox sexComboBox = new JComboBox(new String[]{"男", "女"});
JLabel addressLabel = new JLabel("籍贯: ");
JComboBox addressCombo = new JComboBox(new String[]
{"山东", "河南", "北京", "黑龙江", "河北"});
JLabel commentLabel = new JLabel("简 单 介 绍");
JTextArea sample = new JTextArea();
sample.setLineWrap(true);
// 添加到网格组布局中
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
constraints.weightx = 5;
constraints.weighty = 5;
add(nameLabel, constraints, 0, 0, 1, 1);
add(numberLabel, constraints, 0, 1, 1, 1);
add(sexLabel, constraints, 0, 2, 1, 1);
add(addressLabel, constraints, 0, 3, 1, 1);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 100;
add(nameTextField, constraints, 1, 0, 1, 1);
add(numberTextField, constraints, 1, 1, 1, 1);
add(sexComboBox, constraints, 1, 2, 1, 1);
add(addressCombo, constraints, 1, 3, 1, 1);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
add(commentLabel, constraints, 2, 0, 1, 1);
constraints.fill = GridBagConstraints.BOTH;
add(sample, constraints, 2, 1, 1, 3);
}
public void add(Component c, GridBagConstraints constraints,
int x, int y, int w, int h)
{
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
add(c, constraints);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JButton okButton = new JButton("确定");
JButton cancelButton = new JButton("取消");
Box hbox = Box.createHorizontalBox();
hbox.add(Box.createHorizontalStrut(40));
hbox.add(okButton);
//在两个按钮间添加一个水平弹簧
hbox.add(Box.createHorizontalGlue());
hbox.add(cancelButton);
hbox.add(Box.createHorizontalStrut(40));
add(hbox);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -