问题描述
我几年来没有碰到Java,现在我回来了,试图做一个GUI来测试JFrame。我试图创建一个JButton将单击关闭程序。使用当前代码我收到错误javax.swing.AbstractButton类型中的方法addActionListener(java.awt.event.ActionListener)不适用于参数(new ActionListener(){})
。我发现的其他问题处理多个类或其他问题,不帮助我的问题。任何解决方案或替代品将不胜感激。
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(我要多长时间运行?
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));
}
}
}
-
总是在
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 javaxueswing.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 );
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Box box = Box.createVerticalBox();
JLabel time = new JLabel(0);
time .setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel label = new JLabel(我多久运行一次?);
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();
}
});
}
}
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)); } } }
解决方案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 aWINDOW_CLOSING
event from your button.Call
setVisible()
after you add components andpack()
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(); } }); } }
这篇关于无法将动作侦听器添加到JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!