2013-08-28 36 views
0
jPanel1 = new JPanel(); 
GridBagLayout layout = new GridBagLayout(); 
jPanel1.setLayout(layout); 
GridBagConstraints gbc = new GridBagConstraints(); 


filler = new JLabel(); 
gbc.gridx = 1; 
gbc.gridy = 1; 
gbc.weightx = 1; 
gbc.weighty = 1; 

jPanel1.add(filler, gbc);  

我試圖通過刪除jPanel1.remove(填充),然後將新的JLabel放置在那個位置,但顯然不工作。我究竟做錯了什麼?在GridBagLayout中正確刪除一個元素

謝謝!

+0

顯然此問題與您最後一個問題(http://stackoverflow.com/q/18458887/131872)有關。當你不知道整個問題的背景時,它會花時間試圖回答一個問題。祝你好運,我不知道你可能忘記提及的其他信息。 – camickr

回答

1

至於爲什麼你有問題,我只能想象。

不要忘記,filler組件是假設將被添加到最後一個組件的右邊/底部....

例如...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 

public class GridBagLayoutTest { 

    public static void main(String[] args) { 
     new GridBagLayoutTest(); 
    } 

    public GridBagLayoutTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       final TestPane pane = new TestPane(); 
       JButton btn = new JButton("Add"); 
       btn.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         pane.addNewItem(); 
        } 
       }); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(pane); 
       frame.add(btn, BorderLayout.SOUTH); 
       frame.pack(); 
       frame.setSize(300, 300); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private int gridX = 0; 
     private int gridY = 0; 
     private JLabel filler; 

     private int columnCount = 4; 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      filler = new JLabel(); 
     } 

     public void addNewItem() { 

      remove(filler); 

      JLabel label = new JLabel("Cell " + gridX + "x" + gridY); 
      label.setBorder(new EmptyBorder(10, 10, 10, 10)); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = gridX; 
      gbc.gridy = gridY; 
      add(label, gbc); 
      gridX++; 
      if (gridX >= columnCount) { 
       gridX = 0; 
       gridY++; 
      } 

      gbc = new GridBagConstraints(); 
      gbc.gridx = columnCount; 
      gbc.gridy = gridY + 1; 
      gbc.weightx = 1; 
      gbc.weighty = 1; 
      add(filler, gbc); 

      revalidate(); 
      repaint(); 

     } 

    } 

} 
+0

它的工作.. 我真的很笨!我添加的標籤的weightx和weighty等於1,而不是將它們重置爲0。 非常感謝你。 – user2712751

1

如果filler只是一個JLabel,那麼你可以做

filler.setText("add text here"); 

或者,如果您要更換不同的組件,更好的方法是創建一個使用卡片佈局的面板。然後你可以交換這兩個組件。有關更多信息,請參閱How to Use Card Layout上的Swing教程。

另一種選擇可能是做這樣的事情:

GridBagLayout layout = (GridBagLayout)jPanel1.getLayout(); 
GridbagConstraint gbc = layout.getConstraint(oldComponent); 
jPanel1.remove(oldComponent); 
jPanel1.add(newComponent, gbc); 
jPanel1.revalidate(); 
jPanel1.repaint(); 
+0

我真的不想交換它們。只要刪除一個,並把另一個放在其他位置。它真的很難改變一切..沒辦法,我可以用GridBag做到這一點? – user2712751

+0

這正是CardLayout所做的。它在同一位置顯示不同的組件。你甚至讀過教程嗎? – camickr

+0

@camickr對於一些更多的上下文,看看OP的[上一個問題](http://stackoverflow.com/questions/18458887/incorrect-jpanel-position-in-gridbaglayout) – MadProgrammer