?? gridbaglayoutdemo.java
字號:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo extends JFrame {
public GridBagLayoutDemo() { //構(gòu)造函數(shù)
Container contentPane = getContentPane(); //得到容器
contentPane.setLayout(new GridBagLayout()); //設(shè)置布局管理器
JLabel labelName=new JLabel("姓名"); //姓名標簽
JLabel labelSex=new JLabel("性別"); //性別標簽
JLabel labelAddress=new JLabel("住址"); //住址標簽
JTextField textFieldName = new JTextField(); //性名文本域
JTextField textFieldAddress = new JTextField(); //地址文本域
JComboBox comboBoxSex = new JComboBox(); //性別組合框
comboBoxSex.addItem("男"); //增加選擇項
comboBoxSex.addItem("女");
JButton buttonConfirm=new JButton("確定"); //確定按鈕
JButton buttonCancel=new JButton("退出"); //退出按鈕
//增加各個組件
contentPane.add(labelName, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
contentPane.add(textFieldName, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
contentPane.add(comboBoxSex, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
contentPane.add(labelSex, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 0), 0, 0));
contentPane.add(buttonConfirm, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 3, 0), 0, 0));
contentPane.add(buttonCancel, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 0, 3, 0), 0, 0));
contentPane.add(labelAddress, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
contentPane.add(textFieldAddress, new GridBagConstraints(1, 1, 3, 1, 0.0, 0.0
,GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
setTitle("GridBagLayout 演示"); //設(shè)置窗口標題
setSize(300,140); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可見
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時退出程序
}
public static void main(String args[]) {
new GridBagLayoutDemo();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -