模拟windows程序附带的计算器程序,用java写一个计算器,要求有用户使用界面,归零键,清除键,输入面板,实现各个进制转换,输入一个默认为十进制的数,面板显示分别转换为二进制,十进制,八进制,十六进制的值,要求有异常处理

时间: 2024-05-04 15:20:22 浏览: 11
好的,我来为你编写一个简单的Java计算器程序,满足以上要求。 首先,我们需要设计用户界面。我将使用Swing框架来创建一个简单的GUI界面。具体代码如下: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalculatorGUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField inputField; private JLabel binaryLabel, decimalLabel, octalLabel, hexLabel; private JButton zeroButton, clearButton; private JButton binButton, decButton, octButton, hexButton; private JPanel panel1, panel2, panel3, panel4; public CalculatorGUI() { super("Calculator"); // 创建输入面板 inputField = new JTextField(20); // 创建归零键、清除键 zeroButton = new JButton("0"); clearButton = new JButton("C"); zeroButton.addActionListener(this); clearButton.addActionListener(this); // 创建进制转换按钮 binButton = new JButton("BIN"); decButton = new JButton("DEC"); octButton = new JButton("OCT"); hexButton = new JButton("HEX"); binButton.addActionListener(this); decButton.addActionListener(this); octButton.addActionListener(this); hexButton.addActionListener(this); // 创建用于显示转换结果的标签 binaryLabel = new JLabel("Binary: "); decimalLabel = new JLabel("Decimal: "); octalLabel = new JLabel("Octal: "); hexLabel = new JLabel("Hexadecimal: "); // 创建面板 panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); // 将组件添加到面板中 panel1.add(inputField); panel2.add(binButton); panel2.add(decButton); panel2.add(octButton); panel2.add(hexButton); panel3.add(binaryLabel); panel3.add(decimalLabel); panel4.add(octalLabel); panel4.add(hexLabel); // 将面板添加到窗口中 add(panel1, BorderLayout.NORTH); add(panel2, BorderLayout.CENTER); add(panel3, BorderLayout.SOUTH); add(panel4, BorderLayout.SOUTH); add(zeroButton, BorderLayout.WEST); add(clearButton, BorderLayout.EAST); // 设置窗口属性 setSize(400, 200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String input = inputField.getText(); try { // 按钮事件处理 if (e.getSource() == zeroButton) { inputField.setText(input + "0"); } else if (e.getSource() == clearButton) { inputField.setText(""); binaryLabel.setText("Binary: "); decimalLabel.setText("Decimal: "); octalLabel.setText("Octal: "); hexLabel.setText("Hexadecimal: "); } else if (e.getSource() == binButton) { int decimal = Integer.parseInt(input, 2); binaryLabel.setText("Binary: " + input); decimalLabel.setText("Decimal: " + decimal); octalLabel.setText("Octal: " + Integer.toOctalString(decimal)); hexLabel.setText("Hexadecimal: " + Integer.toHexString(decimal)); } else if (e.getSource() == decButton) { int decimal = Integer.parseInt(input); binaryLabel.setText("Binary: " + Integer.toBinaryString(decimal)); decimalLabel.setText("Decimal: " + input); octalLabel.setText("Octal: " + Integer.toOctalString(decimal)); hexLabel.setText("Hexadecimal: " + Integer.toHexString(decimal)); } else if (e.getSource() == octButton) { int decimal = Integer.parseInt(input, 8); binaryLabel.setText("Binary: " + Integer.toBinaryString(decimal)); decimalLabel.setText("Decimal: " + decimal); octalLabel.setText("Octal: " + input); hexLabel.setText("Hexadecimal: " + Integer.toHexString(decimal)); } else if (e.getSource() == hexButton) { int decimal = Integer.parseInt(input, 16); binaryLabel.setText("Binary: " + Integer.toBinaryString(decimal)); decimalLabel.setText("Decimal: " + decimal); octalLabel.setText("Octal: " + Integer.toOctalString(decimal)); hexLabel.setText("Hexadecimal: " + input); } } catch (NumberFormatException ex) { // 处理异常情况 JOptionPane.showMessageDialog(this, "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE); inputField.setText(""); binaryLabel.setText("Binary: "); decimalLabel.setText("Decimal: "); octalLabel.setText("Octal: "); hexLabel.setText("Hexadecimal: "); } } public static void main(String[] args) { new CalculatorGUI(); } } ``` 这是一个简单的计算器GUI界面,其中包括一个输入面板、归零键、清除键、进制转换按钮、以及用于显示转换结果的标签。我们还实现了ActionListener接口,以便处理各种按钮事件。 接下来,我们需要实现进制转换功能。在上面的代码中,我们使用了Java内置的Integer类来进行进制转换,具体实现如下: ```java int decimal = Integer.parseInt(input, 2); // 二进制转十进制 String binary = Integer.toBinaryString(decimal); // 十进制转二进制 String octal = Integer.toOctalString(decimal); // 十进制转八进制 String hex = Integer.toHexString(decimal); // 十进制转十六进制 ``` 这是将输入的二进制字符串转换为其他进制的代码。其他进制转换的代码类似,只需要将参数中的2、8、16改为相应的进制数即可。 最后,我们需要处理异常情况。当用户输入无效的字符串时,我们可以使用try-catch语句来捕获异常,并弹出一个错误提示框。 这是一个简单的Java计算器程序,满足了题目要求。当然,如果需要更多的功能,可以根据自己的需求进行扩展。

相关推荐

最新推荐

recommend-type

设计一个程序来模拟一个简单的手持计算器

设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、/、=、以及C(清除)、A(全清除)操作。 基本要求 程序运行时,显示一个窗口,等待用户输入,用户可以从键盘输入要计算的表达式,输入的表达式...
recommend-type

java实现计算器加法小程序(图形化界面)

主要介绍了Java实现图形化界面的计算器加法小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

用JAVA编写计算器程序(模拟Windows计算器)

编写模拟Windows的计算器,其实这个程序很简单的,想知道的话,可以看一看。
recommend-type

Java 计算器模拟程序

UML的静态结构图 2. 程序主要流程图 3. 程序代码 4. 程序说明文档 5. 实验总结:总结本次程序调试过程中遇到了哪些问题,如何解决的;通过本次大作业有何收获?存在哪些不足需要改进的地方。
recommend-type

java加减乘除计算器界面编程

编写程序(图形用户界面) 编写一个应用程序,设计一个标题为“计算”的窗口,设计四个按钮,分别命名为“加”、“减”、“积”、“除”,设计三个文本框,单击相应的按钮,将两个文本框的数字做运算,在第三个...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解答下列问题:S—>S;T|T;T—>a 构造任意项目集规范族,构造LR(0)分析表,并分析a;a

对于这个文法,我们可以构造以下项目集规范族: I0: S -> .S S -> .T T -> .a I1: S -> S. [$ T -> T. [$ I2: S -> T. I3: S -> S.;S S -> S.;T T -> T.;a 其中,点(.)表示已经被扫描过的符号,;$表示输入串的结束符号。 根据项目集规范族,我们可以构造出LR(0)分析表: 状态 | a | $ ---- | - | - I0 | s3| I1 | |acc I2 | | 其中s3表示移进到状态3,acc表示接受。在分析字符串a;a时,我们可以按照以下步骤进行
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。