0
我只用在NetBeans中的JFrame中的阻力和下降,所以我沒有在手代碼。NETBEANS:如何讓一個JRadioButton非靜態的,所以我可以在PSVM添加MouseListener的呢?
我有一個JRadioButton稱爲btnDecafPike,我想,當用戶將鼠標懸停它,一個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.");
}
推測,'btnDecafPike'已經在某個類中定義和初始化,也許你應該在其中進行修改。爲什麼要在使用鼠標懸停時顯示框架?工具提示會更有用嗎? – MadProgrammer
對不起。什麼是工具提示? –
工具提示是一個彈出式窗口,當鼠標在控件上逗留時提供額外的上下文,請參閱[如何使用工具提示](https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip。 html)更多詳情 – MadProgrammer