如何为秋千中的按钮创建单击事件? [英] How to create on click event for buttons in swing?

查看:56
本文介绍了如何为秋千中的按钮创建单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是检索文本字段的值,并在单击按钮时将其显示在警报框中.如何为Java Swing中的按钮生成on click事件?

My task is to retrieve a value of text field and display it in a alert box when clicking on the button. how to generate the on click event for button in Java Swing?

推荐答案

为此,您需要使用

For that, you need to use ActionListener, for example:

JButton b = new JButton("push me");
b.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //your actions
    }
});

要以编程方式生成点击事件,可以使用 JButtondoClick() 方法:b.doClick();

For generating click event programmatically, you can use doClick() method of JButton: b.doClick();

这篇关于如何为秋千中的按钮创建单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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