简体   繁体   English

当鼠标悬停在它上面时如何阻止JButton启用?

[英]How to stop JButton being enabled when mouse hovers over it?

I created a frame using NetBeans. 我使用NetBeans创建了一个框架。 The frame has two buttons A and B. Button A is initially disabled. 框架有两个按钮A和B.按钮A最初被禁用。 It is to be enabled only when button B is clicked. 只有在单击按钮B时才会启用它。

public newFrame() {    //newFrame is the name of the frame that has buttons A&B
    initComponents();

    btn_A.disable();
}

    private void btn_BActionPerformed(java.awt.event.ActionEvent evt)
{
    btn_A.enable();
}

The problem is that button A becomes active/enabled when the mouse is moved over it ie inspite of whether button B is clicked or not. 问题是当鼠标移过它时按钮A变为活动/启用,即使按钮B是否被点击。 How can i fix this? 我怎样才能解决这个问题?

I want button A to be enabled only after button B is clicked and not as a result of any other event. 我希望只有在单击按钮B后才能启用按钮A,而不是任何其他事件的结果。

使用btn_A.setEnabled(false)代替btn_A.disable()

btn_A.enable() is a deprecated method. btn_A.enable()是一种不推荐使用的方法。
To do this task, you could replace it by btn_A.setEnabled(false); 要执行此任务,您可以将其替换为btn_A.setEnabled(false); to disable the button and btn_A.setEnabled(true); 禁用按钮和btn_A.setEnabled(true); to enable the button. 启用按钮。

Also, one more suggestion is, add statements like the following in your method if you feel something wrong happening: 另外,还有一个建议是,如果您觉得发生了错误,请在您的方法中添加如下所示的语句:

    System.out.println("Some statement relevant to the method"); 

The main aim of those extra statements being you know when the method was actually executed. 当方法实际执行时,您知道这些额外语句的主要目的。

Try the following code: 请尝试以下代码:

button. addMouseListener(new MouseAdapter() { 
      public void mouseEntered(MouseEvent me) { 
            button.setEnable(true); 
      } 

      public void mouseExited(MouseEvent me) { 
           button.setEnable(false); 
      } 

    }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM