簡體   English   中英

將 Swing 組件作為內容的自定義 Java 工具提示不顯示

[英]Custom Java tool tip with Swing components as content does not show up

我試圖在組件的工具提示中顯示多個圖像,找到了createToolTip()並實現了一個自定義添加所需的組件,如下所示:

setComponent(component);

JPanel images = new JPanel(null);
images.setLayout(new BoxLayout(images, BoxLayout.X_AXIS));
for(ImageIcon icon:myIcons) {
    images.add(new JLabel(icon));
}

JPanel content = new JPanel(new BorderLayout());
content.add(new JLabel(title), BorderLayout.NORTH);
content.add(new JLabel(description));
content.add(images, BorderLayout.SOUTH);

add(content);

但是,我看到的只是一個小點,表明顯示了工具提示,但不知何故忽略了大小。 我想念實現自定義工具提示什么?

工具提示可以呈現 HTML。 如果您可以形成圖像的 URL(如果它們在內存中生成則不切實際,但通常可行),編寫一些 HTML 來加載圖像,並使用該 HTML 作為工具提示是一件容易的事情。


例如

多圖標工具提示

import javax.swing.*;

class MultiIconToolTip {

    public static void main(String[] args) throws Exception {
        final String html =
            "<html><body>" +
            "<img src='" +
            "http://i.stack.imgur.com/OVOg3.jpg" +
            "' width=160 height=120> " +
            "<img src='" +
            "http://i.stack.imgur.com/lxthA.jpg" +
            "' width=160 height=120>" +
            "<p>Look Ma, no hands!";
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JLabel hover = new JLabel("Point at me!");
                hover.setToolTipText(html);
                JOptionPane.showMessageDialog(null, hover);
            }
        });
    }
}

基本的“問題”是 JToolTip

  • is-not設計為容器,它只是偶然的容器,因為 JComponent 是。 對於 Swing “非容器”,它的 ui 委托有責任充當 LayoutManager。
  • 不夠豐富,它只能處理文本(至少對於緊急門 html,這是@Andrew 的最愛 :-)

繞過這些限制基本上是驅動小部件幾乎越過邊緣。 一個干凈的解決方案會滾動一個新組件。另一方面,OP 已經找到了要調整的螺絲。 唯一可以稍微改進的是既不調用 setXXSize,也不設置自定義 ui。 相反,通過覆蓋 getXXSize() 使其表現得像一個容器,例如:

@Override
public Dimension getPreferredSize() {
    if (getLayout() != null) {
        return getLayout().preferredLayoutSize(this);
    }
    return super.getPreferredSize();
}

我建議使用JWindow或 un_decorated JDialog作為彈出窗口(默認用於JCalendarJDatePicker )而不是JTooltip ,以便更好地輸出到 GUI 實現Translucent 和 Shaped Windows


注意:如果您使用 JDK 1.6 或更早版本,請改用此方法 它僅適用於 SUN JDK。

聽起來可能很傻,但是,您是否嘗試過為JPanel設置邊界?

setBounds(100, 100, 150, 50);

您可以嘗試在BorderLayout 設置組件之間的間隙

JPanel content = new JPanel(new BorderLayout(1,1));

基本上缺少兩件事。 首先, JToolTip擴展了JComponent ,與JPanel不同,它沒有默認布局。 要在工具提示上拉伸content ,請使用BorderLayout

setLayout(new BorderLayout());

第二個問題是尺寸。 ToolTipManager尊重工具提示的首選大小。 BorderLayout計算大小時, ToolTipUI忽略它。 因此,有兩種選擇:手動設置首選大小...

setPreferredSize(content.getPreferredSize());

請注意,這不會使布局過時; 否則,您會得到一個大小合適的空工具提示。

...或子類ToolTipUI以尊重布局,這就是我所采用的。 結果代碼是:

setComponent(StadtLabel.this);

JPanel images = new JPanel(null);
waren.setLayout(new BoxLayout(waren, BoxLayout.X_AXIS));
for(ImageIcon icon:myIcons) {
    JLabel lbl = new JLabel(icon);
}

JPanel content = new JPanel(new BorderLayout());
content.add(new JLabel(title), BorderLayout.NORTH);
content.add(new JLabel(description));
content.add(images, BorderLayout.SOUTH);

setLayout(new BorderLayout());
add(content);
setUI(new ToolTipUI() {
    @Override
    public Dimension getMinimumSize(JComponent c) {
        return c.getLayout().minimumLayoutSize(c);
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        return c.getLayout().preferredLayoutSize(c);
    }

    @Override
    public Dimension getMaximumSize(JComponent c) {
        return getPreferredSize(c);
    }
});

與其重新發明輪子,不如試試這個: https : //github.com/timmolderez/balloontip 您可以將任何內容作為 JComponent。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM