Java Swing 库来创建一个圆形计算器应用程序

您所在的位置:网站首页 java有两类应用程序 Java Swing 库来创建一个圆形计算器应用程序

Java Swing 库来创建一个圆形计算器应用程序

2023-06-15 01:42| 来源: 网络整理| 查看: 265

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class CircleCalculator extends JFrame { private JTextField radiusField; // 半径输入框 private JLabel perimeterLabel; // 周长标签 private JLabel areaLabel; // 面积标签 public CircleCalculator() { setTitle("圆形计算器"); // 设置窗口标题 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭操作为退出应用程序 setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器 GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5, 5, 5, 5); JLabel radiusTextLabel = new JLabel("请输入半径:"); // 创建半径提示标签 radiusField = new JTextField(10); // 创建半径输入框,长度为10个字符 JButton calculateButton = new JButton("计算"); // 创建计算按钮 perimeterLabel = new JLabel(); // 创建周长标签 areaLabel = new JLabel(); // 创建面积标签 calculateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { calculate(); // 当计算按钮被点击时,调用calculate()方法 } }); gbc.gridx = 0; gbc.gridy = 0; add(radiusTextLabel, gbc); // 将半径提示标签添加到窗口中 gbc.gridx = 1; gbc.gridy = 0; add(radiusField, gbc); // 将半径输入框添加到窗口中 gbc.gridx = 0; gbc.gridy = 1; add(calculateButton, gbc); // 将计算按钮添加到窗口中 gbc.gridx = 0; gbc.gridy = 2; add(new JLabel("周长:"), gbc); // 创建周长标签,并将其添加到窗口中 gbc.gridx = 1; gbc.gridy = 2; add(perimeterLabel, gbc); // 将周长标签添加到窗口中 gbc.gridx = 0; gbc.gridy = 3; add(new JLabel("面积:"), gbc); // 创建面积标签,并将其添加到窗口中 gbc.gridx = 1; gbc.gridy = 3; add(areaLabel, gbc); // 将面积标签添加到窗口中 pack(); // 根据组件的大小调整窗口的大小 setLocationRelativeTo(null); // 将窗口居中显示 } private void calculate() { String radiusText = radiusField.getText(); // 获取输入的半径文本 try { double radius = Double.parseDouble(radiusText); // 将半径文本转换为double类型 double perimeter = 2 * Math.PI * radius; // 计算周长 double area = Math.PI * radius * radius; // 计算面积 perimeterLabel.setText(String.format("%.2f", perimeter)); // 将周长设置为周长标签的文本 areaLabel.setText(String.format("%.2f", area)); // 将面积设置为面积标签的文本 } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "无效的半径。请输输入一个数字。"); // 如果半径无效(非数字),显示错误对话框 } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CircleCalculator().setVisible(true); // 创建CircleCalculator对象并显示窗口 } }); } }

下面是代码的架构和思路分析:

1. 导入所需的 Swing 类和事件处理类。

2. 创建 `CircleCalculator` 类,继承自 `JFrame`,用于创建应用程序的窗口。

3. 在 `CircleCalculator` 类中定义了三个私有变量:    - `radiusField`:用于输入半径的文本框。    - `perimeterLabel`:用于显示周长的标签。    - `areaLabel`:用于显示面积的标签。

4. 在构造函数 `CircleCalculator()` 中:    - 设置窗口的标题为 "圆形计算器"。    - 设置窗口关闭操作为退出应用程序。    - 使用 `GridBagLayout` 布局管理器来布局窗口的组件。

5. 创建 `GridBagConstraints` 对象 `gbc`,用于设置组件的位置和大小。

6. 创建 `JLabel` 对象 `radiusTextLabel`,用于显示半径的提示文本。

7. 创建 `JTextField` 对象 `radiusField`,用于用户输入半径的文本框。

8. 创建 `JButton` 对象 `calculateButton`,用于触发计算操作。

9. 创建 `JLabel` 对象 `perimeterLabel`,用于显示计算结果中的周长。

10. 创建 `JLabel` 对象 `areaLabel`,用于显示计算结果中的面积。

11. 使用 `calculateButton` 的 `addActionListener` 方法添加一个事件监听器,当用户点击该按钮时,会触发 `calculate()` 方法。

12. 通过 `GridBagConstraints` 的属性设置,将上述组件添加到窗口的指定位置。 ? 13. 在 `calculate()` 方法中,获取用户输入的半径文本,将其转换为 `double` 类型。

14. 使用半径计算周长和面积,并将结果存储在 `perimeter` 和 `area` 变量中。

15. 将计算结果格式化为两位小数,并将其设置为 `perimeterLabel` 和 `areaLabel` 的文本。

16. 如果半径文本无法转换为 `double` 类型(即非数字),捕获 `NumberFormatException` 异常,显示错误对话框提示用户输入一个有效的半径。

17. 在 `main()` 方法中,使用 `SwingUtilities.invokeLater()` 方法创建并显示 `CircleCalculator` 对象的窗口。

它使用了 Java Swing 的 GUI 组件和事件处理机制来实现一个简单的圆形计算器应用程序。

用户可以输入圆的半径,点击计算按钮后,程序会计算并显示圆的周长和面积。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3