L
llforyou
Unregistered / Unconfirmed
GUEST, unregistred user!
俺在Frame中加了几个按钮,然后当点击button3时,自动加入其他按钮.
可是其他按钮是动态加进去的,不刷新Frame它就显示不出来,请问怎么处理啊?
import java.awt.*;
import java.awt.event.*;
public class ExGui {
/**
* Method main
*
*
* @param args
*
*/
private Frame f;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
// f.setVisible(false);
//此处如何写呀?
// f.setVisible(true);
System.out.println("Action occurred"
}
}
public void go(){
f=new Frame("GUI example"
f.setLayout(new FlowLayout(FlowLayout.LEFT));
b1=new Button("Button1"
b2=new Button("Button2"
b3=new Button("Button3"
b4=new Button("Button4"
b5=new Button("Button5"
b6=new Button("Button6"
b7=new Button("Button7"
b8=new Button("Button8"
b3.addActionListener(new ButtonHandler());
f.add(b1);
f.add(b2);
f.add(b3);
f.setSize(800,800);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO: Add your code here
ExGui that=new ExGui();
that.go();
}
}
可是其他按钮是动态加进去的,不刷新Frame它就显示不出来,请问怎么处理啊?
import java.awt.*;
import java.awt.event.*;
public class ExGui {
/**
* Method main
*
*
* @param args
*
*/
private Frame f;
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
// f.setVisible(false);
//此处如何写呀?
// f.setVisible(true);
System.out.println("Action occurred"
}
}
public void go(){
f=new Frame("GUI example"
f.setLayout(new FlowLayout(FlowLayout.LEFT));
b1=new Button("Button1"
b2=new Button("Button2"
b3=new Button("Button3"
b4=new Button("Button4"
b5=new Button("Button5"
b6=new Button("Button6"
b7=new Button("Button7"
b8=new Button("Button8"
b3.addActionListener(new ButtonHandler());
f.add(b1);
f.add(b2);
f.add(b3);
f.setSize(800,800);
f.setVisible(true);
}
public static void main(String[] args) {
// TODO: Add your code here
ExGui that=new ExGui();
that.go();
}
}