Java-通过JButton调用方法 [英] Java - Call Method via JButton

查看:43
本文介绍了Java-通过JButton调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过按JButton调用方法?

How can I call a method by pressing a JButton?

例如:

when JButton is pressed
hillClimb() is called;

我知道在按下JButton时如何显示消息等,但是想知道是否可以这样做?

I know how to display messages etc when pressing a JButton, but want to know if it is possible to do this?

非常感谢.

推荐答案

如果您知道如何在按下按钮时显示消息,那么您已经知道如何调用方法,因为打开新窗口就是对方法的调用.

If you know how to display messages when pressing a button, then you already know how to call a method as opening a new window is a call to a method.

有关更多详细信息,可以实现ActionListener,然后在JButton上使用addActionListener方法. 此处是有关如何编写.

With more details, you can implement an ActionListener and then use the addActionListener method on your JButton. Here is a pretty basic tutorial on how to write an ActionListener.

您也可以使用匿名类:

yourButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        hillClimb();
    } 
});

这篇关于Java-通过JButton调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