如何在 JButton 图标上方和下方设置文本?

2023-11-29

我想设置文字上面和下面 a JButton的图标。目前,为了实现这一目标,我重写布局管理器并使用三个JLabel实例(即 2 个用于文本,1 个用于图标)。但这似乎是一个肮脏的解决方案。

有没有更直接的方法来做到这一点?

Note -
我不是在寻找多线解决方案,我正在寻找一个多标签解决方案。虽然this文章将其称为多线解决方案,它实际上似乎指的是多标签解决方案。


EXAMPLE

import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class JButtonDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }


    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new JMultiLabelButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JMultiLabelButton extends JButton
    {
        private static final long serialVersionUID = 7650993517602360268L;

        public JMultiLabelButton()
        {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            add(new JCenterLabel("Top Label"));
            add(new JCenterLabel(UIManager.getIcon("OptionPane.informationIcon"))); 
            add(new JCenterLabel("Bottom Label"));
        }
    }

    private static final class JCenterLabel extends JLabel
    {
        private static final long serialVersionUID = 5502066664726732298L;

        public JCenterLabel(final String s)
        {
            super(s);
            setAlignmentX(Component.CENTER_ALIGNMENT);
        }

        public JCenterLabel(final Icon i)
        {
            super(i);
            setAlignmentX(Component.CENTER_ALIGNMENT);
        }
    }

}

enter image description here


无法在 JButton 的顶部/底部之间分割文本。这将涉及定制绘画。

由于我不确定您的确切要求,我将仅提出一些随机想法:

  1. 您可以使用带有文本和图标的 JButton。 API 中的一些方法允许您控制文本相对于图标的位置。然后您需要为另一行文本添加第二个标签。基本上与您当前的解决方案相同,但您只需要两个标签。

  2. 你可以使用文字图标 and 复合图标用于从 3 个独立图标中创建 1 个图标的类。然后您只需将图标添加到按钮即可。

  3. 使用 JTextPane。它支持 insertIcon() 方法。因此,您可以添加一行文本,添加图标,然后添加另一行文本。如果您不希望文本左对齐,则可以使用文本窗格的段落属性来在空间内水平对齐文本。您还可以使用背景颜色,使其看起来像标签。

使用CompoundIcon 的示例:

import java.awt.*;
import javax.swing.*;

public final class JButtonDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }


    private static void createAndShowGUI()
    {
        JButton button = new JButton();

        CompoundIcon icon = new CompoundIcon(CompoundIcon.Axis.Y_AXIS,
            new TextIcon(button, "Top Label"),
            UIManager.getIcon("OptionPane.informationIcon"),
            new TextIcon(button, "Bottom Label"));

        button.setIcon( icon );
        button.setFocusPainted( false );

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add( button );
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 JButton 图标上方和下方设置文本? 的相关文章

随机推荐