package gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 编写一个Java GUI应用程序,窗口标题为“GridLayout”, * 窗口布局如下图A所示,在图A窗口中单击任意一个Button, * 网格的划分方式会变化为图B;在图B窗口中单击任意一个Button, * 网格的划分方式会变化为图A。(25分) * @since 2010-9-20 * @start 12:34 * @end 12:55 * @version 0.1 * @author allchin * @serial 1 * */ public class Four_LayoutChanger { public static void main(String[] args){ LayoutChanger lc=new LayoutChanger(); lc.init(); lc.start(); } } class LayoutChanger extends JFrame implements ActionListener{ GridLayout gl=new GridLayout(2,3), gl2=new GridLayout(3,2); Button btn1=new Button("one"), btn2=new Button("two"), btn3=new Button("three"), btn4=new Button("four"), btn5=new Button("five"), btn6=new Button("six"); public void init(){ this.getContentPane().setLayout(gl); this.setBounds(111, 111, 800, 600); this.setVisible(true); this.validate(); this.getContentPane().add(btn1); this.getContentPane().add(btn2); this.getContentPane().add(btn3); this.getContentPane().add(btn4); this.getContentPane().add(btn5); this.getContentPane().add(btn6); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); } public void start(){ } @Override public void actionPerformed(ActionEvent e) { this.getContentPane().removeAll(); if(this.getContentPane().getLayout().equals(gl)){ this.getContentPane().setLayout(gl2); } else if(this.getContentPane().getLayout().equals(gl2)){ this.getContentPane().setLayout(gl); } this.getContentPane().add(btn1); this.getContentPane().add(btn2); this.getContentPane().add(btn3); this.getContentPane().add(btn4); this.getContentPane().add(btn5); this.getContentPane().add(btn6); this.getContentPane().validate(); this.validate(); } }