我的感觉是你没有刷新,我编了一个程序,通过调用setVisible(false);
和setVisible(true);使窗口重画,可以实现你的要求,但窗口会闪烁,不知道有什么更好的办法。import java.awt.*;
import java.awt.event.*;public class Sample implements ActionListener
{
Button addedButton;
Button newButton;
Frame frame = new Frame();
public static void main(String[] args)
{
Sample sample = new Sample();
sample.Create();
}
public void Create()
{
frame.setLayout(new FlowLayout());
frame.add(addedButton);
frame.setSize(300, 300);
frame.setVisible(true);
}
public Sample()
{
addedButton = new Button("Add Button");
newButton = new Button("New Button");
addedButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if(source==addedButton)
{
// Button newButton=new JButton("New Button");
frame.add(newButton);
frame.setVisible(false);
frame.setVisible(true);
}
}
}
和setVisible(true);使窗口重画,可以实现你的要求,但窗口会闪烁,不知道有什么更好的办法。import java.awt.*;
import java.awt.event.*;public class Sample implements ActionListener
{
Button addedButton;
Button newButton;
Frame frame = new Frame();
public static void main(String[] args)
{
Sample sample = new Sample();
sample.Create();
}
public void Create()
{
frame.setLayout(new FlowLayout());
frame.add(addedButton);
frame.setSize(300, 300);
frame.setVisible(true);
}
public Sample()
{
addedButton = new Button("Add Button");
newButton = new Button("New Button");
addedButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if(source==addedButton)
{
// Button newButton=new JButton("New Button");
frame.add(newButton);
frame.setVisible(false);
frame.setVisible(true);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货