【问题标题】:actionPerformed replacing contents of containeractionPerformed 替换容器的内容
【发布时间】:2013-10-04 19:22:41
【问题描述】:

好的,我正在为我的学校项目开发 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
     }
 }

【问题讨论】:

标签: java replace awt containers actionevent


【解决方案1】:

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

【讨论】:

  • CardLayout 可能是这个 GUI 实际需要的。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多