在模态 JDialog 之外时光标不正确?

Incorrect cursor when outside of modal JDialog?(在模态 JDialog 之外时光标不正确?)

本文介绍了在模态 JDialog 之外时光标不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 setCursor() 方法来改变组件使用的光标时,所有组件都可以正常工作,包括 JFrameJDialog.

When using the method setCursor(), to change the cursor used by a component, everything works fine for all components, including JFrame and JDialog.

这里的问题在于 modal JDialog.当鼠标在对话框时,光标显示在右侧.但是,当鼠标移到对话框之外时,光标会重新设置为操作系统默认值,即使底层 JFrame 使用与对话框相同的自定义光标.

The problem here resides with a modal JDialog. When the mouse is inside the dialog, the cursor is displayed right. But, when the mouse is moved outside the dialog, the cursor is re-set to the OS default, even though the underlying JFrame is using the same custom cursor as the dialog.

我搜索了很多,找到了一些相关的问题,但没有一个正确答案.

I've searched A LOT, and found some related questions, but none has a correct answer to this.

我使用的是 Windows 10;JDK 1.8.0_40.

I'm using Windows 10; JDK 1.8.0_40.

SSCCE:

package br.shura.knockback;

import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.*;

public class DialogCursorSSCCE extends JFrame {
  public DialogCursorSSCCE() {
    Cursor cursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
    JButton button = new JButton("Click me to open dialog.");

    button.addActionListener(event -> {
      JDialog dialog = new JDialog();
      JLabel label = new JLabel("Move the mouse outside this dialog.");
      int width = label.getFontMetrics(label.getFont()).stringWidth(label.getText());

      label.setPreferredSize(new Dimension(width + 10, 50));
      dialog.add(label);
      dialog.pack();
      dialog.setCursor(cursor);
      dialog.setLocationRelativeTo(button);
      dialog.setModal(true);
      dialog.setTitle("Dialog");
      dialog.setVisible(true);
    });
    button.setAlignmentX(CENTER_ALIGNMENT);
    button.setMaximumSize(new Dimension(400, 100));
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    add(button);
    setCursor(cursor);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setExtendedState(MAXIMIZED_BOTH);
    setTitle("DialogCursorSSCCE");
  }

  public static void main(String[] args) {
    new DialogCursorSSCCE().setVisible(true);
  }
}

推荐答案

但是,当鼠标移出对话框时,光标会重新设置为操作系统默认值,

But, when the mouse is moved outside the dialog, the cursor is re-set to the OS default,

我对正在发生的事情的猜测是对话框的边框是操作系统对等组件.因此,当您离开 Swing 组件时,会为操作系统对等体生成鼠标悬停事件,因此光标设置为操作系统默认值.

My guess at to what is happening is that the border of a dialog is an OS peer component. So when you leave the Swing component the mouse over event is generated for the OS peer so the cursor is set to the OS default.

当您完全离开对话框时,框架不会收到任何事件,因为对话框是模态的,因此系统光标在框架上时仍会显示.

When you leave the dialog completely the frame doesn't receive any events since the dialog is modal so the system cursor is still displayed while over the frame.

注意光标进入标题栏时的变化.

Note how the cursor changes when it enters the title bar.

现在尝试以下操作:

JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

现在光标只有在到达边框时才会改变,因为 Swing 正在绘制标题栏.

Now the cursor will only change when it reaches the border because Swing is painting the title bar.

我也试过了:

  JDialog dialog = new JDialog();
  dialog.setUndecorated(true);

所以没有装饰,但是光标离开窗口时仍然会改变.

So there are no decorations but the cursor still changes when it leaves the window.

我不知道有什么办法.

这篇关于在模态 JDialog 之外时光标不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在模态 JDialog 之外时光标不正确?

基础教程推荐