我正在使用具有自定义形状的应用程序,但按钮的工具提示存在一些问题。我在一个简单的示例中隔离了该问题,该示例准确地说明了我的情况。

您可以看到,中间按钮的工具提示可以很好地显示,因为它比根面板大,但是左侧按钮上的那个不起作用,并且被我的自定义形状隐藏了。

这是我的例子:

import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;

public class ButtonDemo extends JPanel {
protected JButton b1, b2, b3;

public ButtonDemo() {
    b1 = new JButton("Disable middle button");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.LEADING);

    b2 = new JButton("Middle button");
    b2.setVerticalTextPosition(AbstractButton.BOTTOM);
    b2.setHorizontalTextPosition(AbstractButton.CENTER);

    b3 = new JButton("Enable middle button");
    b3.setEnabled(false);

    b1.setToolTipText("Click this button to disable the middle button.");
    b2.setToolTipText("This middle button does nothing when you click it. This middle button does nothing when you click it. This middle button does nothing when you click it.");
    b3.setToolTipText("Click this button to enable the middle button.");

    add(b1);
    add(b2);
    add(b3);
}

private static void createAndShowGUI() {

    JFrame frame = new JFrame("ButtonDemo");

    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    ButtonDemo newContentPane = new ButtonDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setSize(1024, 768);
    frame.setVisible(true);
    frame.setLocation(0, 0);

    // Shape
    final Point[] points = new Point[]{
            //
            new Point(0, 0),
            //
            new Point(0, frame.getHeight()),
            //
            new Point(frame.getWidth() - 400, frame.getHeight()),
            //
            new Point(frame.getWidth() - 400, 25),
            //
            new Point(frame.getWidth(), 25),
            //
            new Point(frame.getWidth(), 0),
            //
            new Point(0, 0)};

    int[] xpoints = new int[points.length];
    int[] ypoints = new int[points.length];

    for (int i = 0; i < points.length; i++) {
        xpoints[i] = (int) points[i].getX();
        ypoints[i] = (int) points[i].getY();
    }

    Shape formeFenetre = new Polygon(xpoints, ypoints, points.length);

    frame.setShape(formeFenetre);

}
public static void main(final String[] args) {

    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}
}

我以为“ToolTipManager.sharedInstance()。setLightWeightPopupEnabled(false);”正是我在寻找的东西,但是与我的JRE配合得不太好。顺便说一句,我正在使用Java 1.7.0_09,但应该可以与1.7中的任何JRE一起使用。

希望有人知道该怎么做!谢谢阅读。

最佳答案

该问题可能与以下方面有关:

  • Bug ID: 2224554 Version 7 doesn't support translucent popup menus against a translucent window
  • Bug ID: 7156657 Version 7 doesn't support translucent popup menus against a translucent window
  • jdk8/jdk8/jdk: changeset 5453:4acd0211f48b
  • [8] Review request for 7156657 Version 7 doesn't support translucent popup menus against a translucent window
  • 07-24 20:15