[英]swing: setting cursor on JDialog
我在對話框中有一個JPanel。 MouseListener偵聽鼠標的運動,當鼠標位於特定位置時,我在面板上調用setCursor()來更改光標。
一切正常,直到我從該對話框中打開另一個對話框,然后再次將其關閉。 (例如:警告消息(JOptionPane)或新定制的JDialog。執行此操作后,盡管我仍然稱呼'setCursor',但光標不會再次更改。
有人知道會發生什么嗎? 以及如何解決呢?
我嘗試了以下操作,並且在顯示另一個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);
}
}
我找到了解決方案:問題是我有1幀和1個對話框。 該框架是主框架,隨后將創建對話框。 從對話框中,我調用new JDialog(null, "title");
除了應該使用null之外,我還應該添加調用對話框,因為關閉對話框后,焦點移到了主框架上,盡管在我的Mac上看起來好像焦點在對話框上了...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.