用Java写求圆的面积的中文视图代码
时间: 2023-06-27 07:04:22 浏览: 84
以下是用Java编写求圆的面积的中文视图代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CircleArea extends JFrame implements ActionListener {
private JPanel panel;
private JLabel label1, label2;
private JTextField textField;
private JButton button;
public CircleArea() {
setTitle("求圆面积");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(2, 2, 5, 5));
label1 = new JLabel("请输入圆的半径:");
panel.add(label1);
textField = new JTextField();
panel.add(textField);
label2 = new JLabel();
panel.add(label2);
button = new JButton("计算");
button.addActionListener(this);
panel.add(button);
add(panel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double radius = Double.parseDouble(textField.getText());
double area = Math.PI * radius * radius;
label2.setText("圆的面积为:" + area);
}
public static void main(String[] args) {
new CircleArea();
}
}
```
运行程序后,会弹出一个窗口,用户可以在其中输入圆的半径,点击“计算”按钮后,程序会计算圆的面积并在窗口中显示出来。
阅读全文