[英]JLabel positioning into JPanel
I have this code written to make a database connection and add a client: 我编写了这个代码来建立数据库连接并添加一个客户端:
//adding the left panel
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(250, 500));
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
add(left);
//adding the right panel
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(250, 500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
add(right);
//adding the jlabel title to the left panel
JLabel leftTitle = new JLabel("Add a client");
leftTitle.setAlignmentX(CENTER_ALIGNMENT);
left.add(leftTitle);
//adding the jlabel title to the right panel
JLabel rightTitle = new JLabel("Make a reservation");
rightTitle.setAlignmentX(CENTER_ALIGNMENT);
right.add(rightTitle);
//adding the jlabel "name"
JLabel nameL = new JLabel("Name:");
left.add(nameL);
and I want to move this JLabel here: 我想在这里移动这个JLabel:
I've tried doing nameL.setAlignmentX(LEFT_ALIGNMENT);
我试过做
nameL.setAlignmentX(LEFT_ALIGNMENT);
but it's still not working 但它仍然无法正常工作
Your problem is that you've used a BoxLayout
. 你的问题是你使用过
BoxLayout
。
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
^^^^^^^^^
Your BoxLayout
is set to align things centered along the y-axis, so no amount of setting alignment is going to change that. 您的
BoxLayout
设置为对齐沿y轴居中的事物,因此任何设置对齐量都不会改变它。 In order to fix your problem, you need a different layout manager like GroupLayout
or CardLayout
. 为了解决您的问题,您需要一个不同的布局管理器,如
GroupLayout
或CardLayout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.