窗口显示之后,add,remove组件必须在事件线程中进行,setvisible()也一样.

解决方案 »

  1.   

    能说的详细点吗
    比如我在一个类game中定义了 Label aaa 
    要按一个按钮bbb使aaa消失
    在actionPerformed(ActionEvent e)
    怎么写
     remove(aaa)没有用先谢谢楼上的
      

  2.   

    容器.remove(组件)
    然后 repaint
      

  3.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class test{
    public static void main(String [] args){
    TestFrame tt=new TestFrame();

    }
    }class TestFrame extends JFrame implements ActionListener{
    JLabel  aaa;
    JButton b;
    public TestFrame(){
    super(" test ");
    this.getContentPane().setLayout(new FlowLayout());
    this.setSize(100,100);

    aaa=new JLabel("aaaa");
    this.getContentPane().add(aaa);
    b=new JButton("remove");
    this.getContentPane().add(b);
    b.addActionListener(this);
    this.show();
    }
    public void actionPerformed(ActionEvent e){
    //remove the component
    this.getContentPane().remove(aaa);
    //repaint the ContentPane
    this.getContentPane().repaint();


    }
    }
      

  4.   

    SwingUtilities.invokeLater(new Runnable() {
          public void run() { 
                      b=new JButton("remove");
    this.getContentPane().add(b);
    b.addActionListener(this);
    this.show();      }
    }
    可参考
    <<Java开发中的线程安全选择与Swing>>
    http://www.yesky.com/SoftChannel/72342371961929728/20030714/1714114_2.shtml