2017-03-16 3 views
0

Netbeans의 JFrame에서만 드래그 앤 드롭을 사용했기 때문에 직접 코딩하지 않았습니다.NETBEANS : PSR에서 mouseListener를 추가 할 수 있도록 jRadioButton을 비 정적으로 만드는 방법은 무엇입니까?

나는 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."); 
} 
+0

아마도'btnDecafPike'가 어떤 클래스에서 정의되고 초기화되었을 것입니다. 어쩌면 당신은 그것을 수정해야 할 것입니다. 왜 마우스를 사용할 때 프레임을 보여주고 싶습니까? 툴팁이 더 유용할까요? – MadProgrammer

+0

죄송합니다. 툴팁이란 무엇입니까? –

+0

툴팁은 마우스가 컨트롤 위에 추가 컨텍스트를 제공 할 때 팝업되는 작은 팝업 창입니다. [툴팁 사용 방법] (https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip)을 참조하십시오. html) 자세한 내용은 – MadProgrammer

답변

0

왜 psvm에서 수신기를 연결하려고합니까? 왜 JFrame()에 없습니까? \ 여전히 jradiobutton을 반환하는 JFrame에 getter를 만든 다음 그 위에 수신기를 추가하십시오. 그러나 나는 그것을 할 것을 제안하지 않을 것이다. 나는 이런 식으로해야한다고 생각합니다.

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을 만들고 있으므로 화면에 이전 코드가 남아 있습니다.

관련 문제