2014-03-12 40 views
0

我的目標是通過單擊JLabel同時刪除兩個JTextFields。動態添加JTextFields後刪除JTextFields

我創建的文本框是這樣的:

public void mouseClicked(MouseEvent e) { 


      inc++; 
      txtName= new JTextField(); 
      txtNumber = new JTextField(); 
      txtName.setName("txtName"+inc); 
      txtNumber.setName("txtNumber" + inc); 

      pnlPanel.add(txtName); 
      pnlPanel.add(txtNumber); 

      if(count>0){ 
       x+=50; 
       y+=50; 

       txtName.setBounds(225,6+y, 182, 27); 
       txtNumber.setBounds(35, 6+y, 182, 27); 
       txtName.setName(tempBox+count); 
       if(pnlTxtText.getComponentCount() >9){ 

        pnlPanel.setPreferredSize(new Dimension(450+y,50+y)); 
        pnlPanel.add(txtStudName); 
        pnlPanel.add(txtStudentNumber); 

        frmFrame.repaint(); 
        scrpPanel.revalidate(); 
       } 
      } 
      frmFrame.repaint(); 
     } 

    }); 

這是我的代碼去除文本框:

public void mouseClicked(MouseEvent e) { 
    int countPlace= pnlPanel.getComponentCount(); 
    int countOfRemaining =countPlace; 
    pnlPanel.remove(--countOfRemaining); 
    frmFrame.revalidate(); 
    pnlPanel.remove(--countOfRemaining); 
    frmFrame.revalidate(); 
} 

});

而不是刪除同一行上的txtfields,它會逐一刪除它,我不希望這樣。請幫幫我。謝謝。

To further understand my problem i posted a picture. Ever time I click a label, it should delete the last two fields but instead of doing that, it deletes txtfield from a different place

回答

2

呼叫pnlPanel.revalidate()repaint()

,不使用的setBounds()。改爲定義porper LayoutManager

+0

;嗨。我添加了重新驗證。我不明白的是爲什麼這個代碼:int countPlace = \t pnlTxtTxt.getComponentCount(); \t \t \t \t int countOfRemaining = countPlace; \t \t \t \t pnlTxtTxt.remove(countOfRemaining); \t \t \t \t frmGM.revalidate(); \t \t \t \t frmGM.repaint(); \t \t \t \t \t \t \t pnlTxtTxt.remove(countOfRemaining-1);引發此異常:線程「AWT-EventQueue-0」中的異常java.lang.ArrayIndexOutOfBoundsException:數組索引超出範圍:8 \t \t \t \t frmGM。重新驗證(); \t \t \t \t frmGM.repaint() – harraypotter

1

取而代之的是,

public void mouseClicked(MouseEvent e) { 
     int countPlace= pnlPanel.getComponentCount(); 
     int countOfRemaining =countPlace; 
     pnlPanel.remove(countOfRemaining-1); 
     frmFrame.repaint(); 
     pnlPanel.remove(countOfRemaining-1); 
     frmFrame.repaint(); 
    } 
}); 

使用,因爲你刪除一個組件後,計數剩餘的變量沒有被遞減這個

public void mouseClicked(MouseEvent e) { 
     int countPlace= pnlPanel.getComponentCount(); 
     int countOfRemaining =countPlace; 
     pnlPanel.remove(--countOfRemaining); 
     frmFrame.revalidate(); 
     pnlPanel.remove(--countOfRemaining); 
     frmFrame.revalidate(); 
    } 
}); 

以上拋出了一個異常ArrayIndeOutOfBounds。因此,當您嘗試第二次刪除時,索引超出了限制範圍。

+0

你好,謝謝。它刪除2個文本字段,但它不會刪除我想要刪除它們的文本字段。這是爲什麼? – harraypotter

+0

該代碼只會刪除上次添加到面板的文本字段。你能否正確解釋我如何刪除文本字段?您的面板佈局設置爲哪種類型?它是默認的FlowLayout嗎? –

+0

我發佈了圖片。你的代碼是正確的,我只是不明白爲什麼它不會刪除最後兩行。請看我的照片。謝謝! – harraypotter

0

我建議您將所有JPanels添加到地圖(我使用HashMap)中,形式爲<Integer, JPanel>。按順序命名它們,然後做Map.remove(Map.size() - 1)Map.remove(Map.size() - 2)

您還可以通過執行Map.keySet()來獲取當前整數(鍵)集;

Map<Integer, JPanel> temp = new HashMap<Integer, JPanel>(); 
temp.put(0, new JPanel()); 
temp.put(1, new JPanel()); 
temp.put(2, new JPanel()); 
temp.remove(temp.size() - 1); 

使維護多組面板變得更容易。在我的應用程序中,我不會用數字來控制它們,而是用短名稱來代替。