【问题标题】:Tooltip behind modal dialog模态对话框后面的工具提示
【发布时间】:2016-10-17 17:43:43
【问题描述】:

如何检查这些问题。当我检查桌面应用程序的菜单时,有些显示正确显示关闭按钮的工具提示,该按钮应始终位于前面。但有些显示在模态对话框的后面。

错误截图:

我和发帖的人有同样的问题:https://coderanch.com/t/460688/java/Glasspanes-tooltips

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


class GlassPaneContent extends JPanel {

    GlassPaneContent() {
        setSize(200, 50);
        ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
        JButton button = new JButton("A button");
        button.setToolTipText("A tooltip");
        add(button);

    }
}

class GlassPane extends JPanel {

    private static final Color BG_COLOR = new Color(0, 0, 0, 96);
    private GlassPaneContent content = new GlassPaneContent();

    public GlassPane() {
        setLayout(null);
        setOpaque(false);
        add(content);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(BG_COLOR);
        g.fillRect(0, 0, getWidth(), getHeight());
        int x = (getWidth() - content.getWidth()) / 2;
        int y = (getHeight() - content.getHeight()) / 2;
        content.setLocation(x, y);
        super.paintComponent(g);
    }
}

public class MainWindow extends JFrame {

    public MainWindow() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        GlassPane gp = new GlassPane();
        getRootPane().setGlassPane(gp);
        gp.setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow().setVisible(true);
    }
}

我们正在使用 JAVA Swing。如果我需要发布代码,请在下面发表评论。谢谢!

【问题讨论】:

  • 请提供SSCCE 和重现它的步骤。

标签: java swing tooltip


【解决方案1】:

试试这个:

public class MainWindow extends JFrame {

    public MainWindow() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        GlassPane gp = new GlassPane();
        setContentPane(gp);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow();
    }
}

【讨论】:

  • 它正在工作!太感谢了。我只是想问一下,因为我真的是 java swing 的新手。使用 setContentPane(gp) 和 getRootPane().setGlassPane(gp) 有什么区别?
  • 很高兴能帮上忙!问题在于,由于您使用的是setGlassPane(gp),因此您的 JPanel 实现作为 gui 之上的一个层工作。使用paintComponent 方法,您可以在玻璃窗格上绘制一个矩形(因此这部分将不再是透明的),使其位于工具提示上方并隐藏它。欲了解更多信息,请查看:docs.oracle.com/javase/tutorial/uiswing/components/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
相关资源
最近更新 更多