gpt4 book ai didi

java - 面板上的 addActionListener。如何映射到主框架?

转载 作者:行者123 更新时间:2023-11-30 09:19:33 25 4
gpt4 key购买 nike

我有 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。我的 friend 推荐使用Message和Handle,但是我不知道。请帮我。谢谢。

最佳答案

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

欢迎面板.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);

}
}

主框架.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);

}
});
}
}

关于java - 面板上的 addActionListener。如何映射到主框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com