• <tfoot id='RLIJH'></tfoot>

    1. <small id='RLIJH'></small><noframes id='RLIJH'>

      <i id='RLIJH'><tr id='RLIJH'><dt id='RLIJH'><q id='RLIJH'><span id='RLIJH'><b id='RLIJH'><form id='RLIJH'><ins id='RLIJH'></ins><ul id='RLIJH'></ul><sub id='RLIJH'></sub></form><legend id='RLIJH'></legend><bdo id='RLIJH'><pre id='RLIJH'><center id='RLIJH'></center></pre></bdo></b><th id='RLIJH'></th></span></q></dt></tr></i><div id='RLIJH'><tfoot id='RLIJH'></tfoot><dl id='RLIJH'><fieldset id='RLIJH'></fieldset></dl></div>
      <legend id='RLIJH'><style id='RLIJH'><dir id='RLIJH'><q id='RLIJH'></q></dir></style></legend>

        • <bdo id='RLIJH'></bdo><ul id='RLIJH'></ul>

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

        时间:2023-10-14

            • <bdo id='nxOFh'></bdo><ul id='nxOFh'></ul>
                <tbody id='nxOFh'></tbody>

              <small id='nxOFh'></small><noframes id='nxOFh'>

                1. <legend id='nxOFh'><style id='nxOFh'><dir id='nxOFh'><q id='nxOFh'></q></dir></style></legend>
                2. <i id='nxOFh'><tr id='nxOFh'><dt id='nxOFh'><q id='nxOFh'><span id='nxOFh'><b id='nxOFh'><form id='nxOFh'><ins id='nxOFh'></ins><ul id='nxOFh'></ul><sub id='nxOFh'></sub></form><legend id='nxOFh'></legend><bdo id='nxOFh'><pre id='nxOFh'><center id='nxOFh'></center></pre></bdo></b><th id='nxOFh'></th></span></q></dt></tr></i><div id='nxOFh'><tfoot id='nxOFh'></tfoot><dl id='nxOFh'><fieldset id='nxOFh'></fieldset></dl></div>

                3. <tfoot id='nxOFh'></tfoot>
                  本文介绍了在模态 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 之外时光标不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <small id='SXtIV'></small><noframes id='SXtIV'>

                    • <bdo id='SXtIV'></bdo><ul id='SXtIV'></ul>

                        <tbody id='SXtIV'></tbody>
                      <i id='SXtIV'><tr id='SXtIV'><dt id='SXtIV'><q id='SXtIV'><span id='SXtIV'><b id='SXtIV'><form id='SXtIV'><ins id='SXtIV'></ins><ul id='SXtIV'></ul><sub id='SXtIV'></sub></form><legend id='SXtIV'></legend><bdo id='SXtIV'><pre id='SXtIV'><center id='SXtIV'></center></pre></bdo></b><th id='SXtIV'></th></span></q></dt></tr></i><div id='SXtIV'><tfoot id='SXtIV'></tfoot><dl id='SXtIV'><fieldset id='SXtIV'></fieldset></dl></div>
                      <legend id='SXtIV'><style id='SXtIV'><dir id='SXtIV'><q id='SXtIV'></q></dir></style></legend>

                        • <tfoot id='SXtIV'></tfoot>