Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing multiple JPanels in a single JFrame
    text
    copied!<p>I've basically been trying to make a menu for a game by using a JFrame and switching out 2 JPanels (one for the menu and one for the actual game). I'm trying to implement the most basic format I can think of but can't seem to get it to work. If anybody could explain what's wrong with the code I would appreciate it.</p> <p>Here's my JFrame, menu panel, and ActionListener</p> <pre><code>package buttonMenu; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Skeleton extends JFrame implements ActionListener{ JPanel menu; JButton button; public Skeleton(){ setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 400); setVisible(true); menu = new JPanel(); button = new JButton("button"); menu.setSize(400, 400); menu.setBackground(Color.BLACK); menu.setVisible(true); menu.add(button); button.setLocation(200, 200); button.addActionListener(this); add(menu, BorderLayout.CENTER); } public void actionPerformed(ActionEvent a){ JPanel panel = Game.Game(); this.remove(menu); this.add(panel); } public static void main(String args[]){ new Skeleton(); } } </code></pre> <p>The actionPerformed calls the panel created in this class</p> <pre><code>package buttonMenu; import java.awt.Color; import javax.swing.JPanel; public class Game{ public static JPanel Game(){ JPanel panel = new JPanel(); panel.setSize(400, 400); panel.setBackground(Color.WHITE); return panel; } } </code></pre> <p>Again, if anybody could explain to me what's wrong with this code I would appreciate it. Thanks</p>
 

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