[英]How to add ActionListener to JButton
我知道如果我使用名稱聲明 JButton,我可以將 ActionListener 添加到 JButton。
JButton showDialogButton = new JButton("Click Me");
showDialogButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
JDialog d = new JDialog(frame, "Hello", true);
d.setLocationRelativeTo(frame);
d.setVisible(true);
}
});
但是如果我有以下代碼該怎么辦:
MyJFrame frame = new MyJFrame();
frame.setSize(500,300);
JPanel base = new JPanel();
base.setLayout(new BorderLayout());
JPanel north = new JPanel();
north.add(new JLabel("Name"));
north.add(new JTextField());
north.add(new JButton("Enter"));
north.add(new JButton("Exit"));
我將不勝感激任何答案。
為了向您的JButton
添加ActionListener
,您必須保留對它的引用,這就是為什么您不能將它作為new
傳遞給JPanel
的構造函數。
解決方案本質上是做你之前做的,即分別聲明+初始化它們: JButton myButton = new JButton("My Button");
然后像以前一樣將ActionListener
添加到您的JPanel
:
myButton.addActionListener(new ActionListener() ...);
然后就在north.add(myButton);
.
在 add 之外聲明它們
JButton exit = new JButton("exit");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//do stuff
}
});
north.add(exit);
然后對要添加偵聽器的所有其他組件執行相同操作
更新:
使用此處提到的雙括號初始化語法,可以更好地將 ActionListener 添加到匿名對象。 這是一個例子:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GuiTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 300);
JPanel base = new JPanel();
base.setLayout(new BorderLayout());
JPanel north = new JPanel();
Component comp1 = north.add(new JLabel("Name"));
System.out.println("comp1 class type: " + comp1.getClass().getName());
Component comp2 = north.add(new JTextField());
System.out.println("comp2 class type: " + comp2.getClass().getName());
Component comp3 = north.add(new JButton("Enter"));
System.out.println("comp3 class type: " + comp3.getClass().getName());
north.add(new JButton("Exit") {{
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("EXIT");
}
});
}});
base.add(north);
frame.getContentPane().add(base);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
搜索了Java API,發現add
方法返回的是被添加的組件。 不幸的是,它只是一個通用的Component
對象,沒有強制轉換就不能鏈接。 但是你可以像這樣獲得添加的對象:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GuiTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 300);
JPanel base = new JPanel();
base.setLayout(new BorderLayout());
JPanel north = new JPanel();
Component comp1 = north.add(new JLabel("Name"));
System.out.println("comp1 class type: " + comp1.getClass().getName());
Component comp2 = north.add(new JTextField());
System.out.println("comp2 class type: " + comp2.getClass().getName());
Component comp3 = north.add(new JButton("Enter"));
System.out.println("comp3 class type: " + comp3.getClass().getName());
((JButton)north.add(new JButton("Exit")))
.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("EXIT");
}
});
base.add(north);
frame.getContentPane().add(base);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
這段代碼是完整且可驗證的(我在我的 Arch Linux x64 機器上測試過)。 它有點丑,但有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.