在JLabel中以不同方式对齐文本和图标

时间:2013-03-31 15:50:47

标签: java swing alignment jlabel imageicon

我正在尝试创建一个左侧文本对齐且右侧图标对齐的JLabel,我尝试了这段代码:

_ip = new JLabel(ip);
_ip.setFont(boldFont);
_ip.setBounds(5, 0, 100, 50);
_ip.setIcon(images.ipBan);
_ip.setBorder(BorderFactory.createLineBorder(Color.black, 1));
_ip.setHorizontalTextPosition(SwingConstants.LEFT);
add(_ip);

这就是我得到的:

Alignment preview

红色图像显示实际图像对齐,灰色图像显示我想要图像的位置。

如果我添加

_ip.setAlignmentX(JLabel.RIGHT_ALIGNMENT);

什么都没发生,如果我添加

_ip.setHorizontalAlignment(JLabel.RIGHT);

图标右对齐,但文字右对齐,我希望它左对齐

有办法吗?

2 个答案:

答案 0 :(得分:6)

或者,您可以使用具有合适布局的JPanel,如图here所示。

image

答案 1 :(得分:5)

请DYM ???

enter image description here

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

public class CenteredJLabel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel("CenteredJLabel");
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");

    public CenteredJLabel() {
        panel.setLayout(new GridBagLayout());
        panel.add(label);
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setVerticalAlignment(SwingConstants.CENTER);
        label.setIcon(errorIcon);
        label.setIconTextGap(20);
        label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                CenteredJLabel centeredJLabel = new CenteredJLabel();
            }
        });
    }
}

修改

enter image description here

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

public class CenteredJLabel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JLabel labelOne = new JLabel("CenteredJLabel");
    private JLabel labelTwo = new JLabel("1.1.1.1");
    private JLabel labelThree = new JLabel("192.168.255.255");
    private JLabel labelFour = new JLabel("192.168.255.255");
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon questnIcon = UIManager.getIcon("OptionPane.questionIcon");
    private JPanel panelTwo = new JPanel();

    public CenteredJLabel() {
        panel.setLayout(new GridLayout(4, 0, 10, 10));

        labelOne.setHorizontalAlignment(SwingConstants.LEFT);
        labelOne.setVerticalAlignment(SwingConstants.CENTER);
        labelOne.setIcon(errorIcon);
        labelOne.setIconTextGap(20);
        labelOne.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelOne);

        labelTwo.setHorizontalAlignment(SwingConstants.LEFT);
        labelTwo.setVerticalAlignment(SwingConstants.CENTER);
        labelTwo.setIcon(infoIcon);
        labelTwo.setIconTextGap(20);
        labelTwo.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelTwo);

        labelThree.setHorizontalAlignment(SwingConstants.LEFT);
        labelThree.setVerticalAlignment(SwingConstants.CENTER);
        labelThree.setIcon(warnIcon);
        labelThree.setIconTextGap(20);
        labelThree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelThree);

        panelTwo.setLayout(new BorderLayout());
        panelTwo.setOpaque(false);
        panelTwo.add(labelFour, BorderLayout.WEST);
        panelTwo.add(new JLabel(questnIcon), BorderLayout.EAST);
        panel.add(panelTwo);

        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CenteredJLabel centeredJLabel = new CenteredJLabel();
            }
        });
    }
}