2

我有 2 个面板(2 个类,从 JPanel 扩展),1 个框架(1 个类,从 JFrame 扩展)

我的第一个面板 - 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);
                }
            }
        });

    }

}

我的第二个面板 - 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]);              
        }

    }

}

我的主框架 - 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();
    }

}

我的问题:帮我写代码,在面板按钮上添加ActionListener(材料示例)。当我按下(WelcomePanel 的)播放按钮时,WelcomePanel 被隐藏并显示 BoardPanel。而且,当我退出 BoardPanel(按下关闭按钮或单击 x 按钮)时,会显示 WelcomePanel。我的朋友推荐使用 Message 和 Handle,但我不知道。请帮我。谢谢。

4

1 回答 1

2

最好将依赖项(如按钮、面板等组件)声明为字段。通过这种方式,它们对于作为它们的控制器的第三个类是可见的。在下一个示例中,我让 MainFrame 控制自己,这只是一个示例。阅读演示模式以获得更好的实践。

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);

            }
        });
    }
}
于 2013-07-27T23:18:08.143 回答