主题:请帮忙运行一下
请哪位运行一下以下一段代码,大意为一个Frame f里面添加了两个Panel p1(North)和p2(Center),Panel p1上有两个Button toflow和togrid分别点击后控制p2的布局为FlowLayout和GridLayout,同时添加Label l,TextField t和Button b在p2上,点击Button b时把t中的文本复制到l中显示,但是我的几个Button都不响应单击,希望帮忙解决一下。
import java.awt.*;
import java.awt.event.*;
class Copy extends WindowAdapter implements ActionListener,WindowListener
{Frame f;Panel p1,p2;Label l;TextField t;Button toflow,togrid,b;
public static void main(String args[])
{Copy c=new Copy();
c.execute();
}
public void execute()
{f=new Frame("按钮事件");
f.addWindowListener(this);
f.setLayout(new BorderLayout());
p1=new Panel();
p1.setLayout(new FlowLayout());
p2=new Panel();
toflow=new Button("顺序布局");
togrid=new Button("网格布局");
l=new Label();
t=new TextField();
b=new Button("复制");
p1.add(toflow);
p1.add(togrid);
f.add("North",p1);
f.add("Center",p2);
toflow.addActionListener(this);
togrid.addActionListener(this);
b.addActionListener(this);
f.setSize(400,300);f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==toflow)
{p2.setLayout(new FlowLayout());
p2.add(l);p2.add(t);p2.add(b);
}
else if(e.getSource()==togrid)
{p2.setLayout(new GridLayout(3,1));
p2.add(l);p2.add(t);p2.add(b);
}
else if(e.getSource()==b)
{String s=t.getText();
l.setText(s);
}
}
public void windowClosing(WindowEvent e)
{System.exit(0);
}
}
import java.awt.*;
import java.awt.event.*;
class Copy extends WindowAdapter implements ActionListener,WindowListener
{Frame f;Panel p1,p2;Label l;TextField t;Button toflow,togrid,b;
public static void main(String args[])
{Copy c=new Copy();
c.execute();
}
public void execute()
{f=new Frame("按钮事件");
f.addWindowListener(this);
f.setLayout(new BorderLayout());
p1=new Panel();
p1.setLayout(new FlowLayout());
p2=new Panel();
toflow=new Button("顺序布局");
togrid=new Button("网格布局");
l=new Label();
t=new TextField();
b=new Button("复制");
p1.add(toflow);
p1.add(togrid);
f.add("North",p1);
f.add("Center",p2);
toflow.addActionListener(this);
togrid.addActionListener(this);
b.addActionListener(this);
f.setSize(400,300);f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==toflow)
{p2.setLayout(new FlowLayout());
p2.add(l);p2.add(t);p2.add(b);
}
else if(e.getSource()==togrid)
{p2.setLayout(new GridLayout(3,1));
p2.add(l);p2.add(t);p2.add(b);
}
else if(e.getSource()==b)
{String s=t.getText();
l.setText(s);
}
}
public void windowClosing(WindowEvent e)
{System.exit(0);
}
}