gridbag1.java

来自「learning java的源代码。书中每个实例都有相关的代码example。」· Java 代码 · 共 36 行

JAVA
36
字号
//file: GridBag1.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class GridBag1 extends JPanel {  GridBagConstraints constraints = new GridBagConstraints(  );  public GridBag1(  ) {    setLayout(new GridBagLayout(  ));    int x, y;  // for clarity    addGB(new JButton("North"),  x = 1, y = 0);    addGB(new JButton("West"),   x = 0, y = 1);    addGB(new JButton("Center"), x = 1, y = 1);    addGB(new JButton("East"),   x = 2, y = 1);    addGB(new JButton("South"),  x = 1, y = 2);  }  void addGB(Component component, int x, int y) {    constraints.gridx = x;    constraints.gridy = y;    add(component, constraints);  }  public static void main(String[] args) {    JFrame f = new JFrame("GridBag1");    f.addWindowListener(new WindowAdapter(  ) {      public void windowClosing(WindowEvent e) { System.exit(0); }    });    f.setSize(225, 150);    f.setLocation(200, 200);    f.setContentPane(new GridBag1(  ));    f.setVisible(true);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?