求一个JAVA程序

求一个有界面的简单计算器程序,能实现基本的加减乘除运算....
注:界面就类似计算机上的计算器,只需要有加减乘除功能健和数字按键输入的,并且能进行计算,显示结果....
麻烦各位帮帮忙,谢谢了...

//引入所需的类包

import java.awt.*;
import java.applet.*;

//引入所需的类包

import java.awt.*;
import java.applet.*;

//定义类Calculator.java
public class Calculator extends Applet
{
private TextField tfAnswer;//计算器的显示文本框
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;//数字0~9的按钮
//定义小数点,等于号,加,减,清零,乘和除等按钮
private Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision;

private String OperatorCurrent,OperatorPre;//OperatorCurrent表示当前的运算符号,
//OperatorPre表示上一次的运算符号
private String ForeScreen,BackScreen;//ForeScreen表示当前显示的字符
//BackScreen表示上一次显示的字符
private boolean isFloat = false;//判断是否为浮点数

//类的初始化函数
public void init()
{
//将OperatorCurrent,OperatorPre初始化为空字符串
OperatorCurrent = new String("");
OperatorPre = new String("");
//将ForeScreen,BackScreen初始化为空字符串
ForeScreen = new String("");
BackScreen = new String("");
//设置背景色和前景色
this.setBackground(Color.gray);
this.setForeground(Color.blue);

//添加用于显示的文本框
setLayout(null);
tfAnswer = new TextField();
tfAnswer.setBounds(5,5,100,20);
this.add(tfAnswer);

//添加"C"按钮
bClear = new Button("C");
bClear.setBounds(5,30,20,20);
bClear.setBackground(Color.pink);
this.add(bClear);

//添加"/"按钮
bDivision = new Button("/");
bDivision.setBounds(30,30,20,20);
bDivision.setBackground(Color.green);
this.add(bDivision);

//添加"*"按钮
bMulti = new Button("*");
bMulti.setBounds(55,30,20,20);
bMulti.setBackground(Color.green);
this.add(bMulti);

//添加"-"按钮
bMinus = new Button("-");
bMinus.setBounds(80,30,20,20);
bMinus.setBackground(Color.green);
this.add(bMinus);

//添加"7""8""9"按钮
b7 = new Button("7");
b7.setBounds(5,55,20,20);
this.add(b7);
b8 = new Button("8");
b8.setBounds(30,55,20,20);
this.add(b8);
b9 = new Button("9");
b9.setBounds(55,55,20,20);
this.add(b9);

//添加"4""5""6"按钮
b4 = new Button("4");
b4.setBounds(5,80,20,20);
this.add(b4);
b5 = new Button("5");
b5.setBounds(30,80,20,20);
this.add(b5);
b6 = new Button("6");
b6.setBounds(55,80,20,20);
this.add(b6);

//添加"+"按钮
bPlus = new Button("+");
bPlus.setBounds(80,55,20,45);
bPlus.setBackground(Color.green);
this.add(bPlus);

//添加"1""2""3"按钮
b1 = new Button("1");
b1.setBounds(5,105,20,20);
this.add(b1);
b2 = new Button("2");
b2.setBounds(30,105,20,20);
this.add(b2);
b3 = new Button("3");
b3.setBounds(55,105,20,20);
this.add(b3);

//添加数字"0"小数点"."按钮
b0 = new Button("0");
b0.setBounds(5,130,45,20);
this.add(b0);
bPoint = new Button(".");
bPoint.setBounds(55,130,20,20);
this.add(bPoint);

//添加"="按钮
bEqual = new Button("=");
bEqual.setBounds(80,105,20,45);
bEqual.setBackground(Color.green);
this.add(bEqual);

}

//按钮点击时间处理函数
public boolean action(Event e, Object o)
{
String s = new String("");
if(e.target instanceof Button)
{
//如果点击的是数字按钮或者是小数点按钮时
if((e.target == b0)||(e.target == b1)||(e.target == b2)||
(e.target == b3)||(e.target == b4)||(e.target == b5)||
(e.target == b6)||(e.target == b7)||(e.target == b8)||
(e.target == b9)||(e.target == bPoint))
{
//如果点击的是数字按钮时
if(e.target != bPoint)
{
s = (String)o;
System.out.print(s);
doForeScreen(s);//在显示文本框中显示相应的数字
}
//如果点击的是小数点按钮,并且isFloat为false时
if((e.target == bPoint)&&(!isFloat))
{
isFloat = true;
s = (String)o;
//如果显示文本框为空时
if(ForeScreen.equals(""))
{
ForeScreen += "0.";//则添加"0."
repaint();//重绘
}
//否则直接显示小数点"."
else
{
System.out.print(s);
doForeScreen(s);
}
}
}

//如果点击按钮"C"
if(e.target == bClear)
{
doClear();//清空显示文本框
}

//如果点击了运算符按钮
if((e.target == bMulti)||(e.target == bDivision)||
(e.target == bPlus)||(e.target == bMinus))
{
//如果显示文本框不为空时
if(ForeScreen != "")
{
OperatorCurrent = ((String)o);//保存当前运算符
doOperator();//计算结果
System.out.print((String)o);
}
//否则
else
{
OperatorPre = ((String)o);//将运算符保存为前一次的运算符变量中
}
}

//如果点击了按钮"="
if(e.target == bEqual)
{
doOperator();//计算结果
}
}

return true;
}

//计算结果的函数
public void doOperator()
{
//变量dFore保存当前数值,而变量dBack保存前一次的数值
double dFore,dBack;
Double d;

//如果前一次的运算符为空时
if(OperatorPre.equals(""))
{
BackScreen = ForeScreen;//将当前的数值保存在变量BackScreen中
ForeScreen = "";//将ForeScreen置为空,以获取新的数值
tfAnswer.setText(BackScreen);//将保存在变量BackScreen中的数值显示出来
repaint();//重绘
}
//否则,按前一次的运符计算结果
else
{
dFore = (new Double(ForeScreen)).doubleValue();//获取当前显示的数值
dBack = (new Double(BackScreen)).doubleValue();//获取前一次的数值
ForeScreen = "";//将ForeScreen置空,以获取新的数值
BackScreen = tfAnswer.getText();//将结果保存在变量BackScreen中

//如果是加法
if(OperatorPre.equals("+"))
{
d = new Double((dBack + dFore));//两数相加
tfAnswer.setText(d.toString());//显示结果
BackScreen = d.toString();//将结果 保存在变量BackScreen中
repaint();//重绘
}

//如果是减法
if(OperatorPre.equals("-"))
{
d = new Double((dBack - dFore));//两数相减
tfAnswer.setText(d.toString());//显示结果
BackScreen = d.toString();//将结果保存在变量BackScreen中
repaint();//重绘
}

//如果是乘法
if(OperatorPre.equals("*"))
{
d = new Double((dBack * dFore));//两数相乘
tfAnswer.setText(d.toString());//显示结果
BackScreen = d.toString();//将结果保存在变量BackScreen中
repaint();//重绘
}

//如果是除法
if(OperatorPre.equals("/"))
{
d = new Double((dBack / dFore));//两数相除
tfAnswer.setText(d.toString());//显示结果
BackScreen = d.toString();//将结果保存在变量BackScreen中
repaint();//重绘
}
}
OperatorPre = OperatorCurrent;//将当前的运算符保存在变量OperatorPre中
repaint();//重绘
}

//类的绘制函数
public void paint(Graphics g)
{
//tfAnswer.setText(ForeScreen);
}

//当前数值的显示函数
public void doForeScreen(String s)
{
ForeScreen += s;
tfAnswer.setText(ForeScreen);
repaint();
}

//后屏处理函数
public void doBackScreen(String s)
{
BackScreen = ForeScreen;//将当前的数值保存在BackScreen中
ForeScreen = "";//ForeScreen清空
tfAnswer.setText(ForeScreen);//显示数值
repaint();//重绘
}

//文本框清空
public void doClear()
{
//相应变量的置空
OperatorCurrent = "";
OperatorPre = "";
ForeScreen = "";
BackScreen = "";
isFloat = false;
tfAnswer.setText(ForeScreen);//文本框清空
repaint();//重绘
}

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-05-27
/////calculator.java代码

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class calculator extends JApplet{

Container contentPane;

JButton btn0 = new JButton();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btn4 = new JButton();
JButton btn5 = new JButton();
JButton btn6 = new JButton();
JButton btn7 = new JButton();
JButton btn8 = new JButton();
JButton btn9 = new JButton();

//特殊按钮
JButton btnClear = new JButton(); //清除
JButton btnEqual = new JButton(); //等于
JButton btnPlus = new JButton(); //+操作符
JButton btnMinus = new JButton(); //-操作符
JButton btnMultiply = new JButton(); //*操作符
JButton btnDivide = new JButton(); //除操作符

JTextField texResult = new JTextField();

boolean flag=false;
String operand1;
String operand2;
double result;
String action;

public void init(){

contentPane = getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(250, 250));

btn0.setBounds(new Rectangle(50, 230, 45, 45));
btn0.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14));
btn0.setText("0");
btn0.addActionListener(new btn0(this));

