Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a Highscores Screen will be loaded</p> </blockquote> <p>The demo. below of a nested <code>CardLayout</code> adds an <code>ActionListener</code> instead of a <code>MouseListener</code>. It reacts to both mouse <strong>and</strong> keyboard input.</p> <p>There are a multitude of other ways to include more than one GUI element in the same screen space. Off the top of my head, <code>JTabbedPane</code>, <code>JSplitPane</code>, <code>JDesktopPane</code>/<code>JInternalFrame</code>, popping the high scores in a <code>JDialog</code> or <code>JOptionPane</code>..</p> <h3>Screenshots</h3> <p><img src="https://i.stack.imgur.com/dJLvt.png" alt="Game view"> <img src="https://i.stack.imgur.com/i9ZvK.png" alt="High Scores view"></p> <h3>CardLayoutDemo.java</h3> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; class CardLayoutDemo { public static void main(String[] args) { Runnable r = new Runnable () { public void run() { final JRadioButton game = new JRadioButton("Game", true); JRadioButton highScores = new JRadioButton("High Scores"); ButtonGroup bg = new ButtonGroup(); bg.add( game ); bg.add( highScores ); JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5)); buttons.add( game ); buttons.add( highScores ); JPanel gui = new JPanel(new BorderLayout(5,5)); gui.add(buttons, BorderLayout.SOUTH); final CardLayout cl = new CardLayout(); final JPanel cards = new JPanel(cl); gui.add(cards); cards.add(new JLabel("Level 1"), "game"); cards.add(new JLabel("High Scores"), "scores"); ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent ae) { if (game.isSelected()) { cl.show(cards, "game"); } else { cl.show(cards, "scores"); } } }; game.addActionListener(al); highScores.addActionListener(al); JOptionPane.showMessageDialog(null, gui); } }; SwingUtilities.invokeLater(r); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload