2012-10-22 43 views
1

當我點擊位於某個面板上的按鈕時,該面板是否會被刪除?Java - 刪除按鈕所在的面板

public void actionPerformed(ActionEvent e) { 
    if(e.getSource()==removebutton) 
     System.out.println("ok"); 
    removebutton.getRootPane().remove(cartpanel); 

} 

我試圖做一個購物車,其中,當用戶查看購物車,他會看到位於該有一個刪除按鈕面板項目。然後當他點擊該按鈕時,面板將被移除。

我上面的代碼沒有做預期的輸出,但是有沒有任何方法應該與它一起使用?

panel http://dl.dropbox.com/u/62021435/Untitled.png

+1

從你的照片,我會建議使用'JTable'代替,參見[如何使用JTable中( http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)獲取更多信息 – MadProgrammer

回答

1

您可以使用Component#getParent去父容器的引用。然後,您將需要得到Container的父,並從中刪除按鈕的容器......

Container myParent = removebutton.getParent(); 
Container parent = myParent.getParent(); 
parent.remove(myParent); 
+0

是myParent那裏的按鈕所在的面板。父母是所有事物所在的面板? – Katherine

+1

'myParent'是'removebutton'生命的容器,'parent'是'myParent'生命的地方。爲了移除按鈕容器,我們需要對容器父對象的引用,以便我們可以要求它移除按鈕容器:P – MadProgrammer