gpt4 book ai didi

java - 使鼠标按钮触发类事件

转载 作者:搜寻专家 更新时间:2023-11-01 03:42:52 24 4
gpt4 key购买 nike

我真的很难制作一个上面有按钮的程序,当单击一个按钮时,它会调用一个类来工作。我现在只使用 Java 大约 10 周,我掌握了基础知识,但我还没有找到任何让我了解我想在这里做什么的地方。

我试过了

public void mouseEntered(MouseEvent e) {

if (e.getButton()== MouseEvent.BUTTON3){
Object triangle;
Frame.class.getClass();
}

}

我也试过

panel.addMouseListener(new MouseAdapter() {
if (e.getButton()== MouseEvent.BUTTON1) {
Frame.class.getClass(circle); }

无论我尝试过哪种方式,我通常都会收到无法找到对象的错误,或者类型 Object 中的方法 getClass() 不适用于参数(JButton).

谁能帮我弄清楚我做错了什么?

谢谢。

最佳答案

public class MainFrame extends JFrame {

private JButton button = new JButton("Run AnotherClass");

MainFrame() {
super();
this.setTitle("Demo App");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(button);
button.addActionListener(new ButtonHandler());
}

public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
new AnotherClass();
}
}

public static void main(String[] args) {
new MainFrame().setVisible(true);
}

public class AnotherClass {
public AnotherClass() {
JOptionPane.showMessageDialog(null, "AnotherClass is in operation");
}

关于java - 使鼠标按钮触发类事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271944/

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