2014-12-04 111 views
0

這是我的第一個問題所固有的,所以請多多包涵:) 進出口工作在GUI的動態部分,出於某種原因,它取笑我。 登錄後,該課程將在新窗口中打開。 我想要的,是一個新JPanel,以建立並添加到每一個「添加播放器」按鈕被點擊一次「容器」。它應該把它們下面的海誓山盟,但它所做的是增加在第一次點擊一個按鈕,然後點擊次數的休息之後什麼都不做。動態地添加到JPanels一個JFrame

任何幫助表示讚賞:)

package Gui; 

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
//import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

@SuppressWarnings("serial") 
public class TeamManagerGUI extends JFrame implements ActionListener 
{ 
// private JLabel pNameLabel = new JLabel("Player Name: "), 
//     pSchoolLabel = new JLabel("School: "); 
// private JTextField pNameField = new JTextField(), 
//      pSchoolField = new JTextField(); 
    private JButton addButton = new JButton("Add Player"), 
        removeButton = new JButton("Remove player"); 
    private JPanel container = new JPanel(), 
        playerContainer = new JPanel(); 


    int frameCounter = 1; 

    public TeamManagerGUI() 
    { 
     super("Team Manager User Interface"); 
     setSize(1200,800); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     container.setLayout(new GridBagLayout()); 
     playerContainer.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.insets = new Insets(3,3,3,3); 
     addButton.addActionListener(this); 
     container.add(addButton,gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.insets = new Insets(3,3,3,3); 
     container.add(removeButton,gbc); 

     this.add(container); 
    } 

    public void playerFrame() 
    { 
     GridBagConstraints gbc = new GridBagConstraints(); 

     playerFrameArr.add(new JPanel()); 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     playerContainer.add(new JButton("LABEL"),gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     playerContainer.add(new JButton("BUTTON"),gbc); 

     gbc.gridx = 0; 
     gbc.gridy = frameCounter+1; 
     container.add(playerContainer,gbc); 

     System.out.println(frameCounter); 

     frameCounter++; 
    } 

    public void addPlayerRow() 
    { 
     playerFrame(); 
     container.revalidate(); 
     container.repaint(); 
    } 

    public void removePlayerRow() 
    { 
     //Not yet implemented 
    } 

    public void actionPerformed(ActionEvent ae) 
    { 
     if(ae.getSource() == addButton) 
     { 
      addPlayerRow(); 
     } 
     if(ae.getSource() == removeButton) 
     { 
      //Not yet implemented 
     } 
    } 
} 

回答

1

你一次又一次地加入playerContainer。我想你應該使用新創建的JPanel。這個應該填充並添加到主容器中。

添加單個面板多次無法正常顯示,因爲這螺絲了佈局。我認爲你需要一個參考保持到了新的JPanel並填寫這一項與你的佈局和按鈕。

我想是這樣的:

public void playerFrame() 
{ 
    GridBagConstraints gbc = new GridBagConstraints(); 

    JPanel newPanel = new JPanel(new GridBagLayout()); 
    playerFrameArr.add(newPanel); 

    gbc.gridx = 0; 
    gbc.gridy = 0; 
    newPanel.add(new JButton("LABEL"), gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 0; 
    newPanel.add(new JButton("BUTTON"), gbc); 

    gbc.gridx = 0; 
    gbc.gridy = frameCounter + 1; 
    container.add(newPanel, gbc); 

    System.out.println(frameCounter); 

    frameCounter++; 
} 
+0

現在,他們補充精,我怎麼能刪除一個從底部的時間?他們沒有任何真實姓名,或者我錯過了什麼? – H4rdstyler 2014-12-04 14:22:38

+0

那麼你有你的'playerFrameArr'列表不是嗎?您可以從此列表中檢索最後一個條目,並從您的容器中刪除此面板。應該工作正常。您可能還想使用此列表的大小作爲幀計數器。如果您想在常量基礎上添加和刪除這些條目,這可能會派上用場。 – Nitram 2014-12-04 14:33:44