使用哪个 Swing 组件?

Which Swing Component to use?

我是 Java 和 GUI 编程的新手。我有以下问题。

在我的 GUI 中,我有一个带有 JLabel 的 JTextField,它显示为 "Radius"。现在我想在 JTextField 旁边放一个带有问号的图标,单击它会详细说明 JLabel 的含义。例如,在这种情况下,它应该弹出一条消息解释 "The radius of the circle to be drawn on the image"。当鼠标移动时,该消息应该消失下面是我正在尝试实现的图形描述。

我的问题很基础。我想知道我可以使用哪个 Swing 组件来实现它?我试着在网上查找它,但我不知道要查找哪个组件。任何帮助和建议将不胜感激。

你可以很容易地做到这一点。您需要做的就是使用 JLabel 并且不在其上放置任何文本。而是在上面放一张图片

此代码让您将图像设置为 JLabel

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class AddingIconJLabel {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setTitle("JLabel Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ImageIcon imageIcon = new ImageIcon("yourFile.gif");
    JLabel label = new JLabel(imageIcon);

    frame.add(label);
    frame.pack();
    frame.setVisible(true);
  }
}

其次,将 ToolTip 放在 JLabel 上,使您的文本在您移到图像上时出现

这是有用的代码提示

JLabel label = new JLabel("Username");
label.setToolTipText("Enter your username");

我想知道为什么没有人建议为此使用 Popup

这基本上是 "under the hood" 工具提示(和弹出菜单)所使用的。这里的主要优点是您 而不是 关心布局,并且(与标准工具提示相比)可以完全控制它何时出现和何时消失。所以你可以显式在点击图标时创建弹窗,当鼠标离开图标时显式隐藏它:

这是代码 MCVE :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class PopupExample
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel(new BorderLayout());
        p.add(new JLabel("Radius:"), BorderLayout.WEST);
        p.add(new JTextField(10), BorderLayout.CENTER);

        Icon icon = UIManager.getIcon("OptionPane.questionIcon");
        JLabel label = new JLabel(icon);

        addHelpPopup(label, "<html>"
            + "The help text. You can (but do <br>"
            + "not have to) use <i>HTML</i> here for <br>"
            + "<u>formatting</u>"
            + "</html>");

        p.add(label, BorderLayout.EAST);

        f.getContentPane().setLayout(new FlowLayout());
        f.getContentPane().add(p);

        f.add(label);
        f.setSize(400, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }


    private static void addHelpPopup(Component component, String text)
    {
        component.addMouseListener(new MouseAdapter()
        {
            private Popup popup;

            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (popup != null)
                {
                    popup.hide();
                    popup = null;
                }
                PopupFactory popupFactory = PopupFactory.getSharedInstance();
                JLabel label = new JLabel(text);
                label.setOpaque(true);
                label.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(Color.BLACK),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)));
                Dimension dim = label.getPreferredSize();
                Point p = e.getLocationOnScreen();
                popup = popupFactory.getPopup(
                    component, label, p.x, p.y - dim.height);
                popup.show();
            }
            @Override
            public void mouseExited(MouseEvent e)
            {
                if (popup != null)
                {
                    popup.hide();
                    popup = null;
                }
            }
        });

    }

}