2010-02-22 32 views
0

我在對話框中有一個JPanel。 MouseListener監聽鼠標移動,當鼠標位於特定位置時,我在面板上調用setCursor()來更改光標。swing:在JDialog上設置光標

這一切運作良好,直到我打開此對話框中的另一個對話框,並再次關閉它。 (例如:一個警告消息(JOptionPane)或一個新的自定義JDialog,在這個動作之後,光標不會再次改變,儘管我仍然調用'setCursor'

任何想法會發生什麼?是什麼?

回答

0

我發現解決方案:問題是我有1幀和1對話框。框架是主框架,之後創建對話框。在對話框中,我打電話new JDialog(null, "title");而不是使用null,我應該添加調用對話框,因爲關閉對話框後,焦點轉到了主框架上,儘管在我的mac上它看起來像焦點在對話框中。 。

+0

總之:關閉一個對話框將使焦點返回到它的父項,如果沒有定義父項,它將返回到主框架,很高興知道。意味着輸入焦點,而不是鼠標焦點。 – FaithReaper 2017-02-24 12:33:09

2

我嘗試了以下它工作得很好,顯示另一JDialog(在Windows上,JDK 1.6.0_12)之後也。

,鼠標光標變爲在水平方向上的每50個像素,點擊JPanel打開一個模態JDialog。再次關閉並且鼠標光標仍然變化。

public class DialogCursorTest extends JDialog{ 
    public DialogCursorTest() { 
     final JPanel panel = new JPanel(); 
     panel.addMouseMotionListener(new MouseMotionAdapter() { 
      Cursor handCursor = new Cursor(Cursor.HAND_CURSOR); 
      @Override 
      public void mouseMoved(MouseEvent e) { 
       if(e.getX() % 100 > 50) { 
        if(panel.getCursor() != handCursor) { 
         panel.setCursor(handCursor); 
        } 
       } 
       else { 
        if(panel.getCursor() == handCursor) { 
         panel.setCursor(Cursor.getDefaultCursor()); 
        } 
       } 
      } 
     }); 

     panel.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       new JDialog(DialogCursorTest.this, "Test", true).setVisible(true); 
      } 
     }); 

     getContentPane().add(panel); 
    } 

    public static void main(String[] args) { 
     DialogCursorTest test = new DialogCursorTest(); 
     test.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     test.setSize(400, 300); 
     test.setVisible(true); 
    } 
} 
+0

對我也適用,我今天晚上會在我的Mac上測試它。並試圖找出有什麼區別:( – Fortega 2010-02-22 10:21:47