所以我在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));
}
}
答案 0 :(得分:2)
听起来你应该使用CardLayout
。
另见:
答案 1 :(得分:2)
我尝试使用repaint()和validate()但它只是不起作用
仅供参考,使用Swing时应该是:
panel.revalidate();
panel.repaint(); // sometimes needed
虽然通常在从面板添加/删除单个组件时使用该代码。如果你要交换整个面板,那么CardLayout就是你要走的路。
此外,您应该扩展JApplet,而不是Applet。