你能帮我这个代码吗?我怎样才能使它在单击按钮时出现第二个按钮?我已经添加了动作监听器并创建了第二个按钮,但我似乎无法做到这一点。太感谢大家了!!!
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Skeleton extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Skeleton");
JPanel panel = new JPanel();
JButton button = new JButton("This is a button.");
JButton button2 = new JButton("Hello");
frame.setSize(600,600);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setContentPane(panel);
panel.setLayout(new FlowLayout());
panel.add(button);
}
public void actionPerformed(ActionEvent e) {
panel.add(button2); //Whenever I compile with this line
//of code inserted, it tells
//me cannot find Button 2
}
}
再次感谢!