//清除操作符
btnClear.setBounds(new Rectangle(100,230,45,45));
btnClear.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btnClear.setText("C");
btnClear.addActionListener(new btnClear(this));

//=操作符
btnEqual.setBounds(new Rectangle(150,230,45,45));
btnEqual.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btnEqual.setText("=");
btnEqual.addActionListener(new btnEqual(this));

//+操作符
btnPlus.setBounds(new Rectangle(200,230,45,45));
btnPlus.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btnPlus.setText("+");
btnPlus.addActionListener(new btnPlus(this));

btn1.setBounds(new Rectangle(50,180,45,45));
btn1.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn1.setText("1");
btn1.addActionListener(new btn1(this));

btn2.setBounds(new Rectangle(100,180,45,45));
btn2.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn2.setText("2");
btn2.addActionListener(new btn2(this));

btn3.setBounds(new Rectangle(150,180,45,45));
btn3.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn3.setText("3");
btn3.addActionListener(new btn3(this));

//-操作符
btnMinus.setBounds(new Rectangle(200,180,45,45));
btnMinus.setFont(new java.awt.Font("Tahoma",Font.PLAIN,16));
btnMinus.setText("-");
btnMinus.addActionListener(new btnMinus(this));

btn4.setBounds(new Rectangle(50,130,45,45));
btn4.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn4.setText("4");
btn4.addActionListener(new btn4(this));

