📄 demogridlayout.java
字号:
/* * 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -