demogridlayout.java

来自「java绘图 java awt 经典绘图的例子,对于初学awt模块的人非常有帮助」· Java 代码 · 共 78 行

JAVA
78
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package swing;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JRadioButton;/** * * @author zhaolin */public class DemoGridLayout extends JFrame {    public DemoGridLayout() {        JPanel control = new JPanel();        final JPanel content = new JPanel();        content.setLayout(new GridLayout(3, 2));        final JRadioButton jrb1 = new JRadioButton("3*2");        jrb1.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent e) {                if (jrb1.isSelected()) {                    updateContent(content, 3, 2);                }            }        });        jrb1.setSelected(true);        final JRadioButton jrb2 = new JRadioButton("4*3");        jrb2.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent e) {                if (jrb2.isSelected()) {                    updateContent(content, 4, 3);                }            }        });        ButtonGroup bg = new ButtonGroup();        bg.add(jrb1);        bg.add(jrb2);        control.add(jrb1);        control.add(jrb2);        Container c = this.getContentPane();        c.add(control, "North");        c.add(content);        this.pack();        this.setVisible(true);    }    public static void main(String[] args) {        new DemoGridLayout();    }    public static void updateContent(Container c, int row, int col) {        c.setLayout(new GridLayout(0, col));        c.removeAll();        for (int i = 0; i < 5; i++) {            c.add(new JButton("" + i));        }        c.validate();        c.repaint();    }}

⌨️ 快捷键说明

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