你好,我运行了下你的代码,发现这里面一个极小的错误,请看我修改后的代码,错误的地方标记了出来
import javax.swing.*;
import java.awt.*;
class Anniu extends JFrame {
public void annniu() {
String button_name[] = { "1", "2", "3", "5", "6", "7", "8", "9" };
// JFrame frame = new JFrame();这句代码不要了,因为你的Anniu类继承了JFrame
JButton[] bt = new JButton[10];
setBounds(100, 100, 320, 320);
setTitle("数字");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 去掉了frame.
Container co = getContentPane();// 去掉了frame.
GridLayout gr = new GridLayout(3, 3);
co.setLayout(gr);
for (int i = 0; i < button_name.length; i++) {
bt[i] = new JButton(button_name[i]);
co.add(bt[i]);
}
this.setVisible(true);
}
}
public class Test1 {
public static void main(String[] args) {
Anniu an = new Anniu();
an.annniu();
}
}
我还是重新写个给你:
import java.awt.*;
import java.awt.event.*;
public class FrameTest
{
public static void main(String []args)
{
Frame f=new Frame("布局管理");
f.setSize(600,400);
f.setLocation(100,100);
f.setBackground(Color.blue);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//f.setLayout(new BorderLayout(10,10));
//f.setLayout(new FlowLayout());
f.setLayout(new GridLayout(1,2));
Panel p1=new Panel(new BorderLayout());
final CardLayout cl=new CardLayout();
final Panel p2=new Panel(cl);
f.add(p1);
f.add(p2);
Button bt1=new Button("north");
Button bt2=new Button("south");
Button bt3=new Button("west");
Button bt4=new Button("east");
Button bt5=new Button("center");
p1.add(bt1,"North");
p1.add(bt2,"South");
p1.add(bt3,"West");
p1.add(bt4,"East");
p1.add(bt5,"Center");
Button btn1=new Button("背面");
Button btn2=new Button("正面");
ActionListener a=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.next(p2);
}
};
btn1.addActionListener(a);
btn2.addActionListener(a);
p2.add(btn1,"1");
p2.add(btn2,"2");
//f.show();
f.setVisible(true);
}
}