R
roadkill
Hi all,
I have included code at the bottom of this message,
it compiles, runs, and exhibits the behavior I will
describe.
The concensus seems to be that the "nicest" way to
have a number of "screens" or "panels" that are
interchangeable (i.e. switch between them) is to
use CardLayout, which does look like a nifty way
to do this.
I have tried to implement this, and it "sorta"
works, but with some problems.
The "screens" get laid out and show correctly,
and the action (from a button click) triggers
the screen changes fine, but after a seemingly
random number of switches "back and forth", the
screen switch eventually results in a blank
panel.
Basically, I wired up two screens, each with
a button that switches to the other screen.
Sometimes, one screen comes up blank after
3, 5, 7, or 9 switches, and sometimes the
other comes up blank after 2, 4, 6, or 8
switches.
The random-ness makes me think it could be
something about how I am doing this resulting
in my objects not staying "wired in", and
getting cleaned up by the GC.
Anyway, if someone could take a look at the
code (maybe run it if so inclined) and give
me some hints as to what I may be doing
wrong, I would appreciate it greatly.
PS - you'll need to supply some images to
actually run it - the background is a 1024x768
and I am using JDK 1.4.2_06 on linux (Slackware 10)
//=-=-=-=-=-=-=-=-=-=-= Code Example =-=-=-=-=-=-=-=-=-=-=
import java.io.*;
import java.awt.Dimension;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
public class Example extends JFrame implements ActionListener {
public static void main( String args[]) {
new Example().show();
return;
}
private void exitForm( java.awt.event.WindowEvent evt) {
System.exit( 0);
}
public Example() {
pRoot = null;
pBGImg = new ImageIcon( "./img/bg_default.png");
initComponents();
return;
}
private void initComponents() {
setTitle( "Example");
addWindowListener( new java.awt.event.WindowAdapter() {
public void windowClosing( java.awt.event.WindowEvent
evt)
{ exitForm( evt); }
}
);
launchScreen( "main");
return;
}
public void actionPerformed( ActionEvent e) {
String szCmd = e.getActionCommand();
if ( szCmd == "usr_exit") {
dispose();
} else if ( szCmd == "usr_act_01") {
launchScreen( "save");
} else if ( szCmd == "usr_act_02") {
launchScreen( "main");
}
return;
}
private void launchScreen( String sName) {
JComponent pScreen = null;
CardLayout pLayout = null;
if ( pRoot == null) {
JLayeredPane pLayers = new JLayeredPane();
JLabel pBG = new JLabel( pBGImg);
pBG.setBounds( 0, 0, 1024, 768);
pLayers.add( pBG, JLayeredPane.DEFAULT_LAYER);
pRoot = new JPanel( new CardLayout());
pRoot.setOpaque( false);
pRoot.setBounds( 0, 0, 1024, 768);
pScreen = generateMainScreen();
pScreen.setBounds( 0, 0, 1024, 768);
pRoot.add( pScreen, "main");
pScreen = generateSaveScreen();
pScreen.setBounds( 0, 0, 1024, 768);
pRoot.add( pScreen, "save");
pLayers.add( pRoot, JLayeredPane.PALETTE_LAYER);
setLayeredPane( pLayers);
}
pLayout = (CardLayout)(pRoot.getLayout());
pLayout.show( pRoot, sName);
pack();
setSize( new Dimension( 1024, 778));
setResizable( false);
return;
}
private JComponent generateMainScreen() {
JPanel pRet = new JPanel();
pRet.setOpaque( false);
pRet.setLayout( new BorderLayout());
FlowLayout pLayout = new FlowLayout();
pLayout.setVgap( 40);
JPanel pBtns = new JPanel();
pBtns.setLayout( pLayout);
pBtns.setOpaque( false);
JButton pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_exit");
pBtn.setIcon( new ImageIcon( "./img/btn01.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn01_pressed.png"));
pBtns.add( pBtn);
pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_act_01");
pBtn.setIcon( new ImageIcon( "./img/btn02.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn02_pressed.png"));
pBtns.add( pBtn);
pRet.add( pBtns, BorderLayout.SOUTH);
return( pRet);
}
private JComponent generateSaveScreen() {
JPanel pRet = new JPanel();
pRet.setOpaque( false);
pRet.setLayout( new BorderLayout());
FlowLayout pLayout = new FlowLayout();
pLayout.setVgap( 40);
JPanel pBtns = new JPanel();
pBtns.setLayout( pLayout);
pBtns.setOpaque( false);
JButton pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_act_02");
pBtn.setIcon( new ImageIcon( "./img/btn03.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn03_pressed.png"));
pBtns.add( pBtn);
pRet.add( pBtns, BorderLayout.SOUTH);
return( pRet);
}
private JPanel pRoot;
private ImageIcon pBGImg;
}
I have included code at the bottom of this message,
it compiles, runs, and exhibits the behavior I will
describe.
The concensus seems to be that the "nicest" way to
have a number of "screens" or "panels" that are
interchangeable (i.e. switch between them) is to
use CardLayout, which does look like a nifty way
to do this.
I have tried to implement this, and it "sorta"
works, but with some problems.
The "screens" get laid out and show correctly,
and the action (from a button click) triggers
the screen changes fine, but after a seemingly
random number of switches "back and forth", the
screen switch eventually results in a blank
panel.
Basically, I wired up two screens, each with
a button that switches to the other screen.
Sometimes, one screen comes up blank after
3, 5, 7, or 9 switches, and sometimes the
other comes up blank after 2, 4, 6, or 8
switches.
The random-ness makes me think it could be
something about how I am doing this resulting
in my objects not staying "wired in", and
getting cleaned up by the GC.
Anyway, if someone could take a look at the
code (maybe run it if so inclined) and give
me some hints as to what I may be doing
wrong, I would appreciate it greatly.
PS - you'll need to supply some images to
actually run it - the background is a 1024x768
and I am using JDK 1.4.2_06 on linux (Slackware 10)
//=-=-=-=-=-=-=-=-=-=-= Code Example =-=-=-=-=-=-=-=-=-=-=
import java.io.*;
import java.awt.Dimension;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
public class Example extends JFrame implements ActionListener {
public static void main( String args[]) {
new Example().show();
return;
}
private void exitForm( java.awt.event.WindowEvent evt) {
System.exit( 0);
}
public Example() {
pRoot = null;
pBGImg = new ImageIcon( "./img/bg_default.png");
initComponents();
return;
}
private void initComponents() {
setTitle( "Example");
addWindowListener( new java.awt.event.WindowAdapter() {
public void windowClosing( java.awt.event.WindowEvent
evt)
{ exitForm( evt); }
}
);
launchScreen( "main");
return;
}
public void actionPerformed( ActionEvent e) {
String szCmd = e.getActionCommand();
if ( szCmd == "usr_exit") {
dispose();
} else if ( szCmd == "usr_act_01") {
launchScreen( "save");
} else if ( szCmd == "usr_act_02") {
launchScreen( "main");
}
return;
}
private void launchScreen( String sName) {
JComponent pScreen = null;
CardLayout pLayout = null;
if ( pRoot == null) {
JLayeredPane pLayers = new JLayeredPane();
JLabel pBG = new JLabel( pBGImg);
pBG.setBounds( 0, 0, 1024, 768);
pLayers.add( pBG, JLayeredPane.DEFAULT_LAYER);
pRoot = new JPanel( new CardLayout());
pRoot.setOpaque( false);
pRoot.setBounds( 0, 0, 1024, 768);
pScreen = generateMainScreen();
pScreen.setBounds( 0, 0, 1024, 768);
pRoot.add( pScreen, "main");
pScreen = generateSaveScreen();
pScreen.setBounds( 0, 0, 1024, 768);
pRoot.add( pScreen, "save");
pLayers.add( pRoot, JLayeredPane.PALETTE_LAYER);
setLayeredPane( pLayers);
}
pLayout = (CardLayout)(pRoot.getLayout());
pLayout.show( pRoot, sName);
pack();
setSize( new Dimension( 1024, 778));
setResizable( false);
return;
}
private JComponent generateMainScreen() {
JPanel pRet = new JPanel();
pRet.setOpaque( false);
pRet.setLayout( new BorderLayout());
FlowLayout pLayout = new FlowLayout();
pLayout.setVgap( 40);
JPanel pBtns = new JPanel();
pBtns.setLayout( pLayout);
pBtns.setOpaque( false);
JButton pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_exit");
pBtn.setIcon( new ImageIcon( "./img/btn01.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn01_pressed.png"));
pBtns.add( pBtn);
pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_act_01");
pBtn.setIcon( new ImageIcon( "./img/btn02.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn02_pressed.png"));
pBtns.add( pBtn);
pRet.add( pBtns, BorderLayout.SOUTH);
return( pRet);
}
private JComponent generateSaveScreen() {
JPanel pRet = new JPanel();
pRet.setOpaque( false);
pRet.setLayout( new BorderLayout());
FlowLayout pLayout = new FlowLayout();
pLayout.setVgap( 40);
JPanel pBtns = new JPanel();
pBtns.setLayout( pLayout);
pBtns.setOpaque( false);
JButton pBtn = new JButton();
pBtn.setContentAreaFilled( false);
pBtn.setBorderPainted( false);
pBtn.setFocusPainted( false);
pBtn.setEnabled( true);
pBtn.addActionListener( this);
pBtn.setActionCommand( "usr_act_02");
pBtn.setIcon( new ImageIcon( "./img/btn03.png"));
pBtn.setPressedIcon( new ImageIcon( "./img/btn03_pressed.png"));
pBtns.add( pBtn);
pRet.add( pBtns, BorderLayout.SOUTH);
return( pRet);
}
private JPanel pRoot;
private ImageIcon pBGImg;
}