NETBEANS:如何使jRadioButton非静态,所以我可以在PSVM中添加mouseListener?

时间:2017-03-16 05:51:20

标签: swing netbeans jframe mouseevent jradiobutton

我只在Netbeans中使用了JFrame中的拖放功能,所以我没有编写代码。

我有一个名为btnDecafPike的JRadioButton,我希望当用户将鼠标悬停在它上面时,会出现一个JFrame。这是它的代码,它起作用:

private void btnDecafPikeMouseEntered(java.awt.event.MouseEvent evt) {                                          
    Coffee_DarkRoast c = new Coffee_DarkRoast();
    c.setVisible(true);
}  

但是当我使用这段代码时,出现的JFrame不会再消失了:

private void btnDecafPikeMouseEntered(java.awt.event.MouseEvent evt) {                                          
    Coffee_DarkRoast c = new Coffee_DarkRoast();
    c.setVisible(true);
}                                         

private void btnDecafPikeMouseExited(java.awt.event.MouseEvent evt) {                                         
    Coffee_DarkRoast c = new Coffee_DarkRoast();
    c.setVisible(false);
}  

所以,我试试这段代码:

public static void main(String args[]) {
    /* Set the Nimbus look and feel */

    btnDecafPike.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mousePressed(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseExited(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    }
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Beverage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Beverage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Beverage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Beverage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Beverage().setVisible(true);
        }
    });
}

但它给了我一个错误,并说“非静态变量btnDecafPike不能从静态上下文中引用”,那么我该如何解决呢?

编辑:

我使用了这段代码,但它只显示空的工具提示框?

private void btnDecafPikeActionPerformed(java.awt.event.ActionEvent evt) {                                        

    btnDecafPike.setToolTipText("Click this button to disable the middle button.");
}

1 个答案:

答案 0 :(得分:0)

为什么要尝试从psvm附加侦听器?为什么不在JFrame()? \如果你仍然必须在你的JFrame上创建一个getter,它返回jradiobutton,然后在其上添加监听器。但我不建议这样做。 我认为你需要这样的东西。

  Coffee_DarkRoast c = new Coffee_DarkRoast();
private void btnDecafPikeMouseEntered(java.awt.event.MouseEvent evt) {                                          

    c.setVisible(true);
}                                         

private void btnDecafPikeMouseExited(java.awt.event.MouseEvent evt) {                                           
    c.setVisible(false);
} 

您的代码正在创建一个要隐藏的新JFrame,因此旧屏幕仍保留在屏幕上。