[英]Java - Repaint Not Working
So I am making a Text-Based RPG Applet in Java. 所以我用Java制作了一个基于文本的RPG Applet。 I am using SWING panels, with text fields and buttons on it.
我正在使用SWING面板,上面有文本字段和按钮。 I am programming it in Netbeans, and I am using the GUI editor.
我在Netbeans中编程,我正在使用GUI编辑器。 I have crafted the HTML page and it opens in the browser fine.
我制作了HTML页面,它在浏览器中打开很好。 When I click a button to switch panels (basically set the first the non-visible), the new panel doesn't load.
当我单击按钮切换面板(基本上设置第一个不可见)时,新面板不会加载。 I tried using repaint() and validate() but it just doesn't work..... Any help?
我尝试使用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));
}
}
Sounds like you should be using CardLayout
. 听起来你应该使用
CardLayout
。
See also: 也可以看看:
I tried using repaint() and validate() but it just doesn't work
我尝试使用repaint()和validate()但它只是不起作用
FYI, when using Swing it should be: 仅供参考,使用Swing时应该是:
panel.revalidate();
panel.repaint(); // sometimes needed
although that code is generally used when adding/removing individual components from a panel. 虽然通常在从面板添加/删除单个组件时使用该代码。 If you are swapping entire panels then CardLayout is the way to go.
如果你要交换整个面板,那么CardLayout就是你要走的路。
Also, you should be extending JApplet, NOT Applet. 此外,您应该扩展JApplet,而不是Applet。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.