您可能应该创建多个JPanel
对象并将它们放入CardLayout
中,因此一次只能看到一页。请参见以下示例:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CardLayoutExample extends JFrame
{
public CardLayoutExample ()
{
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
getContentPane ().setLayout (new CardLayout ());
getContentPane ().add (new Fruits (), "Fruits");
getContentPane ().add (new Seasons (), "Seasons");
getContentPane ().add (new Colors (), "Colors");
getContentPane ().add (new Towns (), "Towns");
}
public void switchTo (String page)
{
((CardLayout)getContentPane ().getLayout ()).show (getContentPane (), page);
}
public static void main (String [] args)
{
SwingUtilities.invokeLater (new Runnable()
{
@Override
public void run ()
{
CardLayoutExample example = new CardLayoutExample ();
example.pack ();
example.setVisible (true);
}
});
}
private class Colors extends JPanel
{
public Colors ()
{
Box box = Box.createHorizontalBox ();
box.add (new JButton (new AbstractAction("Red")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Fruits");
}
}));
box.add (Box.createHorizontalStrut (8));
box.add (new JButton (new AbstractAction("Green")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Seasons");
}
}));
box.add (Box.createHorizontalStrut (8));
box.add (new JButton (new AbstractAction("Blue")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Towns");
}
}));
setLayout (new BorderLayout ());
add (box, BorderLayout.CENTER);
}
}
private class Fruits extends JPanel
{
public Fruits ()
{
Box box = Box.createVerticalBox ();
box.add (new JButton (new AbstractAction("Apple")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Colors");
}
}));
box.add (Box.createVerticalStrut (8));
box.add (new JButton (new AbstractAction("Orange")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Seasons");
}
}));
box.add (Box.createVerticalStrut (8));
box.add (new JButton (new AbstractAction("Pear")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Towns");
}
}));
setLayout (new BorderLayout ());
add (box, BorderLayout.CENTER);
}
}
private class Seasons extends JPanel
{
public Seasons ()
{
Box box = Box.createHorizontalBox ();
box.add (new JButton (new AbstractAction("Winter")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Colors");
}
}));
box.add (Box.createHorizontalStrut (8));
box.add (new JButton (new AbstractAction("Spring")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Fruits");
}
}));
box.add (Box.createHorizontalStrut (8));
box.add (new JButton (new AbstractAction("Summer")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Towns");
}
}));
setLayout (new BorderLayout ());
add (box, BorderLayout.CENTER);
}
}
private class Towns extends JPanel
{
public Towns ()
{
Box box = Box.createVerticalBox ();
box.add (new JButton (new AbstractAction("Moscow")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Colors");
}
}));
box.add (Box.createVerticalStrut (8));
box.add (new JButton (new AbstractAction("London")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Fruits");
}
}));
box.add (Box.createVerticalStrut (8));
box.add (new JButton (new AbstractAction("Tokyo")
{
@Override
public void actionPerformed (ActionEvent e)
{
switchTo ("Seasons");
}
}));
setLayout (new BorderLayout ());
add (box, BorderLayout.CENTER);
}
}
}