2
votes

I have 2 panel (2 class, extends from JPanel), 1 frame (1 class, extends from JFrame)

My first panel - WelcomePanel:

package caro;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class WelcomePanel extends JPanel {

    public WelcomePanel() {

        ImageIcon logoImage = new ImageIcon("/home/khanhpq/logo.png");
        JButton playButton = new JButton("Play");
        JButton exitButton = new JButton("Exit");

        JLabel imageLabel = new JLabel(logoImage);

        add(imageLabel);
        add(playButton);
        add(exitButton);

        playButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {



            }
        });

        exitButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int option = JOptionPane.showConfirmDialog(null, "Are you sure ?", "Warning", JOptionPane.YES_NO_OPTION);               
                if(option == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });

    }

}

My second panel - BoardPanel:

package caro;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

public class BoardPanel extends JPanel {    

    public BoardPanel() {

        JPanel boardPanel = new JPanel();

        Board board = new Board();

        CellButton cellButton[] = new CellButton[144];

        GridLayout gridLayout = new GridLayout(12, 12);     
        boardPanel.setLayout(gridLayout);       

        for (int i = 0; i < 144; i++) {         
                cellButton[i] = new CellButton();               
                boardPanel.add(cellButton[i]);              
        }

    }

}

My main frame - MainFrame

package caro;

import javax.swing.JFrame;

public class MainFrame extends JFrame {

    public MainFrame() {
        add(new WelcomePanel());
        setSize(360, 380);
        setVisible(true);

    }

    public static void main(String[] args) {
        MainFrame startFrame = new MainFrame();
    }

}

My question: Help me write code, addActionListener on buttons of panels (material example). When i press play button (of WelcomePanel), WelcomePanel is hidden and BoardPanel is show. And, when i exit BoardPanel (close button is pressed, or click x button), WelcomePanel is showed. My friend recommend use Message and Handle, but I don't know. Please help me. Thanks.

1
1) For many components in one space, use a CardLayout as seen in this short example. 2) Don't extend frame or other top level containers. Instead create & use an instance of one. The same for JPanel in this case, the code is not changing or adding functionality to the panel, so just use (2x) an instance JPanel.Andrew Thompson
You can use a CardLayout, as shown in this examplenIcE cOw

1 Answers

2
votes

Its better to declare dependencies (Component like buttons, panels, etc...) as fields. In this way they are visible for a third class that is the Controller of them. In next examaple I make MainFrame controlling itself, just an example. Read about Presentation patterns for better pratices.

WelcomePanel.java

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class WelcomePanel extends JPanel {

    /* Declare your dependecies as fields,
     * so you can hold a reference.
     */
    ImageIcon logoImage;
    JButton playButton;
    JButton exitButton;
    JLabel imageLabel;

    public WelcomePanel() {

        logoImage = new ImageIcon("/home/khanhpq/logo.png");
        playButton = new JButton("Play");
        exitButton = new JButton("Exit");
        imageLabel = new JLabel(logoImage);

        add(imageLabel);
        add(playButton);
        add(exitButton);

    }

}

BoardPanel.java

import javax.swing.JButton;
import javax.swing.JPanel;

public class BoardPanel extends JPanel {

    /* Declare your dependecies as fields,
     * so you can hold a reference.
     */
    JButton closeButton;

    public BoardPanel() {
        closeButton = new JButton();
        add(closeButton);

    }
}

MainFrame.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class MainFrame extends JFrame implements ActionListener {

    /* Declare your dependecies as fields,
     * so you can hold a reference.
     */
    WelcomePanel welcomePanel;
    BoardPanel boardPanel;

    public MainFrame() {
        welcomePanel = new WelcomePanel();
        boardPanel = new BoardPanel();

        add(welcomePanel);
        add(boardPanel);

        boardPanel.setVisible(false);

        boardPanel.closeButton.addActionListener(this);
        welcomePanel.playButton.addActionListener(this);

        setSize(360, 380);
    }

    /**
     * This class is the controller.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(boardPanel.closeButton)) {
            welcomePanel.setVisible(false);
            boardPanel.setVisible(true);
        } else if (e.getSource().equals(welcomePanel.playButton)) {
            welcomePanel.setVisible(true);
            boardPanel.setVisible(false);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainFrame startFrame = new MainFrame();
                startFrame.setVisible(true);

            }
        });
    }
}