gpt4 book ai didi

java - 获取 JButtons 的 Action 事件

转载 作者:行者123 更新时间:2023-11-29 04:55:18 24 4
gpt4 key购买 nike

您好,我正在尝试为我的 jbutton 获取操作事件,但是当我为第二个按钮尝试 e.get 源方法时,我收到错误消息,说它找不到公共(public)符号 myButton2。我认为这可能是 e.getSource 方法的问题,但我不确定。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.JPanel;

class Keypad {
public static void main(String[] args) {
new MyApplication();
}
}

class MyApplication extends JFrame implements ActionListener {
// CLASS INSTANCES AND OBJECTS
public MyApplication() {
setLayout(null);

JPanel panel1 = new JPanel();
add(panel1);

JTextArea text1 = new JTextArea(2, 12);
panel1.add(text1);
panel1.setBounds(0, 130, 370, 40);
Font textFont1 = new Font("Arial Bold", Font.BOLD, 18);

JButton myButton = new JButton("1");
myButton.setBounds(20, 190, 60, 60);

JButton myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
myButton1.setBounds(85, 190, 60, 60);
myButton1.addActionListener(this);

JButton myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
myButton2.setBounds(150, 190, 60, 60);

JButton myButton3 = new JButton("<html><center> 4 <br /> GHI </center> </html>");
myButton3.setBounds(20, 260, 60, 60);

JButton myButton4 = new JButton("<html><center> 5 <br /> JKL </center> </html>");
myButton4.setBounds(85, 260, 60, 60);

JButton myButton5 = new JButton("<html><center> 6 <br /> MNO </center> </html>");
myButton5.setBounds(150, 260, 60, 60);

JButton myButton6 = new JButton("<html><center> 7 <br /> PQRS </center> </html>");
myButton6.setBounds(20, 330, 60, 60);

JButton myButton7 = new JButton("<html><center> 8 <br /> TUV </center> </html>");
myButton7.setBounds(85, 330, 60, 60);

JButton myButton8 = new JButton("<html><center> 9 <br /> WXYZ </center> </html>");
myButton8.setBounds(150, 330, 60, 60);

JButton myButton9 = new JButton("*");
myButton9.setBounds(20, 400, 60, 60);

JButton myButton10 = new JButton("0");
myButton10.setBounds(85, 400, 60, 60);

JButton myButton11 = new JButton("#");
myButton11.setBounds(150, 400, 60, 60);

add(myButton);
add(myButton1);
add(myButton2);
add(myButton3);
add(myButton4);
add(myButton5);
add(myButton6);
add(myButton7);
add(myButton8);
add(myButton9);
add(myButton10);
add(myButton11);

setTitle("Keypad");
setSize(300, 500);
setLocation(250, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}

public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(e.getSource() == myButton2){
// EVENT HANDLING !!!!!
}
}
}

而且,我是否能够创建一种方法,当按钮被点击多次时,即像键盘一样点击 2 或 3 次时起作用。

谢谢

最佳答案

您在类的构造函数 中声明按钮,这意味着它们在类的外部不可见。因此,您需要将按钮声明为成员变量,以便类的所有方法都可以访问它们:

class myApplication extends JFrame implements ActionListener {
JButton myButton1;
JButton myButton2;
JButton myButton3;
...

myApplication() {
myButton1 = new JButton("<html><center> 2 <br /> ABC </center> </html>");
...
myButton2 = new JButton("<html><center> 3 <br /> DEF </center> </html>");
...
}

public void actionPerformed(ActionEvent e) { ... }
}

关于java - 获取 JButtons 的 Action 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com