【问题标题】:Which Swing Component to use?使用哪个 Swing 组件?
【发布时间】:2018-09-11 17:47:05
【问题描述】:

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

在我的 GUI 中,我有一个带有 JLabel 的 JTextField,上面写着“Radius”。现在我想在 JTextField 旁边放一个带有问号的图标,单击该问号可以详细解释 JLabel 的含义。例如,在这种情况下,它应该弹出一条消息,解释“要在图像上绘制的圆的半径”。当鼠标移动时,消息应该消失下面是我尝试实现的图形描述。

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

【问题讨论】:

  • 使用JLabel 并为其设置图标,不带任何文字也使用ToolTip作为您的文字
  • @Anchit 谢谢。你能指点我一个基本的实现吗?我不确定我是否完全遵循
  • Here 是 JLabel 的详细信息,Here 是 ToolTip 的详细信息。此外,如果您使用的是 IDE,这对您来说将非常容易。如果这有帮助,我会发布答案
  • @Anchit 谢谢。是的,发布答案也可能对其他人有所帮助,如果可以,请这样做
  • can I just place a question mark symbol? - 您可以使用 JOptionPane 中的问号图标。见Option Pane Features。然后您将显示一个 LAF 图标。您可以使用UIManager.getIcon("OptionPane.questionIcon") 获取图标。查看UIManager Defaults 了解您可能可以使用的其他系统图标。

标签: java swing jlabel jtextfield


【解决方案1】:

你可以很容易地做到这一点。您需要做的只是使用 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");

【讨论】:

  • 另一个问题:我可以不从电脑中选择图像,而是放置一个问号符号吗?这可能是一个愚蠢的问题,但我想知道如果在其他计算机上找不到图像,这个 GUI 会显示什么?
  • 您可以使用它,但根据 GUI,您需要增强它的视图。我更喜欢放置图像,因为它会简单快捷如果图像不在其他人的 PC 中,您应该将它放在项目目录中。那么每个人的 PC 都会很容易,并且永远不要将您的代码提供给任何人。生成 JAR 文件并在 PC 上运行它。它不需要任何额外的东西
【解决方案2】:

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

这基本上是工具提示(和弹出菜单)“幕后”使用的内容。这里的主要优点是您注意布局,并且(与标准工具提示相反)可以完全控制它何时出现和何时消失。因此,您可以显式在单击图标时创建弹出窗口,并在鼠标退出图标时显式隐藏它:

这是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;
                }
            }
        });

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2021-10-17
    • 2011-02-22
    相关资源
    最近更新 更多