2010-01-06 19 views
0

我正在開發Java桌面應用程序。在GUI中,我希望該用戶可以根據需要動態添加任意數量的工具欄。爲了實現這一點,以下是我已經做過的事情:在Java Swing GUI中動態添加工具欄

  • 採取了mainPanel中並設置其佈局的BorderLayout
  • 然後採取topPanel,並把它添加到mainPanel中的BorderLayout.NORTH
  • 設置topPanel的佈局,BoxLayout的
  • 然後採取5個板命名toolbar1Panel,toolbar2Panel,...
  • Afterthat,增加了一個工具欄,每個在前面的步驟中創建的toolbarPanel的。
  • 現在有一個名爲其上又添加到topPanel的「toolbar1Panel補充說:」第一個工具欄上的「添加」按鈕僅添加了一個toolbarPanel即toolbar1Panel在topPanel

現在我已經實現了上面的「添加」按鈕「的actionPerformed()」方法如下:

// to add second toolbar Panel to the topPanel dynamically 
topPanel.add(toolbar2Panel); 

但問題是,它不工作。意味着沒有工具欄被添加到topPanel。

有什麼我失蹤的。

代碼是Netbeans生成的,所以我認爲它只會給別人添加混亂,這就是爲什麼我沒有在這裏粘貼任何代碼。

+0

什麼是對topPanel佈局管理器? – 2010-01-06 16:33:59

+0

很高興看到更多的代碼。 – Poindexter 2010-01-06 16:34:20

+0

您是否已將toolbar1Panel ...添加到頂部面板? – 2010-01-06 16:34:36

回答

3

向BoxLayout添加另一個工具欄後,您可能需要(re | in)?驗證面板。

我已經重複這樣做了,但我無法理解3個左右方法調用的邏輯;所以我只是嘗試,直到我打的作品之一:

topPanel.validate(); 
topPanel.invalidate(); 
topPanel.revalidate(); 
topPanel.layout(); 

(至少)其中之一應該強迫你的GUI重新計算其佈局,使北面板更大,因此表現出第二(和連續的)工具欄。

+0

萬歲!它正在工作。 非常感謝你。自從早上我一直在嘗試這件事。你已經在我的所有3個職位中給出答案。非常感謝。 – 2010-01-06 17:00:46

+0

非常好!很高興聽到我能夠幫助。你最終使用哪個驗證系列? – 2010-01-06 17:42:47

+0

我用了revalidate() – 2010-01-07 08:22:06

2

沒有指定頂部面板的佈局,它可能會假定不正確的佈局。

向它添加兩個工具欄面板可能只是將第一個替換爲第二個,或者忽略第二個。

只是爲了測試,將topPanel的佈局設置爲FlowLayout並重試。

+0

其實我已經將topPanel的佈局添加爲BoxLayout。我忘了寫在上面的描述。它也不是以這種佈局運行的。 – 2010-01-06 16:43:27

0

我認爲你在測試之前試圖做的太多。我會這樣做的方式是從一些非常簡單的事情開始,例如一個面板,一個靜態標籤。如果按照您的預期顯示,請使用菜單項添加工具欄。那樣有用嗎。然後肆意添加件。

很可能你會遇到一個簡單案例的問題,然後可以弄清楚,或者你會有一個簡單的案例在這裏發佈。

交替地,作爲開始點從網絡pinck一些工作示例。剪下來,然後建立你的情況。

0

它有幫助嗎? 你想達到什麼?

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JToolBar; 
import javax.swing.SwingUtilities; 

public class AddingToolbars { 
    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable(){ 

      public void run() { 
       AddingToolbars me = new AddingToolbars(); 
       me.initGui(); 

      } 

     }); 

    } 

    private JPanel topPanel; 
    private JPanel mainPanel; 
    private JFrame frame; 

    private void initGui() { 
     frame = new JFrame(); 

     mainPanel = new JPanel(new BorderLayout()); 
     frame.setContentPane(mainPanel); 

     topPanel = new JPanel(); 
     BoxLayout bLayout = new BoxLayout(topPanel,BoxLayout.Y_AXIS); 
     topPanel.setLayout(bLayout); 
     mainPanel.add(topPanel,BorderLayout.NORTH); 

     JButton addButton = new JButton("Add toolbar"); 
     mainPanel.add(addButton,BorderLayout.CENTER); 

     addButton.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) { 
       addNewToolBar(); 
      } 

     }); 

     frame.setSize(500,500); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

    } 

    protected void addNewToolBar() { 
     JToolBar tb = new JToolBar(); 
     tb.add(new JButton("b1")); 
     tb.add(new JButton("b2")); 
     tb.add(new JButton("b3")); 

     topPanel.add(tb); 
     mainPanel.validate(); 
    } 
}