简易 计算器 源代码(JAVA)
2009-08-20 18:04
337 查看
package gui.mahe;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Counter extends JFrame {
private JButton btn0;// 归零按钮
private JTextField txtField;// 定义文本框
private double num1, num2;// 存储操作数1和2
private boolean end, add, sub, mul, exc;// 存储运算符号.
private String string;// 存储要输入到文本框中的内容
private String charString[] = { "7", "8", "9", "/", "4", "5", "6", "*",
"1", "2", "3", "-", "0", ".", "=", "+" };// 定义一个字符串数组
private JButton buttons[];// 存储计算器上的按钮.
private Container cpContainer;// 定义窗口容器
private JPanel panel;// 定义二级容器。
private JPanel panel1;
public Counter() {
super("计算器");// 窗口的名称
init();// 调用组件实例化方法
this.setSize(450, 400);// 定义窗口的大小.
this.setVisible(true);// 定义窗口的可见性.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭程序时整个进程全部关闭.
}
// 定义组件的实现方法
private void init() {
txtField = new JTextField("", 30);// 实例化单行文本框
panel = new JPanel();// 实例化二级容器
panel1 = new JPanel();
add();
btn0 = new JButton("归零");
MyAction mc = new MyAction();// 实例化一个监听器对象
// 注册数字按钮的监听器接口
for (int i = 0; i < buttons.length; i++) {
buttons[i].addActionListener(mc);
}
btn0.addActionListener(mc);
panel1.add(txtField);
panel1.add(btn0);
cpContainer = this.getContentPane();// 实例化窗口容器.
cpContainer.setLayout(new BorderLayout());// 设置窗口的布局管理器为框架管理器.
cpContainer.add(panel1, BorderLayout.NORTH);// 为添加的组件设置布局的具体位置.
cpContainer.add(panel, BorderLayout.CENTER);// 将二级容器添加到窗口中.
}
//向Panl中添加按钮
private void add() {
panel.setLayout(new GridLayout(4, 4));// 设置二级容器的布局管理器为网格管理器.
buttons = new JButton[charString.length];// 实例化按钮数组.
// 向二级容器中添加按钮组件.
for (int i = 0; i < charString.length; i++) {
buttons[i] = new JButton(charString[i]);
panel.add(buttons[i]);
}
}
// 定义监听器
private class MyAction implements ActionListener {
// 判断所做的运算
public void signs(int s) {
if (s == 1) {// 加法运算
add = true;
sub = false;
mul = false;
exc = false;
} else if (s == 2) {// 减法运算
add = false;
sub = true;
mul = false;
exc = false;
} else if (s == 3) {// 乘法运算
add = false;
sub = false;
mul = true;
exc = false;
} else if (s == 4) {// 除法运算
add = false;
sub = false;
mul = false;
exc = true;
}
num1 = Double.parseDouble(txtField.getText());// 用包装器完成数值类型的转换
end = true;// 继续接收键盘输入的数字。
}
// 判别文本框中的内容
public void number(int i) {
String s = "0";// 定义字符串变量存储文本框中要输入的信息。
s = String.valueOf(i);// 向文本框中输入结果。
if (end) {// 如果数字输入结束,则文本框回复初值0
txtField.setText("0");
end = false;
}
if ((txtField.getText()).equals(0)) {// 判断如果文本框中的字符串为零,则用s替代0
txtField.setText(s);
} else {// 如果文本框中的内容为零的话,则将字符串迭加起来。
string = txtField.getText() + s;
txtField.setText(string);
}
}
// 获取事件源中的值,将其添加到文本框中,可以在文本框中输入的结果事件源
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[0]) {
txtField.setText(charString[0]);
number(7);
} else if (e.getSource() == buttons[1]) {
number(8);
} else if (e.getSource() == buttons[2]) {
number(9);
} else if (e.getSource() == buttons[4]) {
number(4);
} else if (e.getSource() == buttons[5]) {
number(5);
} else if (e.getSource() == buttons[6]) {
number(6);
} else if (e.getSource() == buttons[8]) {
number(1);
} else if (e.getSource() == buttons[9]) {
number(2);
} else if (e.getSource() == buttons[10]) {
number(3);
} else if (e.getSource() == buttons[12]) {
number(0);
} else if (e.getSource() == buttons[15]) {
signs(1);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[11]) {
signs(2);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[7]) {
signs(3);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[3]) {
signs(4);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[14]) {
num2 = Double.parseDouble(txtField.getText());
if (add) {
num1 += num2;
} else if (sub) {
num1 -= num2;
} else if (mul) {
num1 *= num2;
} else if (exc) {
if (num2 == 0) {
txtField.setText("0");
System.out.println("除数不能为零");
} else {
num1 /= num2;
}
}
txtField.setText(String.valueOf(num1));
end = true;
} else if (e.getSource() == buttons[13]) {
string = txtField.getText();
string += ".";
txtField.setText(string);
buttons[13].setEnabled(false);// 禁止俩次输入点好
} else if (e.getSource() == btn0) {
txtField.setText("0");
}
}
}
}
以上代码仅供学习参考
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Counter extends JFrame {
private JButton btn0;// 归零按钮
private JTextField txtField;// 定义文本框
private double num1, num2;// 存储操作数1和2
private boolean end, add, sub, mul, exc;// 存储运算符号.
private String string;// 存储要输入到文本框中的内容
private String charString[] = { "7", "8", "9", "/", "4", "5", "6", "*",
"1", "2", "3", "-", "0", ".", "=", "+" };// 定义一个字符串数组
private JButton buttons[];// 存储计算器上的按钮.
private Container cpContainer;// 定义窗口容器
private JPanel panel;// 定义二级容器。
private JPanel panel1;
public Counter() {
super("计算器");// 窗口的名称
init();// 调用组件实例化方法
this.setSize(450, 400);// 定义窗口的大小.
this.setVisible(true);// 定义窗口的可见性.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭程序时整个进程全部关闭.
}
// 定义组件的实现方法
private void init() {
txtField = new JTextField("", 30);// 实例化单行文本框
panel = new JPanel();// 实例化二级容器
panel1 = new JPanel();
add();
btn0 = new JButton("归零");
MyAction mc = new MyAction();// 实例化一个监听器对象
// 注册数字按钮的监听器接口
for (int i = 0; i < buttons.length; i++) {
buttons[i].addActionListener(mc);
}
btn0.addActionListener(mc);
panel1.add(txtField);
panel1.add(btn0);
cpContainer = this.getContentPane();// 实例化窗口容器.
cpContainer.setLayout(new BorderLayout());// 设置窗口的布局管理器为框架管理器.
cpContainer.add(panel1, BorderLayout.NORTH);// 为添加的组件设置布局的具体位置.
cpContainer.add(panel, BorderLayout.CENTER);// 将二级容器添加到窗口中.
}
//向Panl中添加按钮
private void add() {
panel.setLayout(new GridLayout(4, 4));// 设置二级容器的布局管理器为网格管理器.
buttons = new JButton[charString.length];// 实例化按钮数组.
// 向二级容器中添加按钮组件.
for (int i = 0; i < charString.length; i++) {
buttons[i] = new JButton(charString[i]);
panel.add(buttons[i]);
}
}
// 定义监听器
private class MyAction implements ActionListener {
// 判断所做的运算
public void signs(int s) {
if (s == 1) {// 加法运算
add = true;
sub = false;
mul = false;
exc = false;
} else if (s == 2) {// 减法运算
add = false;
sub = true;
mul = false;
exc = false;
} else if (s == 3) {// 乘法运算
add = false;
sub = false;
mul = true;
exc = false;
} else if (s == 4) {// 除法运算
add = false;
sub = false;
mul = false;
exc = true;
}
num1 = Double.parseDouble(txtField.getText());// 用包装器完成数值类型的转换
end = true;// 继续接收键盘输入的数字。
}
// 判别文本框中的内容
public void number(int i) {
String s = "0";// 定义字符串变量存储文本框中要输入的信息。
s = String.valueOf(i);// 向文本框中输入结果。
if (end) {// 如果数字输入结束,则文本框回复初值0
txtField.setText("0");
end = false;
}
if ((txtField.getText()).equals(0)) {// 判断如果文本框中的字符串为零,则用s替代0
txtField.setText(s);
} else {// 如果文本框中的内容为零的话,则将字符串迭加起来。
string = txtField.getText() + s;
txtField.setText(string);
}
}
// 获取事件源中的值,将其添加到文本框中,可以在文本框中输入的结果事件源
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttons[0]) {
txtField.setText(charString[0]);
number(7);
} else if (e.getSource() == buttons[1]) {
number(8);
} else if (e.getSource() == buttons[2]) {
number(9);
} else if (e.getSource() == buttons[4]) {
number(4);
} else if (e.getSource() == buttons[5]) {
number(5);
} else if (e.getSource() == buttons[6]) {
number(6);
} else if (e.getSource() == buttons[8]) {
number(1);
} else if (e.getSource() == buttons[9]) {
number(2);
} else if (e.getSource() == buttons[10]) {
number(3);
} else if (e.getSource() == buttons[12]) {
number(0);
} else if (e.getSource() == buttons[15]) {
signs(1);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[11]) {
signs(2);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[7]) {
signs(3);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[3]) {
signs(4);
buttons[13].setEnabled(true);
} else if (e.getSource() == buttons[14]) {
num2 = Double.parseDouble(txtField.getText());
if (add) {
num1 += num2;
} else if (sub) {
num1 -= num2;
} else if (mul) {
num1 *= num2;
} else if (exc) {
if (num2 == 0) {
txtField.setText("0");
System.out.println("除数不能为零");
} else {
num1 /= num2;
}
}
txtField.setText(String.valueOf(num1));
end = true;
} else if (e.getSource() == buttons[13]) {
string = txtField.getText();
string += ".";
txtField.setText(string);
buttons[13].setEnabled(false);// 禁止俩次输入点好
} else if (e.getSource() == btn0) {
txtField.setText("0");
}
}
}
}
以上代码仅供学习参考
相关文章推荐
- java swing开发最简易计算器源代码
- 我的第一个安卓小工具-计算器 希望各路大神指点!!正文是java源代码
- 第二十三篇 Java如何编译 计算器 的源代码共享
- 计算器 java源代码
- JAVA编写的计算器源代码
- Java实现简易聊天软件_类QQ(含课程论文、实施计划书、演示视频、答辩PPT、源代码及运行环境)
- Java计算器的源代码
- Java+NetBeans实现简易的计算器
- Java编写的计算器程序及源代码
- 简易五子棋详细Java源代码(1)
- java编写的计算器源代码
- java 做的简易进制计算器
- java代码实例 使用switch实现简易的计算器(实现加减乘除)
- java编写计算器小应用源代码
- 计算器 java源代码
- Java编写的计算器程序及源代码
- 用java编写计算器的源代码,,,
- java代码实例 使用switch实现简易的计算器(实现加减乘除)
- 计算器java源代码
- 计算器JAVA源代码