Unable to Add an Action Listener to JButton

2019-07-25 15:19发布

问题:

I haven't touched Java in a few years and now I'm back, trying to make a GUI to test out JFrame. I am attempting to create a JButton that will close the program when clicked. With the current code I am receiving the error "The method addActionListener(java.awt.event.ActionListener) in the type javax.swing.AbstractButton is not applicable for the arguments (new ActionListener(){})". The other questions I have found deal with multiple classes or other issues that don't help with my problem. Any solution or alternative would be appreciated.

import javax.swing.*;
import java.awt.*;

public class testFrame
{
  public static void main(String args[])
  {
    long base = System.currentTimeMillis();

    JFrame frame = new JFrame("Test Window");
    frame.setLocationRelativeTo(null);
    frame.setUndecorated(true);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    JLabel label = new JLabel();
    JLabel label2 = new JLabel("How Long Have I Been Running?");
    JButton button = new JButton("EXIT");
    button.addActionListener(new ActionListener(){
       public void actionPerformed (ActionEvent e) {
          JFrame.dispose();
       }
    });
    Box box = Box.createVerticalBox();
    box.add(label2);
    box.add(label);
    box.add(button);
    label2.setAlignmentX(Component.CENTER_ALIGNMENT);
    label.setAlignmentX(Component.CENTER_ALIGNMENT);
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    frame.getContentPane().add(box, BorderLayout.CENTER);

    while(true)
    {

       long input = System.currentTimeMillis();
       label.setText(Long.toString(input - base));
    }
  }
}

回答1:

I changed some things to get your program going.

  • Always build Swing programs on the event dispatch thread.

  • Use a Swing Timer to make something happen every once in a while; running a loop flat out just makes the room hotter.

  • Use JFrame.EXIT_ON_CLOSE for the default close operation, and send a WINDOW_CLOSING event from your EXIT button.

  • Call setVisible() after you add components and pack() the frame.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestFrame {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test Window");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //frame.setUndecorated(true);
                //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                Box box = Box.createVerticalBox();
                JLabel time = new JLabel("0");
                time.setAlignmentX(Component.CENTER_ALIGNMENT);
                JLabel label = new JLabel("How Long Have I Been Running?");
                label.setAlignmentX(Component.CENTER_ALIGNMENT);
                JButton button = new JButton("EXIT");
                button.setAlignmentX(Component.CENTER_ALIGNMENT);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.dispatchEvent(new WindowEvent(
                            frame, WindowEvent.WINDOW_CLOSING));
                    }
                });
                box.add(label);
                box.add(time);
                box.add(button);
                frame.add(box, BorderLayout.CENTER);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                long base = System.currentTimeMillis();
                Timer timer = new Timer(100, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long input = System.currentTimeMillis();
                        time.setText(Long.toString(input - base));
                    }
                });
                timer.start();
            }
        });
    }
}