java JButton:图标左对齐,文本右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13027223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:13:14  来源:igfitidea点击:

JButton: Icon left-aligned, text right-aligned

javaswingiconsalignmentjbutton

提问by 1r0n1k

I'm trying to create a JButtonwith an icon and some text in it. My problem is that I want the icon to be left-aligned and the text right-aligned (It isn't necessary to be right-aligned, but I don't want the text to stick to the icon).

我正在尝试创建一个JButton带有图标和一些文本的图标。我的问题是我希望图标左对齐,文本右对齐(不需要右对齐,但我不希望文本粘在图标上)。

Not being able to do this on my own, I tried a slightly different solution. I used the iconTextGapto create some space between the icon and the text, what worked fine in principle, but when I create multiple buttons, which all have the width of the widest, the icon isn't at the very left anymore (except in the button with the longest text).

由于我自己无法做到这一点,我尝试了一个稍微不同的解决方案。我使用iconTextGap来在图标和文本之间创建一些空间,原则上工作正常,但是当我创建多个按钮时,它们的宽度都是最宽的,图标不再位于最左边(除了最长文本的按钮)。

I included a code to demonstrate that:

我包含了一个代码来证明:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.net.MalformedURLException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class Main{

 private JFrame frame;
 private JPanel buttonPanel;
 private GridBagConstraints constraints;

 public Main() throws MalformedURLException{

    frame = new JFrame();

    buttonPanel = new JPanel();
    frame.add(buttonPanel);

    buttonPanel.setLayout(new GridBagLayout());
    constraints = new GridBagConstraints();

    constraints.insets = new Insets(5, 5, 3, 5);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.gridx = 0;
    constraints.gridy = 0;


    String[] text = { "some Text", "this text is longer" };

    for (int i = 0; i < text.length; i++) {
        JButton button= new JButton(text[i], new ImageIcon(new File("icon.png").toURI().toURL()));
        button.setAlignmentX(SwingConstants.WEST);
        button.setIconTextGap(30);
        button.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));


        buttonPanel.add(button, constraints);
        constraints.gridy++;
    }

    frame.pack();
    frame.setVisible(true);


}

 public static void main(String[] args){
    try {
        new Main();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

}

Does anyone know of a way to have the icon at the left end and have some space between the icon and the text (or the text right-aligned)?

有没有人知道在左端放置图标并在图标和文本(或右对齐的文本)之间留出一些空间的方法?

回答by Roman C

Classic solution to this problem to use additional panel that has a layout that alows to align components such as BorderLayout. Then put the button and label inside it with the corresponding layout alignment. Pack or validate the frame.

这个问题的经典解决方案是使用额外的面板,该面板的布局允许对齐组件,例如BorderLayout. 然后把按钮和标签放在里面,对应的布局对齐。打包或验证框架。

回答by texelcode

Try to set margin to smaller then default values in button properties, and then set iconTextGap to have it rightmost horizontal position

尝试将边距设置为小于按钮属性中的默认值,然后将 iconTextGap 设置为最右侧的水平位置