我有一個包含5個按鈕的JFrame的JPanel。在另一個JPanel中有一個叫做「刪除按鈕」的按鈕,我想要做的就是點擊這個按鈕,然後在其中一個按鈕上選擇要刪除的其他按鈕5。誰能幫我?刪除一個特定的按鈕
public class gui extends JFrame implements ActionListener
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p2 = new JPanel();
JButton b1 = new JButton("Delete");
JButton b2 = new JButton("A");
JButton b3 = new JButton("B");
JButton b4 = new JButton("C");
gui()
{
p1.setLayout(new GridLayout(1,2));
p1.add(p2);
p1.add(p3);
p2.setLayout(new GridLayout(3,1));
p2.add(b2);
p2.add(b3);
p2.add(b4);
p3.add(b1);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
// When I click this button I want to be able to delete a button of my choice (one of the other 3)
}
}
我們_might_能夠幫助你,如果你把更多的精力投入到你的問題:什麼了你已經試過了嗎?任何簡短但相關的代碼示例? – Thomas 2012-03-22 17:39:52
使用一個布爾變量來指示下一個被點擊的按鈕將被「刪除」。如果變量設置爲true,則點擊其他按鈕,然後將該按鈕的可見性設置爲false。再次初始化布爾變量爲false。 – Marcelo 2012-03-22 17:45:19
當你提交代碼時,確保它運行。這節省了其他人的時間,並讓他們專注於所問的問題。添加主要方法,打包元素,設置默認的關閉操作(包括include語句),以及將組件添加到JFrame中都沒有太多要求。 – 2012-03-22 19:43:10