gridbag3.java
来自「learning java的源代码。书中每个实例都有相关的代码example。」· Java 代码 · 共 42 行
JAVA
42 行
//file: GridBag3.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class GridBag3 extends JPanel { GridBagConstraints constraints = new GridBagConstraints( ); public GridBag3( ) { setLayout(new GridBagLayout( )); constraints.weightx = 1.0; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; int x, y; // for clarity constraints.gridheight = 2; // span two rows addGB(new JButton("one"), x = 0, y = 0); constraints.gridheight = 1; // set it back addGB(new JButton("two"), x = 1, y = 0); addGB(new JButton("three"), x = 2, y = 0); constraints.gridwidth = 2; // span two columns addGB(new JButton("four"), x = 1, y = 1); constraints.gridwidth = 1; // set it back } 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("GridBag3"); f.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(200, 200); f.setLocation(200, 200); f.setContentPane(new GridBag3( )); f.setVisible(true); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?