gpt4 book ai didi

java - JButton Action 监听器不工作

转载 作者:行者123 更新时间:2023-11-29 03:07:27 24 4
gpt4 key购买 nike

我正在制作一个简短的文字冒险游戏作为我的第一个大型应用程序,但我遇到了一个问题。我在我的游戏中使用 GUI,我的问题是我为第二个按钮 proceed 使用的 Action 监听器。除了方法 buttonAction 之外,所有其他代码都有效。我向 proceed 添加了一个 Action 监听器,每当我运行代码并单击继续时,什么也没有发生,我也不知道为什么。我将现在在 init2() 中找到的代码从 actionPerformed() 中移走,但它没有任何效果。我也试过在 buttonAction 上使用 @Override,但它给了我错误“SimpleGui 类型的方法 buttonAction(ActionEvent) 必须覆盖或实现父类(super class)型方法”。请记住,我的 Java 技能充其量只是初级的,所以请尽量解释清楚。任何帮助将不胜感激。我的应用程序的代码如下。

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Classes.Rogue;
import Classes.Warrior;
import Classes.Wizard;

public class SimpleGui extends JFrame implements ActionListener {

public JPanel panelControl, panel, panel2, panel3, panel4;
public JButton create, proceed;
public JTextField name;
public final JTextField textField = new JTextField();
public JComboBox playerClass;
public Char player1;
public String[] classOptions = { "Rogue", "Wizard", "Warrior" };
public JLabel textObject;

SimpleGui() {

super("RPG Quest");
name = new JTextField(20);
init();
this.setVisible(true);
this.setSize(455, 250);
this.setResizable(false);
}// end SimpleGui

public void init() {

panel = new JPanel();
panel3 = new JPanel();
panel2 = new JPanel();
playerClass = new JComboBox(classOptions);
create = new JButton("Create Character");
create.addActionListener(this);
textObject = new JLabel("Name");

panel.setBorder(BorderFactory
.createTitledBorder("<html><font color:black>Create Your Character</font></html>"));
panel.add(textObject);
panel.add(name);
panel.add(playerClass);
panel.setBackground(Color.WHITE);

panel3.setBorder(BorderFactory
.createTitledBorder("<html><font color:black>Class Descriptions</font></html>"));
panel3.add(new JLabel(
"<html><font color:black>The Rogue has a 20% chance to land Critical Hits on enemies.</font></html>"));
panel3.add(new JLabel(
"<html><font color:black>The Wizard can only cast spells and has only a 70% chance of landing a hit,</font></html>"));
panel3.add(new JLabel(
"<html><font color:black>but spells do more damage than melee attacks.</font></html>"));
panel3.add(new JLabel(
"<html><font color:black>The Warrior has a 30% chance to block an incoming attack.</font></html>"));
panel3.setBackground(Color.WHITE);

panel2.add(create);
panel2.setBackground(Color.WHITE);

this.add(panel);
this.add(panel3);
this.add(panel2);
this.pack();
this.setResizable(true);
this.setLayout(new BorderLayout());

this.add(panel, BorderLayout.NORTH);
this.add(panel3);
this.add(panel2, BorderLayout.SOUTH);
}// end void

@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == create) {

String type = classOptions[playerClass.getSelectedIndex()];

if (type == "Rogue") {
player1 = new Rogue();
}
if (type == "Wizard") {
player1 = new Wizard();
}
if (type == "Warrior") {
player1 = new Warrior();
}
player1.name = name.getText();

JOptionPane.showMessageDialog(this, "You are a " + type
+ ". Your name is " + player1.name + ".");

init2();
}
}

public void init2() {

this.remove(panel);
this.remove(panel2);
this.remove(panel3);

panel.remove(textObject);
panel.remove(name);
panel.remove(playerClass);
panel.setBorder(BorderFactory
.createTitledBorder("<html><font color:black>Info</font></html>"));
panel.setBackground(Color.WHITE);
proceed = new JButton("Proceed");
proceed.addActionListener(this);
panel.add(proceed);

textField.setText("Hello There");
textField.setEditable(false);

panelControl = new JPanel();
panelControl.setBackground(Color.WHITE);
panelControl.setBorder(BorderFactory.createBevelBorder(NORMAL));
panelControl.add(textField);

this.add(panelControl, BorderLayout.NORTH);
this.add(panel, BorderLayout.SOUTH);
this.pack();
}

public void buttonAction(ActionEvent event) {
proceed.addActionListener(this);

if (event.getSource() == proceed) {
JOptionPane.showMessageDialog(this, "It Works!");
System.out.println("hi");
}
}
}

// end class

最佳答案

由于您的类实现了 ActionListener,它必须为 actionPerformed 定义一个实现,您已经正确完成了。现在当你说

proceed.addActionListener(this);

您实际上是将按钮连接到类中定义的 actionPerformed,而不是连接到您似乎希望连接到的 buttonAction() 方法。这是 create 连接到的同一个监听器。你应该能够通过使用来解决这个问题

public void actionPerformed(ActionEvent event) {
if (event.getSource() == create) {

String type = classOptions[playerClass.getSelectedIndex()];

if (type == "Rogue") {
player1 = new Rogue();
}
if (type == "Wizard") {
player1 = new Wizard();
}
if (type == "Warrior") {
player1 = new Warrior();
}
player1.name = name.getText();

JOptionPane.showMessageDialog(this, "You are a " + type
+ ". Your name is " + player1.name + ".");

init2();


} else if (event.getSource() == proceed)
{
JOptionPane.showMessageDialog(this, "It Works!");
System.out.println("hi");
{

}

这将使 buttonAction() 变得不必要。我要补充的是,您可以将监听器定义为内部类,然后将按钮单独连接到它们,从而使代码更清晰,而不是实现 ActionListener。供您研究的东西:)

关于java - JButton Action 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398426/

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