2015-07-11 23 views
0

我正在研究Java Media Player的GUI代碼。當我嘗試運行的主類,我不斷收到以下錯誤信息:線程「main」中的異常java.lang.IllegalArgumentException:非法組件位置

Exception in thread "main" java.lang.IllegalArgumentException: illegalcomponent position 
     at java.awt.Container.addImpl(Container.java:1085) 
     at java.awt.Container.add(Container.java:465) 
     at dj2.gui.ArtistsPane.<init>(ArtistsPane.java:19) 
     at dj2.gui.MainFrame.<init>(MainFrame.java:36) 
     at dj2.test.GUITest.main(GUITest.java:21) 

我認爲問題來自我ArtistsPane類,因爲其他兩個錯誤是參考它。

這裏是我使用ArtistsPane代碼:

public class ArtistsPane extends JPanel{ 

    public ArtistsPane(){ 

     this.setLayout(new FlowLayout()); 
     add(new TracksAddRemoveToolBar(),FlowLayout.TRAILING);}} 

的問題是在add方法的水平來檢測的。 它有什麼問題? 謝謝!

+0

1)爲了更好地幫助越早,張貼[MCVE(http://stackoverflow.com/help/mcve)(最小完備可驗證例子)或[SSCCE](http://www.sscce.org/)(簡短的,獨立的,正確的例子)。 2)以最小尺寸提供ASCII藝術或簡單的GUI圖形*圖形,並且如果可調整大小,則具有更大的寬度和高度。 –

+0

@AdrewThompson看起來我不能發佈圖片,我需要10個聲望:任何方式來繞過這條規則?我是一個新手.. – Njw96

+0

*「任何方法來繞過這條規則?」*那麼,就像我已經提到的那樣,有** ASCII藝術**。就圖像而言,將其上傳到圖像共享網站並提供鏈接。如果描述性足夠(足夠小),則其他人可能會將其嵌入到問題 –

回答

4

您不是在添加組件時,而是在定義佈局本身時使用FlowLayout.TRAILING,並且它屬於FlowLayout構造函數參數。所以從add方法中刪除那些代碼。

例如,改變

public class ArtistsPane extends JPanel{ 

    public ArtistsPane(){ 

     this.setLayout(new FlowLayout()); 
     add(new TracksAddRemoveToolBar(),FlowLayout.TRAILING);}} 

public class ArtistsPane extends JPanel{ 

    public ArtistsPane(){  
     setLayout(new FlowLayout(FlowLayout.TRAILING)); 
     add(new TracksAddRemoveToolBar()); 
    } // please place your brakcets with care 
} 
+0

謝謝!通過替換上面提到的FlowLayout.TRAILING,我瞭解了錯誤。我試圖指定工具欄在框架中的對齊方式。請問如何實現這一點的任何想法? – Njw96

+0

@NajwaLaabid: ''我試圖在框架中指定工具欄的對齊方式,請問如何做到這一點?「 - 這完全取決於你想放置工具欄的位置。 –

相關問題