用JAVA写一个计算器 要求有图形界面。简单的加减乘除,不要小数点运算。

如题所述

package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 0.0; // 计算的中间结果
private boolean operateValidFlag = true; //判断操作是否合法
private JLabel stuno = new JLabel("计算器"); //学号
private JLabel stuname = new JLabel("计算器"); //姓名
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 320);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//设置布局
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(stuno,BorderLayout.WEST);
topPanel.add(stuname,BorderLayout.EAST);
text_double=new JTextField("0",20);//设置文本区
text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
topPanel.add(text_double,BorderLayout.SOUTH);
this.getContentPane().add(topPanel,BorderLayout.NORTH);//将文本区添加到Content北部

JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;i<button.length;i++)//在面板上添加按钮
panel.add(button[i]);

for(int i=0;i<button.length;i++)
button[i].addActionListener(this);//为按钮注册
text_double.setEditable(false);//文本框不可编辑
text_double.addActionListener(this);//

this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回与此动作相关的命令字符串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用户按了“C”键
}
else if (c.equals("CE")) // 用户按了"CE"键
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_double.setText(button);
} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_double.setText(text_double.getText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {

if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
// 操作不合法
operateValidFlag = false;
text_double.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("+")){
// 加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
// 减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
// 乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
// 平方根运算
if(getNumberFromText()<0){
operateValidFlag = false;
text_double.setText("被开方数不能为负数");}
else
resultNum = Math.sqrt(resultNum);
}
else if (operator.equals("+/-")){
// 正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_double.setText(String.valueOf(t1));
} else {
text_double.setText(String.valueOf(resultNum));
}
}
operator = button; //运算符等于用户按的按钮
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0;
try {
result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-12-01
支持连续运算得

import java.awt.BorderLayout;
import java.awt.Button;
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.WindowEvent;
import java.awt.event.WindowListener;

public class Calculator {

public static void main(String[] args) {
Frame f = new Frame("Calculator");

final TextField tf = new TextField();
Panel p = new Panel();

f.setLayout(new BorderLayout());
f.add(tf, BorderLayout.NORTH);
f.add(p, BorderLayout.CENTER);

p.setLayout(new GridLayout(4, 4));

Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button add = new Button("+");
Button sub = new Button("-");
Button mul = new Button("*");
Button div = new Button("/");
Button decimal = new Button(".");
final Button equal = new Button("=");

p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(sub);
p.add(mul);
p.add(div);
p.add(decimal);
p.add(equal);

final String[] ary = {"", null, ""};

class NumMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
String num = ((Button)e.getSource()).getLabel().trim();
if(ary[1] == null){
ary[0] = ary[0] + "" + num;
}else{
ary[2] = ary[2] + "" + num;
}

tf.setText(tf.getText() + "" + num);
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

}

one.addMouseListener(new NumMouseListener());
two.addMouseListener(new NumMouseListener());
three.addMouseListener(new NumMouseListener());
four.addMouseListener(new NumMouseListener());
five.addMouseListener(new NumMouseListener());
six.addMouseListener(new NumMouseListener());
seven.addMouseListener(new NumMouseListener());
eight.addMouseListener(new NumMouseListener());
nine.addMouseListener(new NumMouseListener());
zero.addMouseListener(new NumMouseListener());

class OperatotrMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(e.getSource() == equal){
try {
long result = calculate(Integer.parseInt(ary[0]), Integer.parseInt(ary[2]), ary[1]);
tf.setText(String.valueOf(result));

ary[0] = tf.getText();
ary[1] = null;
ary[2] = "";
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
ary[1] = ((Button) e.getSource()).getLabel();

if(ary[0].length() > 0){
ary[2] = "";
}else{
ary[0] = String.valueOf(tf.getText());
}

tf.setText("");
}
}

private long calculate(int i, int j, String operator) throws Exception {
if(operator.equals("+")){
return i + j;
}else if(operator.equals("-")){
return i - j;
}else if(operator.equals("*")){
return i * j;
}else if(operator.equals("/")){
return i / j;
}else{
throw new Exception("Invalid operator.(Only +-/* accepted");
}
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

add.addMouseListener(new OperatotrMouseListener());
mul.addMouseListener(new OperatotrMouseListener());
div.addMouseListener(new OperatotrMouseListener());
sub.addMouseListener(new OperatotrMouseListener());
equal.addMouseListener(new OperatotrMouseListener());

f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}

public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});

f.setVisible(true);
f.setBounds(200, 200, 250, 250);
f.validate();
}

}本回答被提问者采纳
第2个回答  2012-03-28
以前写的一个,带C得 import java.awt.BorderLayout; import java.awt.//计算器面板 private JFrame f = //加减乘除面板 private JPanel ,usojUw
第3个回答  2012-03-28
java.swing 框架,百度一下不难
虽然是个比较烂的图形框架

用JAVA写一个计算器 要求有图形界面。简单的加减乘除,不要小数点运算...
private void handleNumber(String button) { if (firstDigit)\/\/输入的第一个数字 { text_double.setText(button);} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))\/\/输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面 \/\/如果字符串...

Java实现有界面的简单的两个整数之间的加减乘除运算
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField;...

用Java编写一个简单的计算器界面,并且可以实现加减乘除计算,很简单一...
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.WindowEvent;import java.awt.event.WindowListener;public class Calculator { public static void main(...

java加减乘除计算器界面编程?
\/\/ 实现四则运算符和点不可挨着出现(点后面可以出现四则运算符,点前面不可出现)}

求简单java写计算器代码加减乘除
dotExist = false; \/\/ 表示当前的数是否有小数点 operated = false; \/\/ 表示任意运算符是否被按下 equaled = false; \/\/ 表示等号是否被按下 storedNumber = 0;lastOperator = '?';\/\/ 初始化窗口变量 operation = new JTextField("0");operation.setEditable(false);numbers = new JButton[10...

用JAVA编写一个简单的计算器,要求如下
以下是上图计算器的代码:package Computer;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Stack;import javax.swing.JApplet;i...

java 编计算器界面
private String num1 = "";\/\/保存第一个运算数字 private String operator = "";\/\/保存运算符号 public static void main(String[] args) { new Calculator();\/\/new计算器实例 } public Calculator(){ \/\/添加组件,布局 inputPanel.add(input);f.add(inputPanel, BorderLayout.NORTH);operation...

用JAVA写了一个计算机的框架,怎么加代码实现基本运算(加减乘除,等于,清...
\/\/ 构造监听器 class Bt implements ActionListener { public void actionPerformed(ActionEvent e2) { try{ if(e2.getSource()==b1)\/\/ 选择"CE"清零 { tf1.setText("0");\/\/ 把显示屏清零 str.setLength(0);\/\/ 清空字符串缓冲区以准备接收新的输入运算数 } else if(e2.getSource()==b2)\/\/ 选择"C...

求JAVA编写的 简易计算器程序,附上注释
1、首先准备好软件即eclipse和java,下载安装完成后打开eclipse。2、点击左上角的file 新建一个project。3、给project取一个名字,其他的选项都是默认然后点击finish。4、接下来是新建一个class。5、在给class取名字的时候注意用英文名的首字母要大写。完成后点击finish。6、这就是开始写代码的工作台,将...

如何使用Java对象语言编写一个加减乘除计算器要有代码
import java.awt.event.*;public class JiSuanQi { String s="",s1=null,s2=null;Frame f=new Frame("计算器");TextField tf=new TextField(30);Panel p1=new Panel();Panel p2=new Panel();Panel p3=new Panel();Button bt1=new Button("=");Button bt2=new Button("删除");Button[...

相似回答