JButton的:图标左对齐,文本右对齐 [英] JButton: Icon left-aligned, text right-aligned

查看:246
本文介绍了JButton的:图标左对齐,文本右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个的JButton 有一个图标,并在它的一些文字。我的问题是我想要的图标为左对齐,文本右对齐(这是没有必要为右对齐,但我不希望文本坚持的图标)。

I'm trying to create a JButton with 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).

不能够做到这一点我自己,我尝试了略有不同的解决方案。我使用了 iconTextGap 以创建图标和文字,有什么正常工作原则,但之间的一些空间,当我创建多个按钮,这都具有最宽的宽度,图标是不是在最左边了(与最长的文字按钮除外)。

Not being able to do this on my own, I tried a slightly different solution. I used the iconTextGap to 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).

我包括code证明:

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)?

推荐答案

看一看<一href="http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setHorizontalTextPosition%28int%29">JButton#setHorizontalTextPosition和<一href="http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setHorizontalAlignment%28int%29">JButton#setHorizontalAlignment

和正因为它可能会有所帮助<一个href="http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setVerticalTextPosition%28int%29">JButton#setVerticalTextPosition和<一href="http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setVerticalAlignment%28int%29">JButton#setVerticalAlignment

And just because it might be helpful JButton#setVerticalTextPosition and JButton#setVerticalAlignment

这篇关于JButton的:图标左对齐,文本右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