![](/img/trans.png)
[英]Is it legal to add the same action listener to a JComboBox and JButton
[英]Unable to Add an Action Listener to JButton
幾年來我還沒有接觸過Java,但是現在我回來了,試圖制作一個GUI來測試JFrame。 我試圖創建一個JButton,單擊該按鈕將關閉程序。 使用當前代碼,我收到錯誤"The method addActionListener(java.awt.event.ActionListener) in the type javax.swing.AbstractButton is not applicable for the arguments (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("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));
}
}
}
我更改了一些內容以使您的程序正常運行。
始終在事件分發線程上構建Swing程序。
使用Swing Timer
可以使某些事情偶爾發生。 循環運行只會使房間變熱。
使用JFrame.EXIT_ON_CLOSE
進行默認的關閉操作,然后從EXIT按鈕發送WINDOW_CLOSING
事件。
添加組件並pack()
框架后,調用setVisible()
。
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();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.