简体   繁体   English

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

[英]How to set text above and below a JButton icon?

I want to set text above and below a JButton 's icon. 我想在JButton的图标上方和下方设置文本。 At the moment, in order to achieve this, I override the layout manager and use three JLabel instances (ie 2 for text and 1 for the icon). 目前,为了实现这一点,我覆盖了布局管理器并使用了三个JLabel实例(即2个用于文本,1个用于图标)。 But this seems like a dirty solution. 但这似乎是一个肮脏的解决方案。

Is there a more direct way of doing this? 有更直接的方法吗?

Note - 注意 -
I'm not looking for a multi-line solution, I'm looking for a multi-label solution. 我不是在寻找多线解决方案,我正在寻找一种多标签解决方案。 Although this article refers to it as a multi-line solution, it actually seems to refer to a multi-label solution. 虽然条是指它作为一个多行的解决方案,它实际上似乎是指一个多标签解决方案。


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

}

在此输入图像描述

There is not way to split the text between the top/bottom of a JButton. 无法在JButton的顶部/底部之间拆分文本。 This would involve custom painting. 这将涉及定制绘画。

Since I'm not sure of your exact requirement I'll just through out a few random ideas: 由于我不确定您的具体要求,我将通过一些随机的想法:

  1. You can use a JButton with text & icon. 您可以使用带有文本和图标的JButton。 There are methods in the API that allow you to controal where text is positioned relative to the icon. API中有一些方法允许您控制文本相对于图标的位置。 Then you would need a second label for the other line of text. 那么你需要另一个标签用于另一行文本。 Basically the same as you current solution but you only need two labels. 基本上与您当前的解决方案相同,但您只需要两个标签。

  2. You could use the Text Icon and Compound Icon classes to create 1 Icon out of 3 separate Icons. 您可以使用文本图标复合图标类从3个单独的图标中创建1个图标。 Then you can just add the icon to a button. 然后,您只需将图标添加到按钮即可。

  3. Use a JTextPane. 使用JTextPane。 Its supports an insertIcon() method. 它支持insertIcon()方法。 So you could add a line of text, add the icon and then add the other line of text. 因此,您可以添加一行文本,添加图标,然后添加另一行文本。 You can play with the paragraph attributes of the text pane to align the text horizontally within the space if you don't want the text left justified. 如果您不希望文本左对齐,则可以使用文本窗格的段落属性来在文本窗格内水平对齐文本。 You can also play with the background color to make it look like a label. 您还可以使用背景颜色使其看起来像标签。

Example using the CompoundIcon: 使用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);
    }
}

you have two four options 你有两个选择

1) use JLabel + Icon + Html (<= Html 3.2) 1)使用JLabel + Icon + Html(<= Html 3.2)

2) use XxxButtonUI and override all required methods from API 2)使用XxxButtonUI并覆盖API中所有必需的方法

3) JLayeredPane with translucency???, another Layout with translucency, as JLabel or JComponent to the JButton, 3)具有半透明度的JLayeredPane ???,具有半透明性的另一个布局,作为JButton的JLabel或JComponent,

4) there are around lots of Graphics SW that can to prepare Background as *.jpg for Icon, then is very simple to change whatever by Event, Action or actual setting for JButton 4)有大量的Graphics SW可以为Icon准备背景为* .jpg,然后通过事件,动作或JButton的实际设置来更改

not correct way is looking for JLabel + Whatever instead of JButton, I think that is halfsized workaround 不正确的方法是寻找JLabel + Whatever而不是JButton,我认为这是半个化的解决方法

在这里找到另一个LayoutManagerhttp//download.oracle.com/javase/tutorial/uiswing/layout/visual.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM