2012-03-27 83 views
1

在NetBeans中,我使用了GUI編輯器來創建JFrame,並且在框架中放置了一個JPanel。 目前,我正試圖在類構建時在面板中創建一個新按鈕。 這是我的代碼,但我似乎無法得到它的工作。 (第一行,使按鍵之外,其他線路儘量表現出來。)如何動態添加JButton到JPanel?

this.jPanel2.add(new JButton("Test"),BorderLayout.NORTH); 
this.jPanel2.validate(); 
this.jPanel2.revalidate(); 
this.jPanel2.repaint(); 
this.jPanel2.setVisible(true); 
this.revalidate(); 
this.setVisible(true); 
this.repaint(); 

我一直在谷歌上搜索了一夜,但似乎無法得到它的工作。

+0

1)爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/)。 2)[嵌套佈局示例](http://stackoverflow.com/a/5630271/418556)動態添加標籤。這是基本相同的原則。 – 2012-03-27 08:32:54

+0

您的示例代碼有點稀疏。也許你可以創建一個更完整的例子? – Paaske 2012-03-27 08:34:59

回答

3

有時您看不到某個按鈕時,這是一個佈局管理器問題(因爲您沒有爲佈局管理器設置正確的屬性)。該按鈕(JButton.setBounds()

this.jPanel2.setLayoutManager(null); 

和設置範圍:您可以通過禁用它進行測試。

如果上述問題解決了您的問題,那麼您需要查看您正在使用的LayoutManager設置的要求(另請參閱Robin的答案)。

所有呼叫validate(),revalidate()repaint()不需要這樣做。

+2

-1甚至建議空佈局管理器並手動設置邊界。在這種情況下沒有正當理由去做 – Robin 2012-03-27 08:34:39

+0

我建議它確認調用佈局管理器時出錯,而不是解決方案。 – Thirler 2012-03-27 08:49:07

+0

我添加了倒票,但當我看到您的評論時刪除了它。也許需要編輯和擴展。 – 2012-03-27 09:05:01

3

通常add調用就足夠了。

注意:a BorderLayout只能在每個位置包含一個組件。因此,如果您在NORTH位置添加其他組件,您的按鈕將不可見。

第二個說明:默認情況下,JPanel沒有BorderLayout但是FlowLayout。您是否在該特定面板上設置了BorderLayout?否則BorderLayout#NORTH約束是不正確

所有validaterevalidaterepaint呼叫可以被移除

編輯

似乎需要某種驗證畢竟。我的印象是,Swing應該足夠聰明,以便在某些事情被添加到Container時收聽事件,並更新任何必要的內容(有點類似於更新TableModel根據事件更新JTable,而不需要撥打repaintJTable上的喜歡)。

然而,在SSCCE嘗試這種時候,我來到了下面的代碼(不同的版本,只發布最詳盡版本)

  • 沒有滾動窗格中,validate調用似乎沒有任何效果。其實,我需要再次打電話pack,使新的標籤可見(不包括在SSCCE,但是從代碼刪除滾動面板是微不足道的)
  • 與滾動窗格中,validate呼叫有效果

    import javax.swing.BoxLayout; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    import java.awt.BorderLayout; 
    import java.awt.Container; 
    import java.awt.Dimension; 
    import java.awt.EventQueue; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    
    public class AddLabelsAtRuntime { 
    
        private int fLabelCounter = 0; 
        private JPanel fLabelPanel; 
        private final JFrame fTestFrame; 
    
        public AddLabelsAtRuntime() { 
        fLabelPanel = new JPanel(); 
        BoxLayout boxLayout = new BoxLayout(fLabelPanel, BoxLayout.PAGE_AXIS); 
        fLabelPanel.setLayout(boxLayout); 
        fTestFrame = new JFrame("Dynamically add labels"); 
        } 
    
        private JFrame createUI(){ 
    
        Container contentPane = fTestFrame.getContentPane(); 
        contentPane.setLayout(new BorderLayout()); 
    
        JScrollPane scrollPane = new JScrollPane(fLabelPanel); 
        scrollPane.setPreferredSize(new Dimension(200, 200)); 
        contentPane.add(scrollPane, BorderLayout.CENTER); 
    
        contentPane.add(createButtonPanel(), BorderLayout.SOUTH); 
    
        fTestFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        fTestFrame.pack(); 
        return fTestFrame; 
        } 
    
        private void addLabel(){ 
        fLabelPanel.add(new JLabel("Label " + ++fLabelCounter)); 
        } 
    
        private JPanel createButtonPanel(){ 
        JPanel buttonPanel = new JPanel(); 
        BoxLayout boxLayout = new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS); 
        buttonPanel.setLayout(boxLayout); 
    
        JButton validateButton = new JButton("Add + validate"); 
        validateButton.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent e) { 
         addLabel(); 
         fLabelPanel.validate(); 
         fTestFrame.validate(); 
         } 
        }); 
        buttonPanel.add(validateButton); 
    
        JButton noValidateButton = new JButton("Add"); 
        noValidateButton.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent e) { 
         addLabel(); 
         } 
        }); 
        buttonPanel.add(noValidateButton); 
    
        JButton packButton = new JButton("Add + pack"); 
        packButton.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent e) { 
         addLabel(); 
         fTestFrame.pack(); 
         } 
        }); 
        buttonPanel.add(packButton); 
    
        return buttonPanel; 
        } 
    
        public static void main(String[] args) { 
        EventQueue.invokeLater(new Runnable() { 
         @Override 
         public void run() { 
         AddLabelsAtRuntime addLabelsAtRuntime = new AddLabelsAtRuntime(); 
         addLabelsAtRuntime.createUI().setVisible(true); 
         } 
        }); 
    
        } 
    
    } 
    
    的Java Swing

    與動態圖像和ActionListener的創建的JButton -

+0

我通常覺得有必要調用'validate()' - 就像在上面鏈接的例子中添加標籤一樣。你能否提供一個動態添加組件的SSCCE,而不需要它? – 2012-03-27 09:07:49

+0

今晚我會試一試,但我不記得需要它 – Robin 2012-03-27 09:58:50

+0

期待看到的代碼。 +1評論'BorderLayout.NORTH' – 2012-03-27 10:07:18

0

用圖像和ActionListener的創建動態的JButton。您將可以在一個位置更改按鈕高度,寬度水平間距和垂直間距。

you can find more details from here

enter image description here