【发布时间】: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
}
}
【问题讨论】:
-
1) “我正在为我的学校项目开发 JApplet” 为什么要编写小程序而不是桌面应用程序。?如果是由于规范。老师请发给Why CS teachers should stop teaching Java applets。 2) 对于一个空间中的多个组件,请使用
CardLayout,如short example 所示。
标签: java replace awt containers actionevent