我已经有了一个按钮,我想在点这个按钮时在上面的面板中再创建并显示一个按钮,代码如下:public class TestButton extends JFrame {
public static void main(String args[]) {
try {
TestButton frame = new TestButton();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} public TestButton() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel center = new JPanel();
getContentPane().add(center, BorderLayout.CENTER); final JPanel south = new JPanel();
getContentPane().add(south, BorderLayout.SOUTH); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
JButton newbutton = new JButton("Heeeeeeeee");
center.add(newbutton);
center.repaint();
}
});
button.setText("New JButton");
south.add(button);
}
}我运行程序时点按钮却根本没有反应,没有新的按钮出现,请问我错在哪里啊?我在网上没有搜索到相关资料,麻烦大家帮帮忙。