当前位置: 首页 > 知识库问答 >
问题:

ActionListener未实现

端木承业
2023-03-14

我正在学习JRadoButton,并在这个特定的程序中制作了三个JRadoButton。JRadoButton是pizzaButton、burgerButton和hotdogButton。当用户选择其中之一时,我想打印特定的行。我覆盖了ActionPer的方法,甚至为每个无线电按钮实现了ActionListener。但是每次尝试后,程序似乎都忽略了ActionPer的方法。'


public class Main {

    public static void main(String[] args) {

        new MyFrame();
    }
}

`` `

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener {

    JRadioButton pizzaButton;
    JRadioButton burgerButton;
    JRadioButton hotdogButton;

    MyFrame() {

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());

        JRadioButton pizzaButton = new JRadioButton("pizza");
        JRadioButton burgerButton = new JRadioButton("burger");
        JRadioButton hotdogButton = new JRadioButton("hot dog");

        ButtonGroup group = new ButtonGroup();
        group.add(pizzaButton);
        group.add(burgerButton);
        group.add(hotdogButton);

        pizzaButton.addActionListener(this);
        burgerButton.addActionListener(this);
        hotdogButton.addActionListener(this);

        this.setVisible(true);
        this.add(pizzaButton);
        this.add(burgerButton);
        this.add(hotdogButton);
        this.pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==pizzaButton) {
            System.out.println("You ordered a pizza");
        } else if (e.getSource()==burgerButton) {
            System.out.println("You ordered a burger");
        } else if (e.getSource()==hotdogButton) {
            System.out.println("You ordered a hot dog");
        }
    }
}

``

我尝试检查可能出错的地方,并仔细检查是否将ActionListener添加到了JRadioButtons中。

共有2个答案

蔡宏大
2023-03-14

您覆盖了您的引用,执行的操作比较了两个不同的对象。

改变

    JRadioButton pizzaButton = new JRadioButton("pizza");
    JRadioButton burgerButton = new JRadioButton("burger");
    JRadioButton hotdogButton = new JRadioButton("hot dog");

    pizzaButton = new JRadioButton("pizza");
    burgerButton = new JRadioButton("burger");
    hotdogButton = new JRadioButton("hot dog");
司寇昱
2023-03-14

在actionPerformed()中,您将按钮作为类属性引用,而在构造函数中,您使用本地变量,这意味着不同的对象。尝试更改:

        JRadioButton pizzaButton = new JRadioButton("pizza");
        JRadioButton burgerButton = new JRadioButton("burger");
        JRadioButton hotdogButton = new JRadioButton("hot dog");

到…里面


        this.pizzaButton = new JRadioButton("pizza");
        this.burgerButton = new JRadioButton("burger");
        this.hotdogButton = new JRadioButton("hot dog");
 类似资料:
  • 问题内容: JFrameWithPanel不是抽象的,并且不会重写java.awt.event.ActionListener公共类中的抽象方法actionPerformed(java.awt.event.ActionEvent)JFrameWithPanel扩展了JFrame实现ActionListener 我没有得到这个代码。Book and Java网站告诉我这是该方法的语法,但是此错误再次不

  • 我使用的和。 我已经准备好了客户端母版,它使用向导添加客户端,同时反映在可数据化中。然后我更新了可更新的编辑实用程序。然后我添加了删除按钮,并尝试调用bean的删除方法。 但是当我点击删除按钮时,添加客户端的向导验证被执行。所以我创建了另一个文件并包含在中,但结果仍然相同。现在,我想添加删除实用程序中的日期。我尝试了很多技巧来做到这一点。 我只是想通过操作监听器传递客户端标识,删除并刷新可更新的命

  • 我有以下问题: 我有这样一个函数: 我的问题如下:我需要一个解决方案,可以通过addActionListener函数作为参数传递任何对象。列表可在此处找到:https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html 有没有一种方法可以在没有大量实例的情况下解决这个问题? 谢啦

  • 问题内容: 我最近正在做一个编程任务,要求我们用代码实现由UML图指定的程序。在某一时刻,该图指定我必须创建一个匿名JButton,该JButton显示一个计数(从1开始)并在每次单击时递减。JButton及其ActionListener都必须是匿名的。 我想出了以下解决方案: 这将添加一个匿名JButton,然后添加另一个(内部)匿名ActionListener来处理事件并根据需要更新按钮的文本

  • 当用户与组件交互时,例如h:commandButton或h:link,JSF会触发可以通过两种方式处理的动作事件。 S.No 技术与描述 1 Method Binding 在UI Component的actionListener属性中传递托管bean方法的名称。 2 ActionListener 实现ActionListener接口并将实现类名称传递给UI Component的actionList

  • 处理ActionEvent的类应该实现此接口。该类的对象必须在组件中注册。 可以使用addActionListener()方法注册该对象。 当动作事件发生时,将调用该对象的actionPerformed方法。 接口声明 以下是java.awt.event.ActionListener接口的声明: public interface ActionListener extends EventLis

  • 处理ActionEvent的类应该实现此接口。 该类的对象必须在组件中注册。 可以使用addActionListener()方法注册该对象。 当动作事件发生时,将调用该对象的actionPerformed方法。 接口声明 (Interface Declaration) 以下是java.awt.event.ActionListener接口的声明 - public interface ActionLi