Java - Repaint Not Working
So I am making a Text-Based RPG Applet in Java. I am using SWING panels, with text fields and buttons on it. I am programming it in Netbeans, and I am using the GUI editor. I have crafted the HTML page and it opens in the browser fine. 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?
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.c开发者_JS百科reateParallelGroup(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
.
See also:
- How to Use CardLayout
- Java UI, trying to go to next page upon clicking button
- Dynamically Add Components to a JDialog
I tried using repaint() and validate() but it just doesn't work
FYI, when using Swing it should be:
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.
Also, you should be extending JApplet, NOT Applet.
精彩评论