Sabtu, 02 Mei 2026

JAVA - Create Game Launcher (Gemini AI & Copilot)

 

prompt :
java netbeans create opening game with jpanel 1 with button play, close if button play clicked then jpanel 2 is opened text="game begin"

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.

Java
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

FeatureDescription
CardLayoutThink 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.
GridBagLayoutA simple way to ensure your buttons and text stay perfectly centered in the middle of the window.
Lambda ExpressionThe 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:

  1. Create a New JFrame Form: Right-click package > New > JFrame Form.

  2. Add a Panel: Drag a JPanel onto the Frame. Right-click it > Set Layout > Card Layout. (Let's call this mainPanel).

  3. Add Sub-Panels: Drag two more JPanel components inside mainPanel.

    • In the Navigator (bottom left), give them variable names like menuPanel and gamePanel.

  4. Design:

    • Select menuPanel and drag a JButton into it.

    • Select gamePanel and drag a JLabel into it.

  5. The Code: Double-click the "Play" button to generate the action event and paste this inside:

    Java
    CardLayout 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.

Java
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class OpeningGame extends JFrame { private CardLayout cardLayout; private JPanel mainPanel; public OpeningGame() { setTitle("Opening Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); setLocationRelativeTo(null); // Center window // CardLayout to switch between panels cardLayout = new CardLayout(); mainPanel = new JPanel(cardLayout); // Create panels JPanel panel1 = createOpeningPanel(); JPanel panel2 = createGamePanel(); // Add panels to CardLayout mainPanel.add(panel1, "Opening"); mainPanel.add(panel2, "Game"); add(mainPanel); setVisible(true); } // Panel 1: Opening screen with Play and Close buttons private JPanel createOpeningPanel() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); JButton playButton = new JButton("Play"); JButton closeButton = new JButton("Close"); // Play button action playButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cardLayout.show(mainPanel, "Game"); } }); // Close button action closeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); panel.add(playButton); panel.add(closeButton); return panel; } // Panel 2: Game screen private JPanel createGamePanel() { JPanel panel = new JPanel(); JLabel label = new JLabel("Game Begin"); label.setFont(new Font("Arial", Font.BOLD, 20)); panel.add(label); return panel; } public static void main(String[] args) { // Run GUI in Event Dispatch Thread SwingUtilities.invokeLater(() -> new OpeningGame()); } }

How it works:

  1. CardLayout is used to switch between JPanel 1 and JPanel 2.
  2. Play button → cardLayout.show(mainPanel, "Game") switches to the game panel.
  3. Close button → System.exit(0) closes the app.
  4. NetBeans:
    • Create a new Java Application project.
    • Add this code to a .java file.
    • Run it — you’ll see the opening screen with two buttons.

Tidak ada komentar: