用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现相关的知识,希望对你有一定的参考价值。
在两个文本框中分别输入两个数,要求可以根据需要计算这两个数的和差积商,加减乘除的运算符通过下拉列表选择,按“计算”按钮,把计算结果显示在第三个文本框中。(代码最好能够有注释,我是初学者,有的语句怕看不懂)
我只能做出两个数相加的。。。
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Addition extends Applet implements ActionListener
Label label1=new Label("+");
Label label2=new Label("=");
TextField field1=new TextField(6);
TextField field2=new TextField(6);
TextField field3=new TextField(6);
Button button1=new Button("相加");
public void init() // 初始化
add(field1); add(label1);
add(field2); add(label2);
add(field3); add(button1);
button1.addActionListener(this);
public void actionPerformed(ActionEvent e) // 处理按钮事件
int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x)); // 数值转换为字符串
import java.awt.*;//AWT核心包
import java.awt.event.*;//提供事件类和监听器
public class Counter extends Frame implements ActionListener
TextField t=new TextField("");//文本框
Panel p1=new Panel();//new一个panel,用于存放数字键和符号键。
Panel p2=new Panel();//new一个panel,用于存放开方、平方、和清除键。
Button[] b=new Button[10];//实例化Button对象
Button bAdd=new Button("加");
Button bSub=new Button("减");
Button bMul=new Button("乘以");
Button bPoint=new Button(".");
Button bDiv=new Button("除以");
Button bEqual=new Button("等於");
Button bSqrt=new Button("开方");
Button bPow=new Button("平方");
Button bNull=new Button("清除");
String str1=""; //str1和str2存放两个输入的数
String str2="";
String operator=null; //存放加减乘除以及开平方的符号
boolean first=true; //检验输入的是否为第一个数
int countOper=0; //累计输入符号的个数,连加连减等操作中会用到
double result=0.0; //暂存结果
double num1=0.0,num2=0.0; //两个输入的数做运算时转化为double存放
boolean error=false; //检验除数是否为0
//构造方法
public Counter()
Frame s=new Frame("计算器");//创建Frame
for(int i=0;i<10;i++)//利用for循环将数字键添加进p1中
b[i]=new Button(String.valueOf(i));
p1.add(b[i]);
b[i].setActionCommand("number");
b[i].setForeground(new Color(150,20,20));
b[i].addActionListener(this);//调用addActionListener()方法注册事件监听器
p1.add(bPoint);
bPoint.setActionCommand("number");
p1.add(bAdd); //数字键,符号键放置在panel的p1中
p1.add(bSub);
p1.add(bMul);
p1.add(bDiv);
p1.add(bEqual);
p2.add(bSqrt);//开方键,平方键,清除键放置在panel的p2中
p2.add(bPow);
p2.add(bNull);
bAdd.setActionCommand("oper");
bSub.setActionCommand("oper");
bMul.setActionCommand("oper");
bDiv.setActionCommand("oper");
bAdd.setForeground(Color.red);//为组键设计颜色
bSub.setForeground(Color.red);
bMul.setForeground(Color.red);
bDiv.setForeground(Color.red);
bPoint.setForeground(Color.black);
bEqual.setForeground(Color.red);
bSqrt.setForeground(Color.blue);
bPow.setForeground(Color.blue);
bNull.setForeground(Color.blue);
bAdd.addActionListener(this);//调用addActionListener()方法注册事件监听器
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPoint.addActionListener(this);
bEqual.addActionListener(this);
bSqrt.addActionListener(this);
bPow.addActionListener(this);
bNull.addActionListener(this);
p1.setLayout(new GridLayout(4,4,5,5));//网格布局管理器,把容器根据行数和列数分成同样大小的单元,
//每个单元可容纳一个组件,并且此组件会填满网格单元,不能控
//制其占据网格的大小。4、4为网格的行、列数。5、5为组建之间的
//间距
p2.setLayout(new FlowLayout());//用FlowLayout布局管理器将组建默认剧中排放,默认间隙为5个像素
add(t,"North"); //frame的north放置输入框,panel放置在center和south
add(p1,"Center");//将p1添加到Center中
add(p2,"South");//将p2添加到South中
setLocation(400,200);//设计按钮尺寸
setSize(200,200);//设计窗口尺寸
setBackground(new Color(20,200,10));//设置Frame的背景,默认为白色
setVisible(true);//设置Frame设置为可见
addWindowListener(new WindowAdapter() //关闭窗口功能
public void windowClosing(WindowEvent e)
System.exit(0);
);
//实现接口ActionListener
public void actionPerformed(ActionEvent e)
Button temp=(Button)e.getSource();
if(e.getActionCommand().equals("number"))
if(first)
str1=str1+temp.getLabel();
t.setText(str1);//将输入的str1显示在文本框中
else
str2=str2+temp.getLabel();
t.setText(str2);//将输入的str2显示在文本框中
else if(e.getActionCommand().equals("oper"))
if(str1=="") //如果还没有输入数就点击运算符执行if
countOper=0;//若此,则将计数清零
first=true;
else
countOper++;//计算输入符号的个数
if(countOper>1)//若输入的符号个数多余一个,则可以进行计算
getResult();
operator=temp.getLabel();//存放加减乘除以及开方、平方的符号
first=false;
else if(e.getActionCommand().equals("开方"))
double d=Math.sqrt(Double.parseDouble(str1));
str1=String.valueOf(d);//将计算出来的结果再次传给str1,为连计算准备
t.setText(String.valueOf(d));//将计算出来的结果传至文本框中
first=false;//置为false,即已输入第一个数
else if(e.getActionCommand().equals("平方"))
double f=Math.pow(Double.parseDouble(str1),2);
str1=String.valueOf(f);
t.setText(String.valueOf(f));
first=false;
else if(e.getActionCommand().equals("清除"))
str1="";//清空
str2="";
t.setText("");//将文本框清空
countOper=0;//将按键计数器清零
first=true;
error=false;
else if(e.getActionCommand().equals("等於"))
if((str1=="")||(str2=="")) //两个数没有输全就点击等号,执行if
countOper=0;//将按键计数器清零
first=true;
else
getResult();
countOper=0;
//运算结果的方法
public void getResult()
num1=Double.parseDouble(str1);
num2=Double.parseDouble(str2);
if(operator.equals("加"))
result=num1+num2;
else if(operator.equals("减"))
result=num1-num2;
else if(operator.equals("乘以"))
result=num1*num2;
else if(operator.equals("除以"))
if(num2==0.0) //除数为0的处理方法
error=true;
else
result=num1/num2;
if(error)
t.setText("error");
else
t.setText(String.valueOf(result));
str1=String.valueOf(result); //运算后把结果放入str1中,str2清空,为连加连减等操作做准备
str2="";
//主方法
public static void main(String[] args)
new Counter();//创建一个对象"计算器"
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalculatorPanel extends JPanel
implements ActionListener
public CalculatorPanel()
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
private void addButton(Container c, String s)
JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
public void actionPerformed(ActionEvent evt)
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9'
|| s.equals("."))
if (start) display.setText(s);
else display.setText(display.getText() + s);
start = false;
else
if (start)
if (s.equals("-"))
else op = s;
else
calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
public void calculate(double n)
if (op.equals("+")) arg += n;
else if (op.equals("-")) arg -= n;
else if (op.equals("*")) arg *= n;
else if (op.equals("/")) arg /= n;
else if (op.equals("=")) arg = n;
display.setText("" + arg);
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
public class CalculatorApplet extends JApplet
public void init()
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
参考技术A 这是根据你自己编的改正的代码,自己觉得比用下拉列表的更好,而且对你来说,这个更好理解消化。不过要是要你题目中所说的代码或是有看不懂的地方,直接HI我,或留言,我再给你。另外,初学JAVA,给你推荐一本书《JAVA开发技术大全》,我就是从这本书起步的。
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Addition extends Applet implements ActionListener
Label label1=new Label();
Label label2=new Label("=");
TextField field1=new TextField(6);
TextField field2=new TextField(6);
TextField field3=new TextField(6);
Button button1=new Button("相加");
Button button2=new Button("相减");
Button button3=new Button("相乘");
Button button4=new Button("相除");
public void init() // 初始化
add(field1); add(label1);
add(field2); add(label2);
add(field3); add(button1);
add(button2); add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
//加
public void dobutton1() // 处理按钮事件
int x=Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x)); // 数值转换为字符串
label1.setText("+");
//减
public void dobutton2() // 处理按钮事件
int x=Integer.parseInt(field1.getText())-Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x)); // 数值转换为字符串
label1.setText("-");
//乘
public void dobutton3() // 处理按钮事件
int x=Integer.parseInt(field1.getText())*Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x)); // 数值转换为字符串
label1.setText("*");
//除
public void dobutton4() // 处理按钮事件
int x=Integer.parseInt(field1.getText())/Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x)); // 数值转换为字符串
label1.setText("/");
//事件源处理
public void actionPerformed(ActionEvent e)
Object obj;
obj=e.getSource();
if(obj==button1)
dobutton1();
else if(obj==button2)
dobutton2();
else if(obj==button3)
dobutton3();
else if(obj==button4)
dobutton4();
本回答被提问者和网友采纳 参考技术B 一看就是学校的作业题,可怜啊,现在用applet的基本上非常少了,可是学校还在教这些无用的东西。 参考技术C package com.inter.test;
import java.applet.Applet;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class TestApplet extends Applet
private JTextField test1;
private JTextField test2;
private JComboBox comBox;
private String[] list="+","-","*","/";
private JButton but;
private JTextField test3;
private JTextField getTest3()
if(test3==null)
test3=new JTextField();
test3.setBounds(new Rectangle(120,50,50,20));
return test3;
private JButton getButton()
if(but==null)
but=new JButton("=");
but.setBounds(new Rectangle(10,50,60,20));
but.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
String one=test1.getText();
String two=test2.getText();
char c=((String)comBox.getSelectedItem()).charAt(0);
switch(c)
case '+':
test3.setText(String.valueOf(Integer.parseInt(one)+Integer.parseInt(two)));
break;
case '-':
test3.setText(String.valueOf(Integer.parseInt(one)-Integer.parseInt(two)));
break;
case '*':
test3.setText(String.valueOf(Integer.parseInt(one)*Integer.parseInt(two)));
break;
case '/':
test3.setText(String.valueOf(Integer.parseInt(one)/Integer.parseInt(two)));
break;
);
return but;
private JTextField getTest1()
if(test1==null)
test1=new JTextField();
test1.setBounds(new Rectangle(10,10,50,20));
return test1;
private JComboBox getCombobox()
if(comBox==null)
comBox=new JComboBox(list);
comBox.setBounds(new Rectangle(65,10,40,20));
return comBox;
public TestApplet()
this.setLayout(null);
this.add(getButton());
this.add(getTest1());
this.add(getCombobox());
this.add(getTest2());
this.add(getTest3());
this.setVisible(true);
private JTextField getTest2()
if(test2==null)
test2=new JTextField();
test2.setBounds(new Rectangle(110,10,50,20));
return test2;
public static void main(String[] args)
new TestApplet();
这个只能实现整数的,你再改一点,就可以实现浮点数了!
java:编写一个计算器小程序,要求可以做加减乘除运算
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; //判断操作是否合法
public Calculator()
super("Calculator");
this.setBounds(300, 300, 300, 300);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//设置布局
text_double=new JTextField("0",20);//设置文本区
text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
this.getContentPane().add(text_double,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();
参考技术A kankan我的
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestCalc implements ActionListener
JFrame jf = new JFrame("计算器");
JPanel jp = new JPanel();
JTextField jtf = new JTextField("0.", 200);
JButton[] jb = new JButton[20];
private int tag = 0;
private double a;
private double b;
private String operator;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
public TestCalc()
jf.add(jtf, BorderLayout.NORTH);
// jtf.requestFocus();
jtf.setEditable(false);
jtf.setCaretPosition(jtf.getText().length() - 1);
jf.add(jp);
jp.setLayout(new GridLayout(5, 4, 4, 4));
String[] str = "Back", "CE", "C", "+", "7", "8", "9", "-", "4", "5",
"6", "*", "1", "2", "3", "/", "0", "+/-", ".", "=" ;
int i = 0;
for (i = 0; i < str.length; i++)
jb[i] = new JButton(str[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
jf.setSize(300, 240);
// jf.setLocation(300,200);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
// jf.pack();//自动调整大小;
jf.setResizable(false);// 不允许别人调大小;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args)
TestCalc tc = new TestCalc();
public void actionPerformed(ActionEvent e)
String command = e.getActionCommand();
if (command.matches("^[[0-9].]$"))
if (tag == 0)
sb1.append(command);
jtf.setText(sb1.substring(0));
else
sb2.append(command);
jtf.setText(sb2.substring(0));
else if (command.matches("^[-/*+]$"))
tag = 1;
operator = command;
else
if (command.matches("="))
String str1 = sb1.substring(0);
String str2 = sb2.substring(0);
if (str1 == null)
a = 0.0;
else
a = Double.parseDouble(str1);
if (str2 == null)
b = 0.0;
else
b = Double.parseDouble(str2);
if (operator.equals("+"))
jtf.setText("" + (a + b));
else if (operator.equals("-"))
BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算
BigDecimal bd2 = new BigDecimal(Double.toString(b));
Double yu1 = bd1.subtract(bd2).doubleValue();
jtf.setText("" + (yu1));
else if (operator.equals("*"))
jtf.setText("" + (a * b));
else if (operator.equals("/"))
BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算
BigDecimal bd2 = new BigDecimal(Double.toString(b));
Double yu1 = bd1.divide(bd2).doubleValue();
jtf.setText("" + yu1);
tag = 0;
sb1.delete(0, sb1.length());
sb2.delete(0, sb2.length());
else if (command.matches("C"))
tag = 0;
sb1.delete(0, sb1.length());
sb2.delete(0, sb2.length());
jtf.setText("0.");
jtf.setCaretPosition(jtf.getText().length() - 1);
else if (command.matches("CE"))
tag = 0;
sb2.delete(0, sb2.length());
jtf.setText(sb1.substring(0));
else if (command.matches("Back")) //Back功能键的实现;
if (tag == 0)
sb1.deleteCharAt(sb1.length() - 1);
jtf.setText(sb1.substring(0));
else
sb2.deleteCharAt(sb2.length() - 1);
jtf.setText(sb2.substring(0));
else if (command.matches("\\+/-"))
if (tag == 0)
if(sb1.substring(0,1).equals("-"))
sb1.replace(0,1,"+");
else
sb1.insert(0, "-");
jtf.setText(sb1.substring(0));
else
if(sb2.substring(0,1).equals("-"))
sb2.replace(0,1,"+");
else
sb2.insert(0, "-");
jtf.setText(sb2.substring(0));
else
参考技术B 这是我做的小计算器,你看下,可以实现加减乘除。觉得好就采纳,谢谢啦。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculate
public static void main(String[] args)
jxframe kj=new jxframe();
kj.setVisible(true);
kj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class jxframe extends JFrame
JTextField w;
private boolean pd;
private String bj;
private double result;
public jxframe()
setTitle("计算器");
pd=true;
bj="=";
result=0;
w=new JTextField(20);
add(w,"North");
add(new m(),"Center");
pack();
class m extends JComponent
public m()
setLayout(new GridLayout(4,4));
addbutton("7",new sz());
addbutton("8",new sz());
addbutton("9",new sz());
addbutton("/",new jx());
addbutton("4",new sz());
addbutton("5",new sz());
addbutton("6",new sz());
addbutton("*",new jx());
addbutton("1",new sz());
addbutton("2",new sz());
addbutton("3",new sz());
addbutton("+",new jx());
addbutton("0",new sz());
addbutton(".",new sz());
addbutton("=",new jx());
addbutton("-",new jx());
public void addbutton(String name,ActionListener e)
JButton an=new JButton(name);
an.addActionListener(e);
add(an);
class sz implements ActionListener
public void actionPerformed(ActionEvent e)
String sb=e.getActionCommand();
if(pd)
w.setText("");
pd=false;
w.setText(w.getText()+sb);
class jx implements ActionListener
public void actionPerformed(ActionEvent e)
String jb=e.getActionCommand();
if(pd)
jb.equals("-");
w.setText(jb);
pd=false;
else
js(Double.parseDouble(w.getText()));
bj=jb;
pd=true;
public void js(double jsb)
if(bj.equals("/")) result/=jsb;
if(bj.equals("*")) result*=jsb;
if(bj.equals("+")) result+=jsb;
if(bj.equals("-")) result-=jsb;
if(bj.equals("=")) result=jsb;
w.setText(""+result);
参考技术C 代码如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Accumulator extends JFrame implements ActionListener,ItemListener
public double result=0;
static String is="+";
JTextField jt1 = new JTextField(10);
JTextField jt2 = new JTextField(10);
JTextField jt3 = new JTextField(10);
JComboBox jcb = new JComboBox();
JButton jb = new JButton("=");
public Accumulator()
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout());
jcb.addItemListener(this);
jcb.addItem("+");
jcb.addItem("-");
jcb.addItem("*");
jcb.addItem("/");
jb.addActionListener(this);
jp.add(jt1);
jp.add(jcb);
jp.add(jt2);
jp.add(jb);
jp.add(jt3);
this.setTitle("简易计算器");
this.add(jp);
this.setBounds(400, 350, 500, 300);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
@Override
public void actionPerformed(ActionEvent ae)
if(ae.getSource()==jb)
if(is.equals("+"))
result =Double.parseDouble(jt1.getText())+Double.parseDouble(jt2.getText());
jt3.setText(String.valueOf(result));
if(is.equals("-"))
result =Double.parseDouble(jt1.getText())-Double.parseDouble(jt2.getText());
jt3.setText(String.valueOf(result));
if(is.equals("*"))
result =Double.parseDouble(jt1.getText())*Double.parseDouble(jt2.getText());
jt3.setText(String.valueOf(result));
if(is.equals("/"))
result =Double.parseDouble(jt1.getText())/Double.parseDouble(jt2.getText());
jt3.setText(String.valueOf(result));
public static void main(String [] args)
Accumulator ac = new Accumulator();
@Override
public void itemStateChanged(ItemEvent ie)
is =(String)ie.getItem();
以上是关于用JAVA制作一个小程序,计算两个数的加减乘除,用Applet实现的主要内容,如果未能解决你的问题,请参考以下文章