btn5.setBounds(new Rectangle(100,130,45,45));
btn5.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn5.setText("5");
btn5.addActionListener(new btn5(this));

btn6.setBounds(new Rectangle(150,130,45,45));
btn6.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn6.setText("6");
btn6.addActionListener(new btn6(this));

//*操作符
btnMultiply.setBounds(new Rectangle(200,130,45,45));
btnMultiply.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btnMultiply.setText("*");
btnMultiply.addActionListener(new btnMultiply(this));

btn7.setBounds(new Rectangle(50,80,45,45));
btn7.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn7.setText("7");
btn7.addActionListener(new btn7(this));

btn8.setBounds(new Rectangle(100,80,45,45));
btn8.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn8.setText("8");
btn8.addActionListener(new btn8(this));

btn9.setBounds(new Rectangle(150,80,45,45));
btn9.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btn9.setText("9");
btn9.addActionListener(new btn9(this));

//除操作符
btnDivide.setBounds(new Rectangle(200,80,45,45));
btnDivide.setFont(new java.awt.Font("Tahoma",Font.PLAIN,14));
btnDivide.setText("/");
btnDivide.addActionListener(new btnDivide(this));

texResult.setText("");
texResult.setBounds(new Rectangle(40, 35, 215, 35));
texResult.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14));
texResult.setHorizontalAlignment(JTextField.RIGHT);

contentPane.add(btn0);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn3);
contentPane.add(btn4);
contentPane.add(btn5);
contentPane.add(btn6);
contentPane.add(btn7);
contentPane.add(btn8);
contentPane.add(btn9);

contentPane.add(btnClear);
contentPane.add(btnEqual);
contentPane.add(btnPlus);
contentPane.add(btnMinus);
contentPane.add(btnMultiply);
contentPane.add(btnDivide);

contentPane.add(texResult);
}

//0的事件监听器
public void btn0_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn0.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn0.getActionCommand());
}
}

class btn0 implements ActionListener{

private calculator adaptee;

btn0(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn0_actionPerformed(e);
}
}

//1的事件监听器
public void btn1_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn1.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn1.getActionCommand());
}
}

class btn1 implements ActionListener{

private calculator adaptee;

btn1(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn1_actionPerformed(e);
}
}

//2的事件监听器
public void btn2_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn2.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn2.getActionCommand());
}
}

class btn2 implements ActionListener{

private calculator adaptee;

btn2(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn2_actionPerformed(e);
}
}

