求一个JAVA计算器程序 20
计算器模拟程序功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。界面要示:用图形界面实现。类似正方形的那种图程序最好有详细的注释哦!...
计算器模拟程序
功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。
界面要示:用图形界面实现。类似正方形的那种图
程序最好有详细的注释哦! 展开
功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。
界面要示:用图形界面实现。类似正方形的那种图
程序最好有详细的注释哦! 展开
展开全部
package com.vogoal.util;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Button;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JPanel jPanel = null;
private JButton jButton1 = null;
/**
* This is the default constructor
*/
public Demo() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);//窗体的大小
this.setContentPane(getJPanel());//附加模板
this.setTitle("小小计算器");//窗体左上角名称
}
public void main(String[] args) {
initialize();
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
public String b = ""; // @jve:decl-index=0://文本框初始化
Map m = new HashMap(); // @jve:decl-index=0:
private JPanel getJPanel() {
if (jPanel == null) {
GridLayout gridLayout = new GridLayout(5, 4);//布局方式
gridLayout.setColumns(4);//设置4列
gridLayout.setHgap(2);//水平间距
gridLayout.setVgap(0);//垂直间距
gridLayout.setRows(5);//5行
GridBagConstraints gridBagConstraints = new GridBagConstraints();//组件的主要父类(这个不好解释)
gridBagConstraints.gridx = 0;//组件的x坐标
gridBagConstraints.gridy = 0;//组件的y坐标
jPanel = new JPanel();//声明panel
Button btn[]; // 声明按钮数组
final String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9" };
btn = new Button[str.length]; // 创建按钮数组
final JTextField jtf = new JTextField();//文本
jPanel.setLayout(gridLayout);//组建添加
jPanel.add(jtf);//将文本添加到jPanel
//下面是声明加减乘除点还有清零按钮
final Button bu2 = new Button("-");
final Button bu3 = new Button("+");
final Button bu4 = new Button("/");
final Button dian = new Button(".");
final Button bu5 = new Button("=");
final Button buexit = new Button("clear");
final Button bu1 = new Button("*");
//循环出0-9数字的按钮
for (int i = 0; i < str.length; i++) {
btn[i] = new Button(str[i]);
final String a = str[i];
//给每个按钮添加单击事件
btn[i].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
b += a;
jtf.setText(b);//将点击按钮对应的数字显示在文本框中
//在没点击运算符号时,并且点击点 将值存入map
//这里就是处理出现输去连续显示问题(你可以去掉看看效果)
if (m.get("fuhao") == null
|| m.get("fuhao").equals(".")) {
m.put("num1", b);
} else {
m.put("num2", b);
}
}
});
jPanel.add(btn[i]);
}
jPanel.add(bu1);
jPanel.add(bu2);
jPanel.add(bu3);
jPanel.add(bu4);
jPanel.add(bu5);
jPanel.add(dian);
bu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "*");
jtf.setText("");
b = "";
}
});
bu3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "+");
jtf.setText("");
b = "";
}
});
dian.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
b += ".";
m.put("num1", a);
}
b += ".";
m.put("num2", b);
}
});
bu4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "/");
jtf.setText("");
b = "";
}
});
bu5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (m.get("num1") != null && m.get("num2") != null
&& m.get("fuhao") != null)
{
double num1 = Double.parseDouble(m.get("num1")
.toString());
double num2 = Double.parseDouble(m.get("num2")
.toString());
if (m.get("fuhao").equals("*")) {
jtf.setText(num1 * num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("+")) {
jtf.setText(num1 + num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("-")) {
jtf.setText(num1 - num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("/")) {
if (m.get("num2").equals("0")) {
jtf.setText(m.get("num1").toString());
} else {
jtf.setText(num1 / num2 + "");
}
m = new HashMap();
}
}
}
});
bu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "-");
jtf.setText("");
b = "";
}
});
buexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == buexit) {
jtf.setText("0");
b = "";
}
}
});
jPanel.add(buexit);
}
return jPanel;
}
}
刚刚做的、你看看吧!第一个事件中加了注释、下面的就是重复了、好运哦!~
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Button;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JPanel jPanel = null;
private JButton jButton1 = null;
/**
* This is the default constructor
*/
public Demo() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);//窗体的大小
this.setContentPane(getJPanel());//附加模板
this.setTitle("小小计算器");//窗体左上角名称
}
public void main(String[] args) {
initialize();
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
public String b = ""; // @jve:decl-index=0://文本框初始化
Map m = new HashMap(); // @jve:decl-index=0:
private JPanel getJPanel() {
if (jPanel == null) {
GridLayout gridLayout = new GridLayout(5, 4);//布局方式
gridLayout.setColumns(4);//设置4列
gridLayout.setHgap(2);//水平间距
gridLayout.setVgap(0);//垂直间距
gridLayout.setRows(5);//5行
GridBagConstraints gridBagConstraints = new GridBagConstraints();//组件的主要父类(这个不好解释)
gridBagConstraints.gridx = 0;//组件的x坐标
gridBagConstraints.gridy = 0;//组件的y坐标
jPanel = new JPanel();//声明panel
Button btn[]; // 声明按钮数组
final String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9" };
btn = new Button[str.length]; // 创建按钮数组
final JTextField jtf = new JTextField();//文本
jPanel.setLayout(gridLayout);//组建添加
jPanel.add(jtf);//将文本添加到jPanel
//下面是声明加减乘除点还有清零按钮
final Button bu2 = new Button("-");
final Button bu3 = new Button("+");
final Button bu4 = new Button("/");
final Button dian = new Button(".");
final Button bu5 = new Button("=");
final Button buexit = new Button("clear");
final Button bu1 = new Button("*");
//循环出0-9数字的按钮
for (int i = 0; i < str.length; i++) {
btn[i] = new Button(str[i]);
final String a = str[i];
//给每个按钮添加单击事件
btn[i].addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
b += a;
jtf.setText(b);//将点击按钮对应的数字显示在文本框中
//在没点击运算符号时,并且点击点 将值存入map
//这里就是处理出现输去连续显示问题(你可以去掉看看效果)
if (m.get("fuhao") == null
|| m.get("fuhao").equals(".")) {
m.put("num1", b);
} else {
m.put("num2", b);
}
}
});
jPanel.add(btn[i]);
}
jPanel.add(bu1);
jPanel.add(bu2);
jPanel.add(bu3);
jPanel.add(bu4);
jPanel.add(bu5);
jPanel.add(dian);
bu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "*");
jtf.setText("");
b = "";
}
});
bu3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "+");
jtf.setText("");
b = "";
}
});
dian.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
b += ".";
m.put("num1", a);
}
b += ".";
m.put("num2", b);
}
});
bu4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "/");
jtf.setText("");
b = "";
}
});
bu5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (m.get("num1") != null && m.get("num2") != null
&& m.get("fuhao") != null)
{
double num1 = Double.parseDouble(m.get("num1")
.toString());
double num2 = Double.parseDouble(m.get("num2")
.toString());
if (m.get("fuhao").equals("*")) {
jtf.setText(num1 * num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("+")) {
jtf.setText(num1 + num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("-")) {
jtf.setText(num1 - num2 + "");
m = new HashMap();
} else if (m.get("fuhao").equals("/")) {
if (m.get("num2").equals("0")) {
jtf.setText(m.get("num1").toString());
} else {
jtf.setText(num1 / num2 + "");
}
m = new HashMap();
}
}
}
});
bu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
double a = Double.parseDouble(jtf.getText());
if (m.get("num1") == null) {
m.put("num1", a);
}
m.put("fuhao", "-");
jtf.setText("");
b = "";
}
});
buexit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (e.getSource() == buexit) {
jtf.setText("0");
b = "";
}
}
});
jPanel.add(buexit);
}
return jPanel;
}
}
刚刚做的、你看看吧!第一个事件中加了注释、下面的就是重复了、好运哦!~
展开全部
正方形的,实现数字加、减、乘、除四则运算。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame f = new JFrame("Calculator");
private JPanel inputPanel = new JPanel();
private JPanel operationPanel = new JPanel();
private JPanel buttonsPanel = new JPanel();
private JTextField input = new JTextField(20);
private JButton backspace = new JButton("BackSpace");
private JButton CE = new JButton("CE ");
private JButton C = new JButton("C ");
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
private JButton decimal = new JButton(".");
private JButton equal = new JButton("=");
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";
private String operator = "";
public static void main(String[] args) {
new Calculator();
}
public Calculator(){
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){
input.setText(((JButton)e.getSource()).getText().trim());
}else{
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){
String inputText = input.getText();
if(inputText.length() > 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");
}else if(e.getSource() == decimal){
String text = input.getText().trim();
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
private JFrame f = new JFrame("Calculator");
private JPanel inputPanel = new JPanel();
private JPanel operationPanel = new JPanel();
private JPanel buttonsPanel = new JPanel();
private JTextField input = new JTextField(20);
private JButton backspace = new JButton("BackSpace");
private JButton CE = new JButton("CE ");
private JButton C = new JButton("C ");
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
private JButton decimal = new JButton(".");
private JButton equal = new JButton("=");
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";
private String operator = "";
public static void main(String[] args) {
new Calculator();
}
public Calculator(){
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){
input.setText(((JButton)e.getSource()).getText().trim());
}else{
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){
String inputText = input.getText();
if(inputText.length() > 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");
}else if(e.getSource() == decimal){
String text = input.getText().trim();
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
更多追问追答
追问
咱能把注释弄详细些吗?谢谢你啊
追答
基本没什么需要注视的拉。
思路很简单
当你按了运算符号,先保存第一个运算数字,保存运算符号。然后输入第二个数字,然后按了等于就把2个数字运算。
譬如你第一次输入了3
然后按*, 那就保存a = 3, b = "*"
再次输入5,另外一个变量保存c = 5
按了等号,结果设置为 3*5=15
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package net.webwork.hello.vo;
import java.awt.BorderLayout;
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 Calculator extends JFrame {
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton jiaButton = new JButton("+");
JButton jianButton = new JButton("-");
JButton chengButton = new JButton("*");
JButton chuButton = new JButton("/");
JButton yuButton = new JButton("%");
JButton jjButton = new JButton("+/-");
JButton sqrtButton = new JButton("sqrt");
JButton dianButton = new JButton(".");
JButton dengButton = new JButton("=");
JButton daoButton = new JButton("1/x");
JButton backButton = new JButton("Backpace");
JButton cButton = new JButton("C");
public double op1;
public double op2;
public static final int JIA = 0;
public static final int JIAN = 1;
public static final int CHENG = 2;
public static final int CHU = 3;
public static final int JJ = 4;
public static final int DIAN = 5;
public int current0p = 0;
private boolean opEnd = false;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JTextField result = new JTextField(20);
public Calculator() {
initPanel2();
initPanel3();
panel2.setLayout(new GridLayout(5, 4));
panel1.setLayout(new BorderLayout());
panel1.add(panel3, BorderLayout.NORTH);// 设置位置
panel1.add(panel2, BorderLayout.CENTER);// 设置位置
getContentPane().add(panel1);
addActionListeners();
setSize(260, 260);
setLocation(500, 300);
setVisible(true);
setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("计算器");
}
private void initPanel2() {
// 把组件添加到相应的panel上
panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(chuButton);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(chengButton);
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(jianButton);
panel2.add(b0);
panel2.add(jjButton);
panel2.add(dianButton);
panel2.add(jiaButton);
panel2.add(daoButton);
panel2.add(yuButton);
panel2.add(sqrtButton);
panel2.add(dengButton);
}
private void addActionListeners() {
ActionHandler c = new ActionHandler();
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);
jiaButton.addActionListener(c);
dengButton.addActionListener(c);
chengButton.addActionListener(c);
chuButton.addActionListener(c);
jianButton.addActionListener(c);
jjButton.addActionListener(c);
dianButton.addActionListener(c);
sqrtButton.addActionListener(c);
yuButton.addActionListener(c);
daoButton.addActionListener(c);
backButton.addActionListener(c);
cButton.addActionListener(c);
}
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b0) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "0");
}
if (e.getSource() == b1) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "1");
opEnd = true;
}
if (e.getSource() == b2) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "2");
opEnd = true;
}
if (e.getSource() == b3) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "3");
opEnd = true;
}
if (e.getSource() == b4) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "4");
opEnd = true;
}
if (e.getSource() == b5) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "5");
opEnd = true;
}
if (e.getSource() == b6) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "6");
opEnd = true;
}
if (e.getSource() == b7) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "7");
opEnd = true;
}
if (e.getSource() == b8) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "8");
opEnd = true;
}
if (e.getSource() == b9) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "9");
opEnd = true;
}
try {
if (e.getSource() == jiaButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = JIA;
}
if (e.getSource() == chengButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = CHENG;
}
if (e.getSource() == chuButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = CHU;
}
if (e.getSource() == jianButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = JIAN;
}
if (e.getSource() == jjButton) {
String tmp = result.getText();
if (tmp.equals("") || tmp.equals("0")) {
return;
}
if (tmp.charAt(0) == '-') {
tmp = tmp.substring(1);
} else {
tmp = '-' + tmp;
}
result.setText(tmp);
}
if (e.getSource() == dianButton) {
String tmp = result.getText();
if (tmp.equals("")) {
return;
}
if (tmp.indexOf(".") != -1) {
return;
}
tmp = tmp + ".";
result.setText(tmp);
}
if (e.getSource() == sqrtButton) {
String tmp = result.getText();
if (tmp.equals(" ")) {
return;
}
double d;
d = Double.parseDouble(tmp);// 先定义一个double类型的d
if (d < 0) {
result.setText("不能对负数求平方根!");
return;
}
op2 = Math.sqrt(d);
result.setText(op2 + "");
}
if (e.getSource() == backButton) {
String s = result.getText();
result.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
result.setText(result.getText() + a);
}
}
if (e.getSource() == cButton) {
result.setText("0");
opEnd = false;
}
if (e.getSource() == dengButton) {
op2 = Double.parseDouble(result.getText());
switch (current0p) {
case JIA:
result.setText(op1 + op2 + "");
break;
case JIAN:
result.setText(op1 - op2 + "");
break;
case CHENG:
result.setText(op1 * op2 + "");
break;
case CHU:
if (op2 == 0) {
result.setText("被除数不能为零!");
break;
}
result.setText(op1 / op2 + "");
break;
}
opEnd = false;
}
} catch (Exception e1) {
result.setText("Wrong");
opEnd = false;
}
}
}
private void initPanel3() {
panel3.setLayout(new GridLayout(2, 1));
panel3.add(result);
panel3.add(panel4);
panel4.setLayout(new GridLayout(1, 2));
panel4.add(backButton);
panel4.add(cButton);
// panel3.setPreferredSize(new Dimension(260,65));
}
public static void main(String[] args) {
Calculator c = new Calculator();// 生成类的实例
}
}
import java.awt.BorderLayout;
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 Calculator extends JFrame {
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton jiaButton = new JButton("+");
JButton jianButton = new JButton("-");
JButton chengButton = new JButton("*");
JButton chuButton = new JButton("/");
JButton yuButton = new JButton("%");
JButton jjButton = new JButton("+/-");
JButton sqrtButton = new JButton("sqrt");
JButton dianButton = new JButton(".");
JButton dengButton = new JButton("=");
JButton daoButton = new JButton("1/x");
JButton backButton = new JButton("Backpace");
JButton cButton = new JButton("C");
public double op1;
public double op2;
public static final int JIA = 0;
public static final int JIAN = 1;
public static final int CHENG = 2;
public static final int CHU = 3;
public static final int JJ = 4;
public static final int DIAN = 5;
public int current0p = 0;
private boolean opEnd = false;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JTextField result = new JTextField(20);
public Calculator() {
initPanel2();
initPanel3();
panel2.setLayout(new GridLayout(5, 4));
panel1.setLayout(new BorderLayout());
panel1.add(panel3, BorderLayout.NORTH);// 设置位置
panel1.add(panel2, BorderLayout.CENTER);// 设置位置
getContentPane().add(panel1);
addActionListeners();
setSize(260, 260);
setLocation(500, 300);
setVisible(true);
setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("计算器");
}
private void initPanel2() {
// 把组件添加到相应的panel上
panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(chuButton);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(chengButton);
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(jianButton);
panel2.add(b0);
panel2.add(jjButton);
panel2.add(dianButton);
panel2.add(jiaButton);
panel2.add(daoButton);
panel2.add(yuButton);
panel2.add(sqrtButton);
panel2.add(dengButton);
}
private void addActionListeners() {
ActionHandler c = new ActionHandler();
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);
jiaButton.addActionListener(c);
dengButton.addActionListener(c);
chengButton.addActionListener(c);
chuButton.addActionListener(c);
jianButton.addActionListener(c);
jjButton.addActionListener(c);
dianButton.addActionListener(c);
sqrtButton.addActionListener(c);
yuButton.addActionListener(c);
daoButton.addActionListener(c);
backButton.addActionListener(c);
cButton.addActionListener(c);
}
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b0) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "0");
}
if (e.getSource() == b1) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "1");
opEnd = true;
}
if (e.getSource() == b2) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "2");
opEnd = true;
}
if (e.getSource() == b3) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "3");
opEnd = true;
}
if (e.getSource() == b4) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "4");
opEnd = true;
}
if (e.getSource() == b5) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "5");
opEnd = true;
}
if (e.getSource() == b6) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "6");
opEnd = true;
}
if (e.getSource() == b7) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "7");
opEnd = true;
}
if (e.getSource() == b8) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "8");
opEnd = true;
}
if (e.getSource() == b9) {
if (opEnd == false) {
result.setText("");
}
result.setText(result.getText() + "9");
opEnd = true;
}
try {
if (e.getSource() == jiaButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = JIA;
}
if (e.getSource() == chengButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = CHENG;
}
if (e.getSource() == chuButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = CHU;
}
if (e.getSource() == jianButton) {
op1 = Double.parseDouble(result.getText());
// 2、说明操作数已经输入完毕
opEnd = false;
current0p = JIAN;
}
if (e.getSource() == jjButton) {
String tmp = result.getText();
if (tmp.equals("") || tmp.equals("0")) {
return;
}
if (tmp.charAt(0) == '-') {
tmp = tmp.substring(1);
} else {
tmp = '-' + tmp;
}
result.setText(tmp);
}
if (e.getSource() == dianButton) {
String tmp = result.getText();
if (tmp.equals("")) {
return;
}
if (tmp.indexOf(".") != -1) {
return;
}
tmp = tmp + ".";
result.setText(tmp);
}
if (e.getSource() == sqrtButton) {
String tmp = result.getText();
if (tmp.equals(" ")) {
return;
}
double d;
d = Double.parseDouble(tmp);// 先定义一个double类型的d
if (d < 0) {
result.setText("不能对负数求平方根!");
return;
}
op2 = Math.sqrt(d);
result.setText(op2 + "");
}
if (e.getSource() == backButton) {
String s = result.getText();
result.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
result.setText(result.getText() + a);
}
}
if (e.getSource() == cButton) {
result.setText("0");
opEnd = false;
}
if (e.getSource() == dengButton) {
op2 = Double.parseDouble(result.getText());
switch (current0p) {
case JIA:
result.setText(op1 + op2 + "");
break;
case JIAN:
result.setText(op1 - op2 + "");
break;
case CHENG:
result.setText(op1 * op2 + "");
break;
case CHU:
if (op2 == 0) {
result.setText("被除数不能为零!");
break;
}
result.setText(op1 / op2 + "");
break;
}
opEnd = false;
}
} catch (Exception e1) {
result.setText("Wrong");
opEnd = false;
}
}
}
private void initPanel3() {
panel3.setLayout(new GridLayout(2, 1));
panel3.add(result);
panel3.add(panel4);
panel4.setLayout(new GridLayout(1, 2));
panel4.add(backButton);
panel4.add(cButton);
// panel3.setPreferredSize(new Dimension(260,65));
}
public static void main(String[] args) {
Calculator c = new Calculator();// 生成类的实例
}
}
追问
拜托拉,注释弄的详细些撒^谢谢哦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
仅供参考
package js;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Js extends WindowAdapter implements MouseListener {
private String first;
private String second;
private String operator;
private Button zero = new Button("0");
private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button decimal = new Button(".");
private Button equal = new Button("=");
private Button add = new Button("+");
private Button sub = new Button("-");
private Button mul = new Button("*");
private Button div = new Button("/");
private TextField input = new TextField();
private Button CE = new Button("CE");
private Button DEL = new Button("Del");
public static void main(String[] args){
new Js();
}
public Js(){
Frame f = new Frame("Calculator");
f.add("North", input);
Panel keys = new Panel();
f.add(keys, "Center");
keys.setLayout(new GridLayout(4, 4));
keys.add(seven);
keys.add(eight);
keys.add(nine);
keys.add(add);
keys.add(four);
keys.add(five);
keys.add(six);
keys.add(sub);
keys.add(one);
keys.add(two);
keys.add(three);
keys.add(mul);
keys.add(zero);
keys.add(decimal);
keys.add(equal);
keys.add(div);
zero.addMouseListener(this);
one.addMouseListener(this);
two.addMouseListener(this);
three.addMouseListener(this);
four.addMouseListener(this);
five.addMouseListener(this);
six.addMouseListener(this);
seven.addMouseListener(this);
eight.addMouseListener(this);
nine.addMouseListener(this);
add.addMouseListener(this);
sub.addMouseListener(this);
div.addMouseListener(this);
mul.addMouseListener(this);
equal.addMouseListener(this);
decimal.addMouseListener(this);
Panel addtionalPanel = new Panel();
addtionalPanel.setLayout(new GridLayout(1, 2));
addtionalPanel.add(CE);
addtionalPanel.add(DEL);
CE.setBackground(Color.green);
DEL.setBackground(Color.cyan);
CE.addMouseListener(this);
DEL.addMouseListener(this);
f.add("South", addtionalPanel);
f.addWindowListener(this);
f.setVisible(true);
f.setLocation(200, 300);
f.setSize(200, 200);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void mouseClicked(MouseEvent e){
Button btn = (Button)e.getSource();
String key = btn.getActionCommand().trim();
if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){
if(isBlank(operator)){
if(isBlank(first)){
first = key;
}
else{
first += key;
}
input.setText(first);
}
else{
if(isBlank(second)){
second = key;
}
else{
second += key;
}
input.setText(second);
}
}
else if(btn == decimal){
if(isBlank(operator)){
if(isBlank(first)){
first = "0.";
input.setText(first);
}
else{
if(first.indexOf(".") == -1){
first += ".";
input.setText(first);
}
}
}
else{
if(isBlank(second)){
second = "0.";
input.setText(second);
}
else{
if(second.indexOf(".") == -1){
second += ".";
input.setText(second);
}
}
}
}
else if(btn == equal){
if(!isBlank(second) && !isBlank(first) && !isBlank(operator)){
double result = 0.0D;
if(operator.equals("+")){
result = Double.parseDouble(first) + Double.parseDouble(second);
}
else if(operator.equals("-")){
result = Double.parseDouble(first) - Double.parseDouble(second);
}
else if(operator.equals("*")){
result = Double.parseDouble(first) * Double.parseDouble(second);
}
else if(operator.equals("/")){
result = Double.parseDouble(first) / Double.parseDouble(second);
}
int value = (int)Math.round(result);
if(value == result){
input.setText(String.valueOf(value));
}
else{
input.setText(String.valueOf(result));
}
first = String.valueOf(result);
second = null;
operator = null;
}
}
else if(btn == add || btn == sub || btn == div || btn == mul){
if(!isBlank(first)){
if(!isBlank(operator) && !isBlank(second)){
if(operator.equals("+")){
first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second));
}
else if(operator.equals("-")){
first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second));
}
else if(operator.equals("*")){
first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second));
}
else if(operator.equals("/")){
first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second));
}
second = null;
}
operator = key;
}
}
else if(btn == CE){
first = null;
second = null;
operator = null;
input.setText(null);
}
else if(btn == DEL){
if(isBlank(operator)){
if(!isBlank(first)){
first = first.substring(0, first.length() -1);
input.setText(first);
}
}
else{
if(!isBlank(second)){
second = second.substring(0, second.length() -1);
input.setText(second);
}
}
}
}
private boolean isBlank(String str){
return str == null || str.trim().equals("");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
package js;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Js extends WindowAdapter implements MouseListener {
private String first;
private String second;
private String operator;
private Button zero = new Button("0");
private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button decimal = new Button(".");
private Button equal = new Button("=");
private Button add = new Button("+");
private Button sub = new Button("-");
private Button mul = new Button("*");
private Button div = new Button("/");
private TextField input = new TextField();
private Button CE = new Button("CE");
private Button DEL = new Button("Del");
public static void main(String[] args){
new Js();
}
public Js(){
Frame f = new Frame("Calculator");
f.add("North", input);
Panel keys = new Panel();
f.add(keys, "Center");
keys.setLayout(new GridLayout(4, 4));
keys.add(seven);
keys.add(eight);
keys.add(nine);
keys.add(add);
keys.add(four);
keys.add(five);
keys.add(six);
keys.add(sub);
keys.add(one);
keys.add(two);
keys.add(three);
keys.add(mul);
keys.add(zero);
keys.add(decimal);
keys.add(equal);
keys.add(div);
zero.addMouseListener(this);
one.addMouseListener(this);
two.addMouseListener(this);
three.addMouseListener(this);
four.addMouseListener(this);
five.addMouseListener(this);
six.addMouseListener(this);
seven.addMouseListener(this);
eight.addMouseListener(this);
nine.addMouseListener(this);
add.addMouseListener(this);
sub.addMouseListener(this);
div.addMouseListener(this);
mul.addMouseListener(this);
equal.addMouseListener(this);
decimal.addMouseListener(this);
Panel addtionalPanel = new Panel();
addtionalPanel.setLayout(new GridLayout(1, 2));
addtionalPanel.add(CE);
addtionalPanel.add(DEL);
CE.setBackground(Color.green);
DEL.setBackground(Color.cyan);
CE.addMouseListener(this);
DEL.addMouseListener(this);
f.add("South", addtionalPanel);
f.addWindowListener(this);
f.setVisible(true);
f.setLocation(200, 300);
f.setSize(200, 200);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void mouseClicked(MouseEvent e){
Button btn = (Button)e.getSource();
String key = btn.getActionCommand().trim();
if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){
if(isBlank(operator)){
if(isBlank(first)){
first = key;
}
else{
first += key;
}
input.setText(first);
}
else{
if(isBlank(second)){
second = key;
}
else{
second += key;
}
input.setText(second);
}
}
else if(btn == decimal){
if(isBlank(operator)){
if(isBlank(first)){
first = "0.";
input.setText(first);
}
else{
if(first.indexOf(".") == -1){
first += ".";
input.setText(first);
}
}
}
else{
if(isBlank(second)){
second = "0.";
input.setText(second);
}
else{
if(second.indexOf(".") == -1){
second += ".";
input.setText(second);
}
}
}
}
else if(btn == equal){
if(!isBlank(second) && !isBlank(first) && !isBlank(operator)){
double result = 0.0D;
if(operator.equals("+")){
result = Double.parseDouble(first) + Double.parseDouble(second);
}
else if(operator.equals("-")){
result = Double.parseDouble(first) - Double.parseDouble(second);
}
else if(operator.equals("*")){
result = Double.parseDouble(first) * Double.parseDouble(second);
}
else if(operator.equals("/")){
result = Double.parseDouble(first) / Double.parseDouble(second);
}
int value = (int)Math.round(result);
if(value == result){
input.setText(String.valueOf(value));
}
else{
input.setText(String.valueOf(result));
}
first = String.valueOf(result);
second = null;
operator = null;
}
}
else if(btn == add || btn == sub || btn == div || btn == mul){
if(!isBlank(first)){
if(!isBlank(operator) && !isBlank(second)){
if(operator.equals("+")){
first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second));
}
else if(operator.equals("-")){
first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second));
}
else if(operator.equals("*")){
first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second));
}
else if(operator.equals("/")){
first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second));
}
second = null;
}
operator = key;
}
}
else if(btn == CE){
first = null;
second = null;
operator = null;
input.setText(null);
}
else if(btn == DEL){
if(isBlank(operator)){
if(!isBlank(first)){
first = first.substring(0, first.length() -1);
input.setText(first);
}
}
else{
if(!isBlank(second)){
second = second.substring(0, second.length() -1);
input.setText(second);
}
}
}
}
private boolean isBlank(String str){
return str == null || str.trim().equals("");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
追问
拜托,注释能详细些?谢谢哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |