2014-10-19 104 views
0

我正在嘗試創建GroupLayout,但每次編譯時都會收到錯誤消息。我在代碼中找不到錯誤的行,在我看來是正確的。爲什麼我會遇到這些錯誤?GroupLayout錯誤:在我的代碼中找不到錯誤的行

構造函數中的代碼(擴展的JFrame):

mem_settings = new JPanel(); 

    lbl_mem_settings = new JLabel("Memory settings"); 
    lbl_mem_select = new JLabel("Type"); 
    cb_mem_select = new JComboBox(new String[] {"24C01","24C02","24C04"}); 


    // Layout 
    GroupLayout GL = new GroupLayout(mem_settings); 
    mem_settings.setLayout(GL); 
    GL.setAutoCreateGaps(true); 
    GL.setAutoCreateContainerGaps(true); 

    GL.setHorizontalGroup(GL.createSequentialGroup() 
     .addGroup(GL.createParallelGroup(LEADING) 
      .addComponent(lbl_mem_settings) 
      .addComponent(lbl_mem_select)) 
     .addGroup(GL.createParallelGroup(LEADING) 
      .addComponent(cb_mem_select)) 
    ); 
    GL.setVerticalGroup(GL.createSequentialGroup() 
     .addGroup(GL.createParallelGroup(BASELINE) 
      .addComponent(lbl_mem_settings)) 
     .addGroup(GL.createParallelGroup(BASELINE) 
      .addComponent(lbl_mem_settings) 
      .addComponent(cb_mem_select)) 
    ); 

    add(mem_settings); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    pack(); 
    setVisible(true); 

和錯誤,我得到:

run: 
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Type,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a vertical group 
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090) 
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040) 
    at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878) 
    at java.awt.Container.preferredSize(Container.java:1794) 
    at java.awt.Container.getPreferredSize(Container.java:1778) 
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661) 
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719) 
    at java.awt.Container.preferredSize(Container.java:1794) 
    at java.awt.Container.getPreferredSize(Container.java:1778) 
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661) 
    at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:920) 
    at java.awt.Container.preferredSize(Container.java:1794) 
    at java.awt.Container.getPreferredSize(Container.java:1778) 
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661) 
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719) 
    at java.awt.Container.preferredSize(Container.java:1794) 
    at java.awt.Container.getPreferredSize(Container.java:1778) 
    at java.awt.Window.pack(Window.java:809) 
    at memcryp.GUI.<init>(GUI.java:46) 
    at memcryp.MEMCRYP.main(MEMCRYP.java:20) 
BUILD STOPPED (total time: 6 minutes 39 seconds) 
+0

「*我在代碼中找不到錯誤的行*」,根據您的錯誤按摩問題在memcryp.GUI類的第46行。它應該是memcryp.MEMCRYP類的'main'方法中第20行調用的構造函數的一部分。 – Pshemo 2014-10-19 16:45:32

回答

1

該錯誤信息是明確的,但。你JLabel稱爲lbl_mem_select僅連接到一個水平組:

GL.setHorizontalGroup(GL.createSequentialGroup() 
     .addGroup(GL.createParallelGroup(LEADING) 
      .addComponent(lbl_mem_settings) 
      .addComponent(lbl_mem_select)) //here you attach the label 
     .addGroup(GL.createParallelGroup(LEADING) 
      .addComponent(cb_mem_select)) 
    ); 

然而,在垂直組你不包括標籤。更改垂直組的代碼爲

GL.setVerticalGroup(GL.createSequentialGroup() 
     .addGroup(GL.createParallelGroup(BASELINE) 
      .addComponent(lbl_mem_settings) 
      .addComponent(lbl_mem_select)) //add this line 
     .addGroup(GL.createParallelGroup(BASELINE) 
      .addComponent(lbl_mem_settings) 
      .addComponent(cb_mem_select)) 
    ); 

應該這樣做。

這就是說......你真的想用GroupLayout手動編寫一個用戶界面。從GroupLayout教程

GroupLayout is a layout manager that was developed for GUI builders such as Matisse, the GUI builder provided with the NetBeans IDE

雖然可以手工編寫佈局與它的報價,它需要相當多的(不可讀恕我直言)代碼來做到這一點。還有其他(第三方)佈局管理器更適合手寫佈局。

+0

謝謝!改變它後,它的作品。 – Jojojoppe 2014-10-19 16:51:24

+2

如果幫助解決問題,請[接受答案](http://meta.stackexchange.com/a/5235/155831)。 – 2014-10-19 21:28:41