使用动作侦听器更改JPanel的内容

时间:2016-06-05 22:09:42

标签: java swing jpanel

有没有办法给JButton一个动作监听器,它将改变按钮所在的JPanel的内容(并删除过程中的按钮)。我现在有这个代码,但它只是让代码冻结。

public class CL extends Frame implements WindowListener, ActionListener
{
    //Main method calls initializer (and sets size and visibility)

    //Initializer adds a panel to the frame and a button that calls the action Listener
    public void actionPerformed(ActionEvent event) 
    {
        //Clear the JPanel (That the button is in)
        panel.removeAll();
        //Add new contents to the panel
        JLabel label = new JLabel("This is a new label")
        panel.add(label);
    }
}

这不会抛出任何错误,所以...这里有错误还是我接近这个完全错误?在此先感谢您的任何帮助

0 个答案:

没有答案