2013-05-02 34 views
0
public class Grouplayout implements Runnable { 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Grouplayout()); 
} 

@Override 
public void run() { 
    JFrame jFrame = new JFrame(); 
    GroupLayout layout = new GroupLayout(jFrame.getContentPane()); 
    jFrame.getContentPane().setLayout(layout); 
    layout.setAutoCreateGaps(true); 
    layout.setAutoCreateContainerGaps(true); 

    JLabel jLabel1 = new JLabel("a"); 
    JLabel jLabel2 = new JLabel("b"); 
    JLabel jLabel3 = new JLabel("c"); 
    JLabel jLabel4 = new JLabel("d"); 

    layout.setVerticalGroup(
     layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel1) 
       .addComponent(jLabel2)) 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel3) 
       .addComponent(jLabel4))); 

    jFrame.pack(); 
    jFrame.setVisible(true); 
} 
} 

我試圖運行它,但我有以下異常:在線程 「AWT-EventQueue的 - 0」 java.lang.IllegalStateException的Java GroupLayout的異常

例外:javax.swing中.JLabel [,0,0,0x0,無效,alignmentX = 0.0,alignmentY = 0.0,邊界=,旗幟= 8388608,MAXIMUMSIZE =,=的minimumSize,首選大小=,=的DefaultIcon,disabledIcon =,=的Horizo​​ntalAlignment LEADING,horizo​​ntalTextPosition = TRAILING, iconTextGap = 4,labelFor =,text = a,verticalAlignment = CENTER,verticalTextPosition = CENTER]未附加到水平組

有什麼問題?我該如何解決它?

回答

3

你必須同時指定水平和垂直佈局,也看到GroupLayout giving error with java swing

我建議你使用一個工具來幫助您建立GUI。

+0

我一直在投票,直到我看到你的編輯,並解決了它。輝煌的最後一句話。 :) – 2013-05-02 20:57:01