2014-10-29 129 views
0

是的,我GOOGLE了大約30分鐘。是的,在stackoverflow中有關於該主題的2個不同的帖子,但那些不會給我任何解決我的問題。BoxLayout無法共享

我用了很多BoxLayout來定位一些東西。當我嘗試添加最後的東西到我的主面板時,我得到「BoxLayout不能共享」。

代碼:

private void open(int i) { 
    JLabel titelLabel = new JLabel("Aufgabenblatttitel: "); 
    JTextField titelTextField = new JTextField(); 
    JLabel dozentLabel = new JLabel("Dozent: "); 
    JTextField dozentTextField = new JTextField(); 
    JLabel beschreibungLabel = new JLabel("Aufgabenblattbeschreibung: "); 
    JTextField beschreibungTextField = new JTextField(); 
    JLabel studiengangLabel = new JLabel("Studiengang: "); 
    JTextField studiengangTextField = new JTextField(); 
    JLabel dateLabel = new JLabel("Erstellt am: "); 

    for(Aufgabe aufgabe : data.get(i).getAufgaben()) { 
     JPanel aufgabenPanel = new JPanel(); 

     JLabel aufgabeTitelLabel = new JLabel("Titel: "); 
     JTextField aufgabeTitelTextField = new JTextField(); 
     aufgabeTitelTextField.setText(aufgabe.getTitel()); 
     JPanel aufgabeTitelPanel = new JPanel(); 
     aufgabeTitelPanel.add(aufgabeTitelLabel); 
     aufgabeTitelPanel.add(aufgabeTitelTextField); 
     aufgabeTitelPanel.setLayout(new BoxLayout(aufgabeTitelPanel, BoxLayout.LINE_AXIS)); 

     JLabel aufgabeBeschreibungLabel = new JLabel("Beschreibung: "); 
     JTextField aufgabeBeschreibungTextField = new JTextField(); 
     aufgabeBeschreibungTextField.setText(aufgabe.getBeschreibung()); 
     JPanel aufgabeBeschreibungPanel = new JPanel(); 
     aufgabeBeschreibungPanel.add(aufgabeBeschreibungLabel); 
     aufgabeBeschreibungPanel.add(aufgabeBeschreibungTextField); 
     aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS)); 

     JLabel aufgabeLoesungLabel = new JLabel("Lösung: "); 
     JTextField aufgabeLoesungTextField = new JTextField(); 
     aufgabeLoesungTextField.setText(aufgabe.getLoesung()); 
     JPanel aufgabeLoesungPanel = new JPanel(); 
     aufgabeLoesungPanel.add(aufgabeLoesungLabel); 
     aufgabeLoesungPanel.add(aufgabeLoesungTextField); 
     aufgabeLoesungPanel.setLayout(new BoxLayout(aufgabeLoesungPanel, BoxLayout.LINE_AXIS)); 

     aufgabenPanel.add(aufgabeTitelPanel); 
     aufgabenPanel.add(aufgabeBeschreibungPanel); 
     aufgabenPanel.add(aufgabeLoesungPanel); 
     aufgabenPanel.setLayout(new BoxLayout(aufgabenPanel, BoxLayout.PAGE_AXIS)); 

     this.add(aufgabenPanel); 
    }  
} 

這是類 「AufgabeEditieren」,這被定義爲部分:

public class AufgabeEditieren extends JPanel { ... } 

所以說:AufgabeEditieren構造函數調用open()類已經被初始化之後。它試圖創建一些面板和對象,並希望通過「this.add(aufgabenPanel);」將它們添加到類本身。這是參考AufgabeEditieren(它的對象)。那爲什麼它不起作用?它的面板,應該能夠得到這些物品?謝謝...

回答

3

好吧,我花了一段時間,因爲我真的不熟悉你的母語(如果你用英文名稱發佈變量的代碼,對每個人來說都會更簡單),但問題來自在這裏:

aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS)); 

你的設置上aufgabeBeschreibungPanel一個BoxLayout的,但你提供aufgabeBeschreibungLabelBoxLayout參數。而應該寫:

aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungPanel, BoxLayout.LINE_AXIS)); 

當遇到這個問題,這是最常見的原因是,你寫道:

y.setLayout(new BoxLayout(x, BoxLayout.XXX)); 

其中yx是不同的。

+0

謝謝!當我太累時,我應該停止編碼。像這樣愚蠢的東西不得不躺在身邊。 – Shiuyin 2014-10-29 20:51:11