java怎么把输出的词居中?

java怎么把输出的词居中?,第1张

jLabel4setHorizontalAlignment(SwingConstantsCENTER);试试这个看看
不行就试这个jLabel4setBounds(new Rectangle(180, 6, 80, 50));里面的参数是要根据你的外框大小进行调整的,参数(距离最上距离,距离最左距离,这个Rectangle的宽,这个Rectangle的高)

增加两句话,我用粗体标出了:
public panelcenter() {
thissetLayout(new BorderLayout());
JLabel j1 = new JLabel();
j1setHorizontalAlignment(SwingConstantsCENTER);
j1setText("第一个swing程序");
jpsetLayout(new BorderLayout());
jpadd(j1, BorderLayoutCENTER);
thisadd(jp);
thissetSize(500, 300);
thissetLocationRelativeTo(null);
thissetVisible(true);
}

如果只是要JLabel而不要其他东西跟在label后面的话,那就把各个label放在box里面,如下:
Box box1 = BoxcreateHorizontalBox();
box1add(new JLabel("Input (odt file)", JLabelCENTER));
Box box2 = BoxcreateHorizontalBox();
box2add(new JLabel("Input (odt file)", JLabelCENTER));
Box boxH = BoxcreateVerticalBox();
boxHadd(box1);
boxHadd(box2);
boxHadd(BoxcreateVerticalGlue());
这样就能对齐了,设定JLabel位置的时候就改变 “JLabelCENTER”就行了
刚好在做一个需要这样的作业

1、LEFT、RIGHT、CENTER、LEADING 、TRAILING等参数表示label中内容对齐方式:左对齐、右对齐、居中、靠顶对齐、靠下对齐\x0d\2、可以。 有构造函数 \x0d\JLabel(Icon image) \x0d\JLabel(Icon image,int horizontalAlignment)\x0d\JLabel(String text,Icon image,int horizontalAlignment) \x0d\参数说明:\x0d\String text:标签中显示文字\x0d\ \x0d\Icon image: 对象,需要使用ImageIcon子对象创建, Icon image=new ImageIcon(String filename); filename:本地路径\x0d\ \x0d\horizontalAlignment:对齐方式,就是1中的那些常量

@param x the new <i>x</i>-coordinate of this component 相对左上角x轴的值
@param y the new <i>y</i>-coordinate of this component 相对左上角y轴的值
@param width the new <code>width</code> of this component 宽度
@param height the new <code>height</code> of this component 高度


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13377346.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存