模态JDialog之外的光标不正确? [英] Incorrect cursor when outside of modal JDialog?

查看:106
本文介绍了模态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.

这里的问题在于模式 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,

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

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之外的光标不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