简体   繁体   中英

ActionListener not implemented

I was learning about JRadiobuttons and I made three JRadioButtons in this particular program. The JRadioButtons were pizzaButton, burgerButton and hotdogButton. I wanted to print specific lines when one of them is selected by the user. I overrided the ActionPerformed method and even implemented the ActionListener for each one of the RadioButtons. But after every try, the program seemed to ignore the ActionPerformed method. `


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");
        }
    }
}

``

I tried checking the things that could go wrong and double checked whether I added the the ActionListener to the JRadioButtons.

In actionPerformed() you reference the buttons as class attributes, whereas in the constructor you use local variables, which mean different objects. Try to change:

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

into


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

you overwrite your references, the actionPerformed compares 2 different objects.

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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