日期:2014-05-20 浏览次数:20831 次
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class GridBagLayoutDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutDemo().new MyFrame();
}
});
}
class MyFrame extends JFrame{
private static final long serialVersionUID = 1L;
protected JLabel label = new JLabel("最高记录: ");
protected JLabel labe2 = new JLabel("当前分数: ");
protected JButton Jchongxin = new JButton("重新开始");
protected JButton Jpaihang = new JButton("排行榜");
protected JButton Jhuiqi = new JButton("悔棋");
protected JButton Jtuichu = new JButton("退出游戏");
protected JLabel labe3 = new JLabel("下一组棋子");
protected JTextArea JTxiayizu = new JTextArea();
public MyFrame(){
setTitle("A Test Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 100, 200, 350);
GridBagLayout gly = new GridBagLayout();
addComponent(0, 0,label, gly);
addComponent(1, 0,labe2, gly);
addComponent(2, 0,Jchongxin, gly);
addComponent(3, 0,Jpaihang, gly);
addComponent(4, 0,Jhuiqi, gly);
addComponent(5, 0,Jtuichu, gly);
addComponent(6, 0, JTxiayizu, gly);
setLayout(gly);
setVisible(true);
}
private void addComponent(int low, int column, JComponent compoent, GridBagLayout gly){
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10, 10, 10, 10);
c.gridx = column;
c.gridy = low;
c.fill = GridBagConstraints.BOTH;
gly.setConstraints(compoent, c);
add(compoent);
}
}
}