Java JButton操作侦听器不工作

Java JButton操作侦听器不工作,java,swing,actionlistener,Java,Swing,Actionlistener,我正在制作一个简短的文本冒险游戏作为我的第一个大型应用程序,我遇到了一个问题。我在游戏中使用GUI,我的问题是第二个按钮的动作侦听器,继续。除方法按钮操作外,所有其他代码都有效。我在procedue中添加了一个操作侦听器,每当我运行代码并单击“继续”时,都不会发生任何事情,我也不知道为什么。我将现在在init2()中找到的代码从actionPerformed()移动,但没有任何效果。我还尝试在按钮操作上使用@Override,但它给了我一个错误“SimpleGui类型的方法按钮操作(Action

我正在制作一个简短的文本冒险游戏作为我的第一个大型应用程序,我遇到了一个问题。我在游戏中使用GUI,我的问题是第二个按钮的动作侦听器,
继续
。除方法
按钮操作
外,所有其他代码都有效。我在
procedue
中添加了一个操作侦听器,每当我运行代码并单击“继续”时,都不会发生任何事情,我也不知道为什么。我将现在在
init2()
中找到的代码从
actionPerformed()
移动,但没有任何效果。我还尝试在
按钮操作
上使用
@Override
,但它给了我一个错误“SimpleGui类型的方法按钮操作(ActionEvent)必须重写或实现超类型方法”。请记住,我的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
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
进口类。流氓;
导入类。战士;
导入类向导;
公共类SimpleGui扩展JFrame实现ActionListener{
公共JPanel面板控制,面板,面板2,面板3,面板4;
公共JButton创建、继续;
公共JTextField名称;
公共最终JTextField textField=新JTextField();
公共JComboxPlayerClass;
公共字符播放器1;
公共字符串[]classOptions={“盗贼”、“向导”、“战士”};
公共jlabeltextObject;
SimpleGui(){
超级(“RPG任务”);
name=新的JTextField(20);
init();
此.setVisible(true);
这个。设置大小(455250);
此参数为.setresizeable(false);
}//结束SimpleGui
公共void init(){
panel=新的JPanel();
panel3=新的JPanel();
panel2=新的JPanel();
playerClass=新的JComboBox(类选项);
create=新的JButton(“创建字符”);
create.addActionListener(this);
textObject=newjlabel(“名称”);
面板订单(BorderFactory)
.createTitleBorder(“创建您的角色”);
panel.add(textObject);
面板。添加(名称);
panel.add(playerClass);
面板.立根背景(颜色.白色);
面板3.订单(边界工厂)
.createTitleBorder(“类描述”);
面板3.添加(新标签)(
“盗贼有20%的几率对敌人造成致命一击。”);
面板3.添加(新标签)(
“巫师只能施展法术,命中率只有70%,”;
面板3.添加(新标签)(
“但是法术比近战攻击造成的伤害更大。”);
面板3.添加(新标签)(
“战士有30%的几率阻挡来袭的攻击。”);
镶板3.立根背景(颜色:白色);
面板2.添加(创建);
镶板2.立根背景(颜色:白色);
本条添加(面板);
本条增补(第3组);
本条增补(第2组);
这个包();
此参数为.setresizeable(true);
此.setLayout(新的BorderLayout());
此.add(面板,BorderLayout.NORTH);
本条增补(第3组);
添加(面板2,BorderLayout.SOUTH);
}//端部空隙
@凌驾
已执行的公共无效操作(操作事件){
if(event.getSource()==create){
字符串类型=类选项[playerClass.getSelectedIndex()];
如果(类型==“流氓”){
player1=新盗贼();
}
如果(类型==“向导”){
player1=新建向导();
}
如果(类型==“战士”){
player1=新战士();
}
player1.name=name.getText();
showMessageDialog(这是“您是”+类型
+“。您的名字是“+player1.name+”;
init2();
}
}
public void init2(){
这个。移除(面板);
这一点。移除(面板2);
这一点。移除(面板3);
面板。移除(textObject);
面板。移除(名称);
面板。移除(playerClass);
面板订单(BorderFactory)
.createTitledBorder(“信息”);
面板.立根背景(颜色.白色);
继续=新按钮(“继续”);
继续。addActionListener(此);
小组。添加(继续);
setText(“你好”);
textField.setEditable(false);
panelControl=新的JPanel();
面板控制。背景(颜色。白色);
panelControl.setboorder(BorderFactory.createBevelOrder(NORMAL));
panelControl.add(textField);
添加(panelControl,BorderLayout.NORTH);
此.add(面板,BorderLayout.SOUTH);
这个包();
}
公共作废按钮操作(ActionEvent事件){
继续。addActionListener(此);
如果(event.getSource()==继续){
showMessageDialog(这是“它可以工作!”);
System.out.println(“hi”);
}
}
}
//末级
您已经编写了

proceed.addActionListener(this);
因此,每当您单击
JButton
继续
时,您基本上是在调用类
SimpleGui的
actionPerformed(ActionEvent事件)
。现在,
actionPerformed
方法没有任何用于
procedure
的代码。所以,它什么也不做

我建议您在类
SimpleGui
actionPerformed
方法中添加
procedure
变量的代码。您可以在现有代码之后添加以下内容
actionPerformed
,如下所示:

@Override
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == create) {
        //Your existing code;
    } else if (event.getSource() == proceed){
        //put the code for proceed action here.
    }
}
我希望这能解决你的问题。

你已经写好了

proceed.addActionListener(this);
因此,每当您单击
JButton
继续
时,您基本上是在调用类
SimpleGui的
actionPerformed(ActionEvent事件)
。现在,
actionPerformed
方法没有任何用于
procedure
的代码。所以,它什么也不做

我建议您在中添加
procedure
变量的代码
proceed.addActionListener(this);
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");
{

}