2017-02-15 87 views
0

我正在學習java中的GUI,並且遇到組件對齊問題。 我正在使用具有3個水平框的垂直框。 我希望拳頭和第三個盒子在中間對齊,第二個對齊在左邊。 這是代碼。組件對齊java

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); 
    } 

第一個問題是,第一個箱子都是左邊的。 第二個問題是,如果我在第二個JTextField的構造函數中使用更大的數字,則第一個JTextField會變大。 這裏是我想達到 http://prntscr.com/e8utum 什麼,這裏是我做了什麼:http://prntscr.com/e8uusn

回答

1

第一個盒子實際上是居中。它似乎與左側對齊,因爲它已調整大小以適合父面板。

事實上,如果你設置了JTextField MAXIMUMSIZE你會發現,它的中心

text.setMaximumSize(new Dimension(300, Integer.MAX_VALUE)); 
+0

,似乎這樣做。謝謝 –