我如何检查的JButton是pressed?如果isEnable()不工作? [英] How can I check that JButton is pressed? If the isEnable() is not work?

查看:270
本文介绍了我如何检查的JButton是pressed?如果isEnable()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查的JButton是pressed?我知道有它的名字是isEnabled的方法

How can I check that JButton is pressed? I know that there is a method that its name is "isEnabled"

于是,我试着写一个code测试。

So I try to write a code to test.


  1. 这code有2个Jbuttons中这是添加按钮和结帐按钮。

  2. 的code将显示添加按钮pressed的消息时,我preSS结帐按钮后,我preSS添加按钮,但如果添加按钮不可结帐按钮之前pssed $ p $是pssed $ p $中,code将显示添加按钮没有pressed的消息。

在这里,code:

final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    }
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (btnAdd.isEnabled()) {
            System.out.println("Add Button is pressed");
        }
        if (!btnAdd.isEnabled()) {
            System.out.println("Add Button is not pressed");
        }
    }
});

当我运行这个code时,code只给出了添加按钮pressed虽然我没preSS的添加按钮。为什么会出现这样呢?

When I run this code,the code give only the " Add button is pressed" although I didn't press the "Add" Button. Why does it occur like that?

推荐答案

的JButton 拥有的模型,回答这些问题:

JButton has a model which answers these question:


  • isArmed()

  • 是pressed()

  • isRollOVer()

  • isArmed(),
  • isPressed(),
  • isRollOVer()

等。因此,你可以要求你正在寻找的答案型号:

etc. Hence you can ask the model for the answer you are seeking:

     if(jButton1.getModel().isPressed())
        System.out.println("the button is pressed");

这篇关于我如何检查的JButton是pressed?如果isEnable()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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