展开全部
设置62616964757a686964616fe59b9ee7ad9431333363386662JLabel内容的对齐方式有2种:
方案一: 在创建时,设置好对齐方式JLabel jl1 = new JLabel(“右边”,JLabel.RIGHT);
方案二: 创建完成后, 修改对齐方式JLabel jl1 = new JLabel(“右边”);
jl1.setHorizontalAlignment(JLabel.RIGHT);
效果图
参考代码import java.awt.*;
import javax.swing.*;
public class TextFrame extends JFrame {
public TextFrame() {
JLabel jl1 = new JLabel(“右边”);
jl1.setHorizontalAlignment(JLabel.RIGHT);
JLabel jl2 = new JLabel(“中央”);
jl2.setHorizontalAlignment(JLabel.CENTER);
JLabel jl3 = new JLabel(“左边”);
jl3.setHorizontalAlignment(JLabel.LEFT);
add(jl1);add(jl2);add(jl3);
setLayout(new GridLayout(3, 1));
setTitle(“标题”);
setSize(200, 200);
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextFrame().setVisible(true);
}
}