prompt :
Creating a simple transition between two screens (panels) in NetBeans is most efficiently handled using a CardLayout. This allows you to stack multiple JPanel components and "flip" between them.
Here is a clean, step-by-step guide to setting this up.
1. The Main Frame Setup
The JFrame acts as the container. We will use a JPanel with a CardLayout to hold both your Menu and the Game screen.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameLauncher extends JFrame {
// CardLayout helps us switch between panels
private CardLayout cardLayout = new CardLayout();
private JPanel mainContainer = new JPanel(cardLayout);
public GameLauncher() {
setTitle("Simple Game Launcher");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create the two panels
JPanel menuPanel = createMenuPanel();
JPanel gamePanel = createGamePanel();
// Add panels to the container with unique names
mainContainer.add(menuPanel, "Menu");
mainContainer.add(gamePanel, "Game");
add(mainContainer);
setVisible(true);
}
// --- Panel 1: The Opening Menu ---
private JPanel createMenuPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.setLayout(new GridBagLayout()); // Centers the button
JButton playButton = new JButton("PLAY");
playButton.setPreferredSize(new Dimension(100, 40));
// Action: Switch to the "Game" panel when clicked
playButton.addActionListener(e -> cardLayout.show(mainContainer, "Game"));
panel.add(playButton);
return panel;
}
// --- Panel 2: The Game Screen ---
private JPanel createGamePanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(new GridBagLayout());
JLabel text = new JLabel("GAME BEGIN");
text.setForeground(Color.WHITE);
text.setFont(new Font("Arial", Font.BOLD, 24));
panel.add(text);
return panel;
}
public static void main(String[] args) {
// Run the GUI on the Event Dispatch Thread
SwingUtilities.invokeLater(GameLauncher::new);
}
}
2. Key Components Explained
| Feature | Description |
| CardLayout | Think of this like a deck of cards. Only one "card" (panel) is visible at a time. |
| cardLayout.show() | This method takes the container and the String name of the panel you want to display. |
| GridBagLayout | A simple way to ensure your buttons and text stay perfectly centered in the middle of the window. |
| Lambda Expression | The e -> ... syntax is a shorthand way to write the ActionListener for your button. |
3. How to do this in NetBeans GUI Builder (Design Mode)
If you prefer using the Drag-and-Drop interface in NetBeans:
Create a New JFrame Form: Right-click package > New > JFrame Form.
Add a Panel: Drag a
JPanelonto the Frame. Right-click it > Set Layout > Card Layout. (Let's call thismainPanel).Add Sub-Panels: Drag two more
JPanelcomponents insidemainPanel.In the Navigator (bottom left), give them variable names like
menuPanelandgamePanel.
Design:
Select
menuPaneland drag aJButtoninto it.Select
gamePaneland drag aJLabelinto it.
The Code: Double-click the "Play" button to generate the action event and paste this inside:
JavaCardLayout cl = (CardLayout) mainPanel.getLayout(); cl.show(mainPanel, "gamePanel");(Note: NetBeans usually names the cards based on the variable name of the panel added).
Copilot AI
Got it ✅ — you want a Java Swing program (can be built in NetBeans) with:
- JPanel 1: Contains Play and Close buttons.
- If Play is clicked → Switch to JPanel 2 with text
"Game Begin". - If Close is clicked → Exit the program.
Here’s a complete, runnable Java program that does exactly that.
It uses a CardLayout to switch between panels.
How it works:
- CardLayout is used to switch between
JPanel 1andJPanel 2. - Play button →
cardLayout.show(mainPanel, "Game")switches to the game panel. - Close button →
System.exit(0)closes the app. - NetBeans:
- Create a new Java Application project.
- Add this code to a
.javafile. - Run it — you’ll see the opening screen with two buttons.


Tidak ada komentar:
Posting Komentar