所以我在Java中制作一个基于文本的RPG Applet。我正在使用SWING面板,上面有文本字段和按钮。我在Netbeans编程,我正在使用GUI编辑器。我已经制作了HTML页面,它可以在浏览器中打开。当我点击按钮切换面板时(基本上将第一个面板设置为不可见),新面板不会加载。我尝试使用repaint()和validate(),但它只是不工作.....任何帮助吗?
package applettest;
import javax.swing.UIManager;
public class NewApplet extends java.applet.Applet {
startScreen ss;
registerScreen rs;
charactercreationScreen ccs;
@Override
public void init() {
try {
//This sets the look and feel to NIMBUS.
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
startup();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public void startup() {
showCharacterCreationScreenSTART();
showRegisterScreenSTART();
showStartScreenSTART();
}
public void showStartScreen() {
setSize(410, 350);
ss = new applettest.startScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
}
public void showStartScreenSTART() {
setSize(410, 350);
rs.setVisible(false);
ss = new applettest.startScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ss, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
repaint();
validate();
}
public void showRegisterScreen() {
repaint();
validate();
ss.setVisible(false);
setSize(400, 350);
rs = new applettest.registerScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
repaint();
validate();
}
public void showRegisterScreenSTART() {
repaint();
validate();
ccs.setVisible(false);
setSize(400, 350);
rs = new applettest.registerScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(rs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
repaint();
validate();
}
public void showCharacterCreationScreen() {
rs.setVisible(false);
setSize(400, 350);
ccs = new applettest.charactercreationScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
repaint();
validate();
}
public void showCharacterCreationScreenSTART() {
setSize(400, 350);
ccs = new applettest.charactercreationScreen(this);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
addComponent(ccs, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
}
}
听起来你应该使用CardLayout
。
参见:
- 如何使用CardLayout
- Java UI,试图在点击 按钮时转到下一页
- 动态添加组件到JDialog
我尝试使用repaint()和validate(),但它只是不工作
仅供参考,使用Swing时应该是:
panel.revalidate();
panel.repaint(); // sometimes needed
,尽管该代码通常用于从面板中添加/删除单个组件。如果你要交换整个面板,CardLayout是最好的选择。
另外,你应该扩展JApplet,而不是Applet。