質問

Java 1.5を使用してMac OS X 10.5で開発されたJava Swingアプリケーションがあります。

ユーザーがダイアログ内のテキストの上にマウスを移動すると、カスタムカーソルが表示されます。ただし、カーソルは変更されません。

JDialogの代わりにJFrameを使用しないと、カーソルが変わります。しかし、その後、すべてのダイアログコードを自分で記述する必要があります。

カーソルを表示するにはどうすればよいですか

問題を示すために作成できる最も簡単なコードは次のとおりです。

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);
    }
}
役に立ちましたか?

解決

それはJava 1.5のバグのように見えます。最初にJava 1.6.0_07を試したところ、(Windows XPで)期待どおりに機能しました。その後、Java 1.5.0_06で再コンパイルしましたが、実際にはカーソルはデフォルトの状態のままです。

MacOSでのJava 1.6の難しさを知っているので、それを修正するのは難しいでしょう...

バグ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およびシステム。

他のヒント

PhiLhoに感謝します。Sunのバグレポートで解決できました。所有者(親フレーム)は、null以外で表示する必要があります。記録のために、ハンドカーソルを表示する サンプルコードの修正版を次に示します。

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);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top