//3的事件监听器
public void btn3_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn3.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn3.getActionCommand());
}
}

class btn3 implements ActionListener{

private calculator adaptee;

btn3(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn3_actionPerformed(e);
}
}

//4的事件监听器
public void btn4_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn4.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn4.getActionCommand());
}
}

class btn4 implements ActionListener{

private calculator adaptee;

btn4(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn4_actionPerformed(e);
}
}

//5的事件监听器
public void btn5_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn5.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn5.getActionCommand());
}
}

class btn5 implements ActionListener{

private calculator adaptee;

btn5(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn5_actionPerformed(e);
}
}

//6的事件监听器
public void btn6_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn6.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn6.getActionCommand());
}
}

class btn6 implements ActionListener{

private calculator adaptee;

btn6(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn6_actionPerformed(e);
}
}

//7的事件监听器
public void btn7_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn7.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn7.getActionCommand());
}
}

class btn7 implements ActionListener{

private calculator adaptee;

btn7(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn7_actionPerformed(e);
}
}

//8的事件监听器
public void btn8_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn8.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn8.getActionCommand());
}
}

class btn8 implements ActionListener{

private calculator adaptee;

btn8(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn8_actionPerformed(e);
}
}

//9的事件监听器
public void btn9_actionPerformed(ActionEvent e){

if(flag){
texResult.setText(btn9.getActionCommand());
flag = false;
}
else{

texResult.setText(texResult.getText()+btn9.getActionCommand());
}
}

class btn9 implements ActionListener{

private calculator adaptee;

btn9(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btn9_actionPerformed(e);
}
}

//清除操作
public void btnClear_actionPerformed(ActionEvent e){

texResult.setText("");
}

class btnClear implements ActionListener{

private calculator adaptee;

btnClear(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnClear_actionPerformed(e);
}
}

//+操作事件监听器
public void btnPlus_actionPerformed(ActionEvent e){

operand1 = texResult.getText();
action = "plus";
flag = true;
}

class btnPlus implements ActionListener{

private calculator adaptee;

btnPlus(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnPlus_actionPerformed(e);
}
}

//-操作事件监听器
public void btnMinus_actionPerformed(ActionEvent e){

operand1 = texResult.getText();
action = "minus";
flag = true;
}

class btnMinus implements ActionListener{

private calculator adaptee;

btnMinus(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnMinus_actionPerformed(e);
}
}

//*操作事件监听器
public void btnMultiply_actionPerformed(ActionEvent e){

operand1 = texResult.getText();
action = "multiply";
flag = true;
}

class btnMultiply implements ActionListener{

private calculator adaptee;

btnMultiply(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnMultiply_actionPerformed(e);
}
}

//除操作事件监听器
public void btnDivide_actionPerformed(ActionEvent e){

operand1 = texResult.getText();
action = "divide";
flag = true;
}

class btnDivide implements ActionListener{

private calculator adaptee;

btnDivide(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnDivide_actionPerformed(e);
}
}

//=操作事件监听器
public void btnEqual_actionPerformed(ActionEvent e){

double digit1;
double digit2;
operand2 = texResult.getText();

if(!flag){
if(action.equals("plus")){

digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 + digit2;
texResult.setText("" + result);
flag = true;
}
else if(action.equals("minus")){

digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 - digit2;
texResult.setText(""+result);
flag = true;
}
else if(action.equals("multiply")){

digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 * digit2;
texResult.setText(""+result);
flag = true;
}
else if(action.equals("divide")){

digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 / digit2;
texResult.setText(""+result);
flag = true;
}
}
}

class btnEqual implements ActionListener{

private calculator adaptee;

btnEqual(calculator adaptee){
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e){
adaptee.btnEqual_actionPerformed(e);
}
}
}

////calculator.html文件代码

<html>
<head><title>计算器</title></head>
<center>
<applet code=calculator.class width=300 height=300>
</applet>
</center>
</html>

