java导入包后为什么报错
package radius;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Radius implements ActionListener {
JFrame frame;
JPanel p;
JLabel label1, label2;
JButton button1, button2;
JTextField tf;
Radius() {
frame = new JFrame("计算圆的面积");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
label1 = new JLabel("请输入半径(米):");
p.add(label1);
tf = new JTextField(20);
p.add(tf);
// 添加两个按钮
button1 = new JButton("计算面积");
button1.addActionListener(this); // 注册按钮1的监视器
p.add(button1);
button2 = new JButton("清空");
button2.addActionListener(this); // 注册按钮2的监视器
p.add(button2);
label2 = new JLabel();
p.add(label2);
frame.getContentPane().add(p);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
// 处理计算面积的按钮事件
double radius = Double.parseDouble(tf.getText());
double area = Math.PI * radius * radius;
label2.setText("圆的面积为: " + area + " 平方米");
} else if (e.getSource() == button2) {
// 处理清空按钮事件
tf.setText("");
label2.setText("");
}
}
public static void main(String[] args) {
new Radius();
}
}