actionPerformed替换容器的内容

时间:2013-10-04 19:22:42

标签: java replace awt containers actionevent

好的,我正在为我的学校项目开发一个JApplet。我想要它做的是每次点击JButton,“菜单按钮”,它删除容器的当前内容,然后将新的JApplet添加到容器。我有点工作,我得到的唯一错误是它没有重新绘制容器的内容,但如果我调整窗口(我正在使用appletviewer当前显示它)它将显示我想要的它显示。下面是我用于actionPerformed方法的代码示例...

  public void actionPerformed(ActionEvent event)
  {
     if(event.getSource() == word_guess)//JButton 
     {
        WordGuess w = new WordGuess(); //Applet wanted to be displayed

        c.remove(main);//removes current content of container
        c.remove(side);
        c.setLayout(new GridLayout(1,0)); //changes Layout 
        c.add(w);  

        w.init(); //calls the init method of WordGuess
        repaint(); //I tried to see if repainting would help, and it didn't
     }
 }

2 个答案:

答案 0 :(得分:1)

在操作结束时调用revalidate();方法。

答案 1 :(得分:0)

你应该尝试将repaint()放在if块之外,也许这会起作用吗?