以上只要有java运行环境JDK,将calculator.java文件编译,然后建一文本文件,直接运行calculator.html就可以看到效果
第2个回答  2008-05-27
//界面

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class computor
{

private JFrame jf = null;
private JButton jbadd, jbsub, jbmutil, jbdiv, jbequal, jbdot, jbcr;
private JTextField jexpression;
private JButton jb[] = new JButton[10];
private Font newfnt;
private Font oldfnt;

public computor(){

newfnt = new Font(null, Font.BOLD, 30);
//expfnt = new Font(null, Font.BOLD, 20);

jexpression = new JTextField("");
jexpression.setHorizontalAlignment(JTextField.RIGHT);
//jexpression.setFont(expfnt);

for(int index=0; index<10; index++)
{
jb[index] = new JButton(index+"");
jb[index].addActionListener(new ExpressionButtonHandler());
jb[index].addMouseListener(new EnterOutHandle());
//jb[index].setFont(fnt);
}

oldfnt = jb[0].getFont();

jbequal = new JButton("=");
jbequal.addActionListener(new ResultButtonHandler());
//jbequal.setFont(fnt);

jbdot = new JButton(".");
jbdot.addActionListener(new ExpressionButtonHandler());
//jbdot.setFont(fnt);

jbadd = new JButton("+");
jbadd.addActionListener(new ExpressionButtonHandler());
//jbadd.setFont(fnt);

jbsub = new JButton("-");
jbsub.addActionListener(new ExpressionButtonHandler());
//jbsub.setFont(fnt);

jbmutil = new JButton("*");
jbmutil.addActionListener(new ExpressionButtonHandler());

//jbmutil.setFont(fnt);

jbdiv = new JButton("/");
jbdiv.addActionListener(new ExpressionButtonHandler());
//jbdiv.setFont(fnt);

jbcr = new JButton("CR");
jbcr.addActionListener(new CRButtonHandler());
//jbcr.setFont(fnt);

jf = new JFrame();
jf.setTitle("Swing计算器");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 200);
jf.setLocation(100, 200);
jf.setLayout(new BorderLayout()); //总体布局

JPanel jptop = new JPanel(new GridLayout(1,2));
jptop.add(jexpression);
jptop.add(jbcr);

JPanel jpdown = new JPanel(new GridLayout(4,4));
jpdown.add(jb[1]);
jpdown.add(jb[2]);
jpdown.add(jb[3]);
jpdown.add(jbadd);
jpdown.add(jb[4]);
jpdown.add(jb[5]);
jpdown.add(jb[6]);
jpdown.add(jbsub);
jpdown.add(jb[7]);
jpdown.add(jb[8]);
jpdown.add(jb[9]);
jpdown.add(jbmutil);
jpdown.add(jbdot);
jpdown.add(jb[0]);
jpdown.add(jbequal);
jpdown.add(jbdiv);

Container ca = jf.getContentPane();
ca.add(jptop, BorderLayout.NORTH);
ca.add(jpdown, BorderLayout.CENTER);

jf.show();

}

//定义事件处理类
private class ExpressionButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton jbut = (JButton)e.getSource();
String exp = jexpression.getText();
exp = exp + jbut.getText();
jexpression.setText(exp);
}
}

//定义事件处理类
private class CRButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jexpression.setText("");
}
}

public class EnterOutHandle implements MouseListener
{
public void mousePressed(MouseEvent e){
}

public void mouseReleased(MouseEvent e){
}

public void mouseEntered(MouseEvent e){
JButton jbsource = (JButton)e.getSource();
jbsource.setFont(newfnt);
}

public void mouseExited(MouseEvent e){
JButton jbsource = (JButton)e.getSource();
jbsource.setFont(oldfnt);

}

public void mouseClicked(MouseEvent e){
}
}

//定义事件处理类
private class ResultButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String exp = jexpression.getText();
//利用expression类来计算结果
expression expr = new expression(exp);
double re = 0;
try{
re = expr.getResult();
}catch(IllegalExpressionException le)
{
JOptionPane.showMessageDialog(jf, le.toString());
}

jexpression.setText(re + "");
}
}

public static void main(String[] args)
{
computor jf = new computor();

}
}

/*.从命令行接收一个表达式,计算该表达式的值。 简单的+ - * / %
java work3 1+2
javac work3

特殊:单独一个*的参数,系统会将当前目录下的所有的文件名作为参数
因此要求所有的表达式不要有空格,若有空格,则必须用""括起来
*/

class IllegalExpressionException extends Exception{
public IllegalExpressionException(String exp){
super(exp);
}
}

