1.定义一个JPanel的子类,在该子类中放三个按钮,然后创建该子类的两个对象,生成如下界面。
import java.awt.*; import javax.swing.*; public class DaiQing1 extends JFrame { String[] str1={"button4","button5","button6"}; String[] str2={"button1","button2","button3"}; JButton[] button; JButton[] button2; JPanel panel1,panel2; public DaiQing1(){ super("Exercise11_1"); button=new JButton[3]; button2=new JButton[3]; panel1=new JPanel(); panel2=new JPanel(); getContentPane().add(panel1,"North"); getContentPane().add(panel2,"Center"); panel1.setLayout(new GridLayout(1,3)); panel2.setLayout(new GridLayout(1,3)); for(int i=0;i<3;i++){ button=new JButton(str1); panel1.add(button); } for(int i=0;i<3;i++){ button2=new JButton(str2); panel2.add(button2); } setSize(260,86); setVisible(true); setResizable(false); } } 测试类: import javax.swing.*; public class testDaiQing1 { public static voidmain(String[] args) { DaiQing1frame = new DaiQing1(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
|