5

我正在实施 aMVC并且我想在我看来有 a JToolTipon a JLabel。我成功了,但现在我想设置解雇和初始计时器。

我阅读了JToolTipTollTipManager的 Javadoc 。我在 ToolTipManager 中找到了我想要的东西,但现在我不明白如何使用它。

JToolTip没有设置ToolTipManager并在他们说的文档中:

管理系统中的所有工具提示。

我真的不明白我应该如何使用ToolTipManager. 我的观点应该扩展ToolTipManager还是完全误导?

4

3 回答 3

11

ToolTipManager是单例。要使用它,您需要调用一个sharedInstance返回此类实例的静态方法。

ToolTipManager管理所有工具提示 - 它是一个全局配置类,对 的更改sharedInstance将应用​​于应用程序中的每个工具提示。

于 2012-06-21T07:18:06.787 回答
7

下面是一个展示如何使用 ToolTipManager 的示例:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

public class TestTooltips {

    protected static void initUI() {
        ToolTipManager.sharedInstance().setInitialDelay(0);
        ToolTipManager.sharedInstance().setDismissDelay(500);
        JFrame frame = new JFrame("test");
        JLabel label = new JLabel("Label text");
        label.setToolTipText("My cool nice and fun tooltip");
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}
于 2012-06-21T07:18:16.007 回答
5

不知道您尝试了什么以及您真正想要什么...,您是否通过以下代码成功

import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;

public class JScrollableToolTip extends JToolTip {

    private static final long serialVersionUID = 1L;
    private final MouseWheelListener mouseWheelListener;
    private final JTextArea tipArea;

    public JScrollableToolTip(final int rows, final int columns) {
        setLayout(new BorderLayout());
        mouseWheelListener = createMouseWheelListener();
        tipArea = new JTextArea(rows, columns);
        tipArea.setLineWrap(true);
        tipArea.setWrapStyleWord(true);
        tipArea.setEditable(false);
        LookAndFeel.installColorsAndFont(tipArea, "ToolTip.background",
                "ToolTip.foreground", "ToolTip.font");
        JScrollPane scrollpane = new JScrollPane(tipArea);
        scrollpane.setBorder(null);
        scrollpane.getViewport().setOpaque(false);
        add(scrollpane);
    }

    private MouseWheelListener createMouseWheelListener() {
        return new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(final MouseWheelEvent e) {
                JComponent component = getComponent();
                if (component != null) {
                    tipArea.dispatchEvent(new MouseWheelEvent(tipArea, e.getID(),
                            e.getWhen(), e.getModifiers(), 0, 0, e.getClickCount(),
                            e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(),
                            e.getWheelRotation()));
                }
            }
        };
    }

    @Override
    public void addNotify() {
        super.addNotify();
        JComponent component = getComponent();
        if (component != null) {
            component.addMouseWheelListener(mouseWheelListener);
        }
    }

    @Override
    public void removeNotify() {
        JComponent component = getComponent();
        if (component != null) {
            component.removeMouseWheelListener(mouseWheelListener);
        }
        super.removeNotify();
    }

    @Override
    public void setComponent(JComponent c) {
        JComponent component = getComponent();
        if (component != null) {
            component.removeMouseWheelListener(mouseWheelListener);
        }
        super.setComponent(c);
    }

    @Override
    public void setTipText(final String tipText) {
        String oldValue = this.tipArea.getText();
        tipArea.setText(tipText);
        tipArea.setCaretPosition(0);
        firePropertyChange("tiptext", oldValue, tipText);
    }

    @Override
    public Dimension getPreferredSize() {
        return getComponent(0).getPreferredSize();
    }

    @Override
    public String getTipText() {
        return tipArea == null ? "" : tipArea.getText();
    }

    @Override
    protected String paramString() {
        String tipTextString = (tipArea.getText() != null ? tipArea.getText() : "");
        return super.paramString() + ",tipText=" + tipTextString;
    }

    //for testing only:
    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JFrame f = new JFrame("JScrollableToolTip");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300, 200);
                f.setLocationRelativeTo(null);
                ToolTipManager.sharedInstance().setInitialDelay(500);
                ToolTipManager.sharedInstance().setDismissDelay(10000);
                ToolTipManager.sharedInstance().mousePressed(null);
                JTable table = new JTable(50, 4) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public JToolTip createToolTip() {
                        JScrollableToolTip tip = new JScrollableToolTip(3, 20);
                        tip.setComponent(this);
                        return tip;
                    }
                };
                table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                        setToolTipText("Row " + row + " Column " + column
                                + "\nUsed to display a 'Tip' for a Component. "
                                + "Typically components provide api to automate the process of "
                                + "using ToolTips. For example, any Swing component can use the "
                                + "JComponent  setToolTipText method to specify the text for a standard tooltip.");

                        return this;
                    }
                });
                f.add(new JScrollPane(table));
                f.setVisible(true);
            }
        });
    }
}
于 2012-06-21T07:24:35.830 回答