public class expression{
public String exp = null, texp = null;
public String op1, op2;
public String operator;

public expression(String exp){
this.exp = new String(exp);
texp = new String(exp);
op1 = new String();
op2 = new String();
operator = new String();
}

public void setExpression(String exp){
this.exp = new String(exp);
texp = new String(exp);
op1 = new String();
op2 = new String();
operator = new String();
}

public String getExpression(){
return exp.toString();
}

private void getOp1(){
int index = 0;
for(index=0; index<texp.length(); index++)
{
char c = texp.charAt(index);
if ((c>='0' && c<='9') || (c=='.') || (c=='e')|| (c=='E'))
{}
else
break;
}

op1 = texp.substring(0, index);
texp = texp.substring(index); //删除op1部分
}

private void getOp2(){
//必须要先解析op1, operator
op2 = new String(texp);
}

private void getOperator(){
int index = 0;
for(index=0; index<texp.length(); index++)
{
char c = texp.charAt(index);
if ((c>='0' && c<='9') || (c=='.'))
break;
}

operator = texp.substring(0, index);
texp = texp.substring(index); //删除op1部分
}

public double getResult() throws IllegalExpressionException
{
getOp1();
getOperator();
getOp2();
//对获取到的参数和运算符进行转换
double re = 0;
double dop1, dop2;
char c = ' ';

//获取运算符

try{
dop1 = Double.parseDouble(op1);
dop2 = Double.parseDouble(op2);
}catch(NumberFormatException e)
{
throw new IllegalExpressionException("错误的操作数");
}

operator = operator.trim();
if (operator.length()!=1)
{
throw new IllegalExpressionException("错误的运算符");
}

c= operator.charAt(0);

switch(c){
case '+': re = dop1 + dop2; break;
case '-': re = dop1 - dop2; break;
case '*': re = dop1 * dop2; break;
case '/': re = dop1 / dop2; break;
default: throw new IllegalExpressionException("错误的运算符");
}

return re;
}

public static void main(String[] argvs){

expression e = new expression("3.1 + 4+");
try{
System.out.println(e.getResult());
System.out.println(e.op1);
System.out.println(e.operator);
System.out.println(e.op2);

System.out.println(e.getExpression());
}
catch(IllegalExpressionException ee)
{
System.out.println(ee.toString());
}

}

}

求一非常简单的java 程序?
import java.util.Scanner;public class GetNumResult { public static void main(String[] args) { Scanner s = new Scanner(System.in);System.out.println("请输入一个整数:");int num = s.nextInt();for(int i = 1; i <= 10; i ++){ System.out.println(num + " * " + i ...

求java程序代码
import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;\/ 一个简单的Swing窗口,输入内容单击“确定”按钮后...

求JAVA代码
程序如下:public class ComputeGCD { public static void main(String[] args) { int t = gcd(24,16); System.out.println("gcd(24,16) = " +t); int t1 = gcd(225,25); System.out.println("gcd(225,25)"+t1); } public static int gcd(int m,int n){...

求JAVA程序代码
import java.awt.Button;import java.awt.GridLayout;import java.awt.Panel;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.GregorianCalendar;import javax.swing.JFrame;public class MyMobilePhone extends ...

求一java的程序
package com;public class ProducerConsumer { public static void main(String[] args) { ProductStack ps = new ProductStack();Producer p = new Producer(ps, "creator 1");Consumer c = new Consumer(ps, "consumer 1");new Thread(p).start();new Thread(c).start();} } class ...

求写一个java小程序
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.swing.JOptionPane;\/** * 复制文件或文件夹 * * zww *\/public class ...

求写一个java程序
您好,这样的:import java.util.regex.Matcher;import java.util.regex.Pattern;public class Student{ public char[] name = new char[]{'无','聊','题'};public int sid= 12123123;public static void main(String[] args){ Student stu = new Student();String strName = new String(stu...

求编写一段java程序
import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.JButton;public class ChatClient extends JFrame{ private static final long serialVersionUID = 1L; JTextArea ta = new JTextArea("你可以通过此客户端的群聊!"...

求一道Java程序设计的代码
import java.awt.Font;public class MyGUI extends JFrame { private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private JLabel jLabel = null;private JLabel jLabel1 = null;private JLabel jLabel2 = null;private JLabel jLabel3 = null;private JRadio...

求一个java小程序
简单写了一个,你参考一下吧:import java.util.*;public class t{public static void main(String[]args){Scanner sc = new Scanner(System.in);System.out.println("输入总人数:");int n = sc.nextInt();\/\/总人数int count = 0;\/\/记录出局的人数int index = 0;\/\/人的序号int[] ...

相似回答