繁体   English   中英

JButton动作监听器不起作用

[英]JButton Action Listener not working

我正在制作一个简短的文字冒险游戏,这是我的第一个大型应用程序,但遇到了一个问题。 我在游戏中使用的是GUI,而我的问题是我用于第二个按钮的动作监听器, proceed 除方法buttonAction之外,所有其他代码均有效。 我添加了一个动作侦听器来proceed ,每当我运行代码并单击“继续”时,都不会发生任何事情,而且我也不知道为什么。 我将现在在init2()找到的代码从actionPerformed() init2() ,但是它没有任何效果。 我已经使用也尝试@OverridebuttonAction ,但它给我的错误“的方法buttonAction型SimpleGui必须覆盖或实现超法(动作事件)”。 请记住,我的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

你写

proceed.addActionListener(this);

所以每当你点击JButton proceed你基本上调用actionPerformed(ActionEvent event)之类的SimpleGui 现在, actionPerformed方法没有任何用于proceed代码。 因此,它什么都不做。

我在这里建议您在类SimpleGuiactionPerformed方法中添加proceed变量的代码。 您可以在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.
    }
}

我希望这能解决您的问题。

由于您的类实现了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 ,您可以将侦听器定义为内部类,并将按钮分别连接到它们,从而使代码更简洁。 您需要研究的东西:)

您将事件处理程序设为“ this”,这意味着单击按钮时,

@Override public void actionPerformed(ActionEvent事件)

将被触发。

公共无效buttonAction(ActionEvent事件)

在您的代码中没有用。 而且您不能在其上使用@Override ,因为没有接口具有此方法签名。

如果(event.getSource()==创建)

将只允许创建按钮通过。

因此,该解决方案可以在actionPerformed方法中添加另一个条件并为“继续”按钮添加代码。

if (event.getSource() == create){...}
else if (event.getSource() == proceed){...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM