简体   繁体   中英

JButton ActionListener not responding

I couldn't find the answer anywhere else online, so I came here. I apologize in advance if the mistake in my code is very obvious; I'm still quite new to java swing. Here's what's going on: I have created a JButton named toggleElevators , and I want it to change text when clicked. I have already created an ActionListener and added it to toggleElevators . All I want right now is for the JButton to change text when clicked from Click me to Clicked .

First, here's a picture of what the JFrame looks like when executed:

的JFrame

NOTE: There is a third class, but it is purely for drawing the picture on the left. It has nothing to do with the GridLayout or the JButton.

Run class (created frame and adds toggleElevators JButton:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;

public class Run extends Input{

Input i = new Input();

public static void main(String[] args) {
    new Run();
}

public Run() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Elevators");
            frame.setLayout(new GridLayout(0, 3));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new Elevators(Color.MAGENTA, true));
            frame.add(new Elevators(Color.ORANGE, false));
            frame.setSize(800,600);
            frame.setResizable(false);

            frame.getContentPane().add(toggleElevators); //adds toggleElevators button to JFrame
            i.addButtonListeners(); //calls method defined in Input class, which adds the ActionListener to the toggleElevators button

            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

}

Input class (creates toggleElevators JButton and its ActionListener):

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

import javax.swing.JButton;

public class Input {
JButton toggleElevators = new JButton("Click me.");

public void addButtonListeners() {
    toggleElevators.addActionListener(new toggleElevatorsListener());
}

class toggleElevatorsListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        toggleElevators.setText("Clicked.");
        System.out.println("ActionListener called."); //I know the ActionListener is not being called because this line is not being printed out in the console
    }
}
}

Your Run class extends Input, but also HAS an Input named i . You're adding this.toggleElevators to the frame, but you're adding a listener to i.toggleElevators .

Remove the i field from your class. I would also forget completely about defining and extending an Input class. It doesn't serve any purpose, and seems to confuse more than help you.

You create a new Input in your Run class, while the Run class also extends Input .

When you call i.addButtonListeners(); the action listeners are added on the toggleElevators from i and not on the toggleElevators you inherited from the Input class.

Try addButtonListeners() .

Your Run class extends Input . Therefore it has its own toggleElevators which is the one it sets in the frame. However, i has is own toggleElevators where it sets the event listeners. So they are not set on the one in the frame but on one that never gets used.

You can simply delete the i object. As Run extends Input , it can call the method directly, and then the listener will be added to its own toggleElevators .

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