Swing JDialog 中的自定义光标

发布于 2024-07-09 18:57:01 字数 723 浏览 6 评论 0原文

我有一个 Java Swing 应用程序,是使用 Java 1.5 在 Mac OS X 10.5 上开发的。

我试图在用户将鼠标移动到对话框中的某些文本上时显示自定义光标。 但光标永远不会改变。

当我不使用 JFrame 而不是 JDialog 时,光标确实会发生变化。 但随后我必须自己编写所有对话框代码。

如何让光标出现?

这是我可以创建来演示该问题的最简单的代码:

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

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JDialog dialog = pane.createDialog(null, "Test Dialog");
        dialog.setVisible(true);
    }
}

I have a Java Swing application, developed on Mac OS X 10.5 using Java 1.5.

I'm trying to make a custom cursor appear when the user moves the mouse over some text in a dialog. The cursor never changes, though.

When I don't use a JFrame instead of a JDialog, the cursor does change. But then I'll have to write all the dialog code myself.

How can I get the cursor to appear?

Here's the simplest code I could create to demonstrate the problem:

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

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JDialog dialog = pane.createDialog(null, "Test Dialog");
        dialog.setVisible(true);
    }
}

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

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

发布评论

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

评论(2

坦然微笑 2024-07-16 18:57:01

看起来这是 Java 1.5 中的一个错误:我首先尝试使用 Java 1.6.0_07,它按预期工作(在 Windows XP 上)。 然后我用Java 1.5.0_06重新编译,光标确实保持在默认状态。

了解 MacOS 上 Java 1.6 的困难后,我发现很难解决这个问题...

Bug ID: 5079694 JDialog 不尊重 setCursor
他们给出了一个解决方法...

[编辑]经过测试的解决方法:

public class CursorTest extends JFrame
{
  private CursorTest()
  {
  }

  private void ShowDialog()
  {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[] { "OK" } );

        JDialog dialog = pane.createDialog(this, "Test Dialog");
        dialog.setVisible(true);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        CursorTest testFrame = new CursorTest();
        testFrame.setTitle("Test GUI");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setSize(500, 300);
        testFrame.setVisible(true);
        testFrame.ShowDialog();
      }
    });
  }
}

与我的 JDK 和 JDK 配合良好。 系统。

Looks like it is a bug in Java 1.5: I first tried with Java 1.6.0_07 and it worked as expected (on Windows XP). Then I recompiled with Java 1.5.0_06 and indeed the cursor remains in default state.

Knowing the difficulties of Java 1.6 on MacOS, I see it will be hard to fix that...

Bug ID: 5079694 JDialog doesn't respect setCursor
They give a workaround...

[EDIT] Tested workaround:

public class CursorTest extends JFrame
{
  private CursorTest()
  {
  }

  private void ShowDialog()
  {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[] { "OK" } );

        JDialog dialog = pane.createDialog(this, "Test Dialog");
        dialog.setVisible(true);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        CursorTest testFrame = new CursorTest();
        testFrame.setTitle("Test GUI");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setSize(500, 300);
        testFrame.setVisible(true);
        testFrame.ShowDialog();
      }
    });
  }
}

Works fine with my JDK & system.

記柔刀 2024-07-16 18:57:01

感谢 PhiLho,Sun 的 bug 报告给了我解决方案。 所有者(父框架)必须为非空且正在显示。 作为记录,这里是我的示例代码的修改版本,确实显示手形光标。

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

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JFrame parent = new JFrame();
        parent.setVisible(true);
        JDialog dialog = pane.createDialog(parent, "Test Dialog");
        dialog.setModal(false);
        dialog.setVisible(true);
    }
}

Thanks PhiLho, that Sun bug report gave me the solution. The owner (parent frame) must be non-null and showing. For the record, here's a modified version of my example code that does show a hand cursor.

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

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JFrame parent = new JFrame();
        parent.setVisible(true);
        JDialog dialog = pane.createDialog(parent, "Test Dialog");
        dialog.setModal(false);
        dialog.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文