swing:在 JDialog 上设置光标

发布于 2024-08-23 04:42:35 字数 229 浏览 7 评论 0原文

我在对话框中有一个 JPanel。 MouseListener 监听鼠标移动,当鼠标位于特定位置时,我在面板上调用 setCursor() 来更改光标。

这一切都运行良好,直到我从此对话框中打开另一个对话框并再次关闭它。 (例如:警告消息(JOptionPane),或新的定制 JDialog。执行此操作后,光标不会再次更改,尽管我仍然调用“setCursor”。

任何人都知道会发生什么?以及如何解决这个问题?

I have a JPanel inside a dialog. A MouseListener listens to mouse movements, and when the mouse is on a specific location, I call setCursor() on the panel to change the cursor.

This all works well, untill I open another dialog from within this dialog and close it again. (For example: a warning message (JOptionPane), or a new custommade JDialog. After this action, the cursor does not change again, although I still call 'setCursor'.

Anyone an idea what happens? And how to resolve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-08-30 04:42:36

我找到了解决方案:问题是我有 1 个框架和 1 个对话框。该框架是主框架,对话框是随后创建的。从对话框中,我调用 new JDialog(null, "title"); 我应该添加调用对话框,而不是使用 null,因为关闭对话框后,焦点转到主框架,虽然在我的 mac 上看起来焦点是在对话框上......

I found the solution: problem was I had 1 frame and 1 dialog. The frame is the main frame, the dialog is created afterwards. From the dialog, I call new JDialog(null, "title"); In stead of using null, I should have added the calling dialog, because after closing the dialog, the focus went to the main frame, although on my mac it looked like the focus was on the dialog...

GRAY°灰色天空 2024-08-30 04:42:35

我尝试了以下方法,并且在显示另一个 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);
    }
}

I tried the following and it worked fine, also after displaying another JDialog (on Windows, JDK 1.6.0_12).

Mouse cursor changes every 50 pixels in horizontal direction, clicking the JPanel opens a modal JDialog. Close it again and mouse cursor still changes.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文