Swing 关闭模式JComponent后setCursor()出现问题

Swing 关闭模式JComponent后setCursor()出现问题,swing,java-8,modal-dialog,ubuntu-16.04,jcomponent,Swing,Java 8,Modal Dialog,Ubuntu 16.04,Jcomponent,创建模式JComponent时,鼠标(正确地)将更改为默认光标。然而,当我关闭组件时,我遇到了一个奇怪的问题 如果在光标包含在边界中时关闭组件 对于组件,光标将正确还原 如果在光标超出边界时关闭组件(例如,使用esc),则即使调用setCursor(),光标也不会恢复 在这两种情况下,如何以非黑客方式正确地恢复光标 示例代码: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; class

创建模式JComponent时,鼠标(正确地)将更改为默认光标。然而,当我关闭组件时,我遇到了一个奇怪的问题

  • 如果在光标包含在边界中时关闭组件 对于组件,光标将正确还原
  • 如果在光标超出边界时关闭组件(例如,使用esc),则即使调用setCursor(),光标也不会恢复
在这两种情况下,如何以非黑客方式正确地恢复光标

示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

class ModalTest {
    public static void main (String[] args) {

        // Create a frame.
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setLocationRelativeTo(null);

        // Add a button.
        JButton button = new JButton();
        frame.add(button);
        frame.revalidate();
        frame.setVisible(true);

        frame.setCursor(Cursor.HAND_CURSOR);

        // While the mouse is inside the JFrame, the cursor should be Cursor.HAND_CURSOR.
        // And when the modal JOptionPane is visible, the cursor should be Cursor.DEFAULT_CURSOR.
        // However, when that modal JComponent is closed while the cursor is outside it, it does
        // not revert to Cursor.HAND_CURSOR.
        button.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                JOptionPane.showConfirmDialog (
                    frame,
                    "Move the cursor inside, then outside of this component and press Esc. The cursor will stay default.",
                    "Cursor Test",
                    JOptionPane.YES_NO_OPTION
                );
                // Even if you change the cursor after, it stays default.
                frame.setCursor(Cursor.CROSSHAIR_CURSOR);
            }
        });
    }
}

使用setCursor的最佳方法是在MouseEntered和MouseExited中的MouseListener中:“当此组件的contains方法对于当前光标位置返回true时,将显示此光标图像,并且此组件可见、可显示和已启用。”读起来像是当光标超出边界时图像不应该更改。什么版本的Java?当我按escape时,光标将恢复为十字光标。(不是手,因为那时你的其他设置已经生效了)。我把关于十字准星的最后一行注释掉了。当我运行它时,我有一个手动光标。我点击,模态弹出。光标将变为箭头(默认)。我将光标移到它上面,然后移离它(或者移回到JFrame上,或者甚至移到我的桌面上。我按Esc键。模式关闭。光标变回手。(如果我将光标移离JFrame,移到桌面上,我必须将其移回JFrame上才能看到手。)我在Windows 10上使用JDK 1.8.0µ。使用setCursor的最佳方法是在MouseEntered和MouseExited中的MouseListener中:“当此组件的contains方法对于当前光标位置返回true时,将显示此光标图像,并且此组件可见、可显示和已启用。”当光标超出边界时,图像不应更改。什么版本的Java?当我按escape时,光标将恢复为十字光标。(不是手动,因为此时您的其他设置已生效)。Java 8(我与Aaron一起工作)是的。我注释掉了关于十字光标的最后一行。当我运行它时,我有一个手动光标。我单击,模式弹出。光标变为箭头(默认)。我将光标移到它上面,然后再移离它(回到JFrame,甚至桌面上。我按Esc。模式关闭。光标变回手动。(如果我将光标一直从JFrame移到桌面上,我必须将光标移回到JFrame上才能看到手。)我在Windows10上使用的是JDK1.8.0µ。