JLable文字和图片排列

在JLable中加入文字和图片,如何实现在水平方向,一个左对齐,一个右对齐呢?

废话少说,见代码:
[code="java"]
package org.zergle.test;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestJLabelFrame extends JFrame {
XLabel lbl = new XLabel("内容");

public TestJLabelFrame() {
    super();
    JPanel container = (JPanel) this.getContentPane();
    lbl.setIcon(new ImageIcon(this.getClass().getResource("icon.gif")));
    container.add(lbl, BorderLayout.CENTER);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(640, 480);
}

public static void main(String[] args) {
    new TestJLabelFrame().setVisible(true);
}

}

class XLabel extends JLabel {

public XLabel() {}

public XLabel(String text) {
    super(text);
}

public void paint(Graphics g) {
    String text = this.getText();
    Icon icon = this.getIcon();
    Rectangle2D bounds = this.getBounds();
    FontMetrics fm = this.getFontMetrics(new Font("宋体", Font.PLAIN, 12));
    Rectangle2D rect = fm.getStringBounds(text, 0, text.length(), g);
    int y = (int) (bounds.getHeight() - rect.getHeight()) / 2;
    g.drawString(text, 0, y);
    if (icon != null) {
        int x = (int) bounds.getWidth() - icon.getIconWidth();
        y = (int) (bounds.getHeight() - icon.getIconHeight()) / 2;
        icon.paintIcon(this, g, x, y);
    }
    g.dispose();
}

}
[/code]

哈哈,准备接分  :wink:

setHorizontalAlignment(int alignment)

icon.gif 图片放在和源代码同一个目录下哦