【发布时间】:2017-07-03 11:44:02
【问题描述】:
我正在学习 Java 中的 GUI,但我遇到了组件对齐问题。 我正在使用具有 3 个水平框的垂直框。 我想让拳头和第三个盒子在中间对齐,第二个在左边对齐。 这是代码。
public class KopjoFushen extends JFrame
{
private JTextField text;
private JTextField text2;
public KopjoFushen()
{
super("Kopjo fushen");
JLabel label=new JLabel();
label.setText("Fusha1");
text =new JTextField(10);
Box siper=Box.createHorizontalBox();
siper.add(label);
siper.add(text);
text2 =new JTextField(60);
text2.setEditable(false);
text2.setText("Fusha e pandryshueshme");
Box mes=Box.createHorizontalBox();
mes.add(text2);
JButton buton=new JButton("Kopjo fushen e lire");
buton.addActionListener(new ButonHandler());
Box poshte=Box.createHorizontalBox();
poshte.add(buton);
Box total=Box.createVerticalBox();
total.add(siper);
siper.setAlignmentX(Component.CENTER_ALIGNMENT);
mes.setAlignmentX(Component.RIGHT_ALIGNMENT);
total.setAlignmentX(Component.CENTER_ALIGNMENT);
total.add(mes);
total.add(poshte);
setLayout(new FlowLayout());
add(total);
}
第一个问题是第一个 Box 都向左对齐。 第二个问题是,如果我在第二个 JTextField 的构造函数中使用更大的数字,则第一个 JTextField 会变得更大。 这是我想要实现的目标 http://prntscr.com/e8utum 这就是我所做的:http://prntscr.com/e8uusn
【问题讨论】:
标签: java swing user-interface alignment box