Java中计算器的程序

力求Java中一个计算器的程序代码,十分感谢,满意的话猛加分!十分急!大哥们,可以简单点的不?是大学的实验报告,四面纸可以写完的,十分,万分感激啊!... 力求Java中一个计算器的程序代码,十分感谢,满意的话猛加分!十分急!
大哥们,可以简单点的不?是大学的实验报告,四面纸可以写完的,十分,万分感激啊!
展开
 我来答
340417593
2009-06-08 · TA获得超过1037个赞
知道答主
回答量:80
采纳率:0%
帮助的人:83.5万
展开全部
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class Counter extends Frame
{
//声明三个并悔滑面板的布局
GridLayout gl1,gl2,gl3;
Panel p0,p1,p2,p3;
JTextField tf1;
TextField tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer str;//显示屏所显示的字符串
double x,y;//x和y都是运算数
int z;//Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
static double m;//记忆的数字
public Counter()
{
gl1=new GridLayout(1,4,10,0);//实例化三个面板的布局
gl2=new GridLayout(4,1,0,15);
gl3=new GridLayout(4,5,10,15);

tf1=new JTextField(27);//显示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new TextField(10);//显示记忆的索引值
tf2.setEditable(false);
//实例化所前圆有按钮、设置其前景色并注册监听器
b0=new Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new Bt());
b1=new Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new Bt());
b2=new Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new Bt());
b3=new Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new Bt());
b4=new Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new Bt());
b5=new Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new Bt());
b6=new Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new Bt());
b7=new Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8=new Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9=new Button("绝腊9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10=new Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b11=new Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new Bt());
b12=new Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new Bt());
b13=new Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new Bt());
b14=new Button("6");
b14.setForeground(Color.blue);
b14.addActionListener(new Bt());
b15=new Button("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b16=new Button("%");
b16.setForeground(Color.blue);
b16.addActionListener(new Bt());
b17=new Button("1");
b17.setForeground(Color.blue);
b17.addActionListener(new Bt());
b18=new Button("2");
b18.setForeground(Color.blue);
b18.addActionListener(new Bt());
b19=new Button("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20=new Button("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b21=new Button("1/X");
b21.setForeground(Color.blue);
b21.addActionListener(new Bt());
b22=new Button("0");
b22.setForeground(Color.blue);
b22.addActionListener(new Bt());
b23=new Button("+/-");
b23.setForeground(Color.blue);
b23.addActionListener(new Bt());
b24=new Button(".");
b24.setForeground(Color.blue);
b24.addActionListener(new Bt());
b25=new Button("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26=new Button("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());

//实例化四个面板
p0=new Panel();
p1=new Panel();
p2=new Panel();
p3=new Panel();
//创建一个空字符串缓冲区
str=new StringBuffer();

//添加面板p0中的组件和设置其在框架中的位置和大小
p0.add(tf1);
p0.setBounds(10,25,300,40);
//添加面板p1中的组件和设置其在框架中的位置和大小
p1.setLayout(gl1);
p1.add(tf2);
p1.add(b0);
p1.add(b1);
p1.add(b2);
p1.setBounds(10,65,300,25);
//添加面板p2中的组件并设置其的框架中的位置和大小
p2.setLayout(gl2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBounds(10,110,40,150);
//添加面板p3中的组件并设置其在框架中的位置和大小
p3.setLayout(gl3);//设置p3的布局
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b17);
p3.add(b18);
p3.add(b19);
p3.add(b20);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p3.add(b24);
p3.add(b25);
p3.add(b26);
p3.setBounds(60,110,250,150);
//设置框架中的布局为空布局并添加4个面板
setLayout(null);
add(p0);
add(p1);
add(p2);
add(p3);
setResizable(false);//禁止调整框架的大小
//匿名类关闭窗口
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e1)
{
System.exit(0);
}
});
setBackground(Color.lightGray);
setBounds(100,100,320,280);
setVisible(true);

}
//构造监听器
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"清零
{
tf1.setText("0");//把显示屏清零
str.setLength(0);
}
else if(e2.getSource()==b23)//单击"+/-"选择输入的运算数是正数还是负数
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(-x));
}
else if(e2.getSource()==b25)//单击加号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);//清空缓冲区以便接收新的另一个运算数
y=0d;
z=0;
}
else if(e2.getSource()==b20)//单击减号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e2.getSource()==b15)//单击乘号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e2.getSource()==b10)//单击除号按钮获得x的值和z的值并空y的值
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e2.getSource()==b26)//单击等号按钮输出计算结果
{
str.setLength(0);
switch(z)
{
case 0 : tf1.setText(""+(x+y));break;
case 1 : tf1.setText(""+(x-y));break;
case 2 : tf1.setText(""+(x*y));break;
case 3 : tf1.setText(""+(x/y));break;
}
}
else if(e2.getSource()==b24)//单击"."按钮输入小数
{
if(tf1.getText().trim().indexOf(′.′)!=-1)//判断字符串中是否已经包含了小数点
{

}
else//如果没数点有小
{
if(tf1.getText().trim().equals("0"))//如果初时显示为0
{
str.setLength(0);
tf1.setText((str.append("0"+e2.getActionCommand())).toString());
}
else if(tf1.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{
}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
}
}

y=0d;

}
else if(e2.getSource()==b11)//求平方根
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText("数字格式异常");
if(x<0)
tf1.setText("负数没有平方根");
else
tf1.setText(""+Math.sqrt(x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b16)//单击了"%"按钮
{
x=Double.parseDouble(tf1.getText().trim());
tf1.setText(""+(0.01*x));
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b21)//单击了"1/X"按钮
{

x=Double.parseDouble(tf1.getText().trim());
if(x==0)
{

tf1.setText("除数不能为零");
}
else
{
tf1.setText(""+(1/x));
}
str.setLength(0);
y=0d;
}
else if(e2.getSource()==b3)//MC为清除内存
{
m=0d;
tf2.setText("");
str.setLength(0);
}
else if(e2.getSource()==b4)//MR为重新调用存储的数据
{
if(tf2.getText().trim()!="")//有记忆数字
{
tf1.setText(""+m);
}
}
else if(e2.getSource()==b5)//MS为存储显示的数据
{

m=Double.parseDouble(tf1.getText().trim());
tf2.setText("M");
tf1.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b6)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR
{
m=m+Double.parseDouble(tf1.getText().trim());
}
else//选择的是其他的按钮
{
if(e2.getSource()==b22)//如果选择的是"0"这个数字键
{
if(tf1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作
{

}
else
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
else if(e2.getSource()==b0)//选择的是“BackSpace”按钮
{
if(!tf1.getText().trim().equals("0"))//如果显示屏显示的不是零
{
if(str.length()!=1)
{
tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常
}
else
{
tf1.setText("0");
str.setLength(0);
}
}
y=Double.parseDouble(tf1.getText().trim());
}
else//其他的数字键
{
tf1.setText(str.append(e2.getActionCommand()).toString());
y=Double.parseDouble(tf1.getText().trim());
}
}
}
catch(NumberFormatException e){
tf1.setText("数字格式异常");
}
catch(StringIndexOutOfBoundsException e){
tf1.setText("字符串索引越界");
}
}
}
public static void main(String args[])
{
new Counter();
}

}
tower888
2009-06-08 · TA获得超过611个赞
知道小有建树答主
回答量:397
采纳率:0%
帮助的人:278万
展开全部
package javatest;

import java.awt.Frame;

import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.ComponentOrientation;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.ToolTipManager;

public class 计算器 extends Frame {

private static final long serialVersionUID = 1L;
private TextField textField = null;
private Button button = null;
private Button button1 = null;
private Button button2 = null;
private Button button3 = null;
private Button button4 = null;
private Button button5 = null;
private Button button6 = null;
private Button button7 = null;
private Button button8 = null;
private Button button9 = null;
private Button button10 = null;
private Button button11 = null;
private Button button12 = null;
private Button button13 = null;
private Button button14 = null;
private Button button15 = null;
private TextField textField1 = null;
private TextField textField2 = null;
private TextField textField3 = null;
private Button button16 = null;
/**
* This method initializes textField
*
* @return java.awt.TextField
*/
private TextField getTextField() {
if (textField == null) {
textField = new TextField();
textField.setBounds(new Rectangle(45, 30, 226, 16));
textField.setComponentOrientation(ComponentOrientation.UNKNOWN);
textField.setName("textfield");

}
return textField;
}

/**
* This method initializes button
*
* @return java.awt.Button
*/
private Button getButton() {
if (button == null) {
button = new Button();
button.setBounds(new Rectangle(30, 72, 32, 33));
button.setLabel("0");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))//必须加equals,不能写成if(textField2.getText=="")
{
textField1.setText(textField1.getText()+"0");
textField.setText(textField.getText()+"0");
}
else
{
textField3.setText(textField3.getText()+"0");
textField.setText(textField.getText()+"0");
}
}
});
}
return button;
}

/**
* This method initializes button1
*
* @return java.awt.Button
*/
private Button getButton1() {
if (button1 == null) {
button1 = new Button();
button1.setBounds(new Rectangle(91, 72, 32, 33));
button1.setLabel("1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"1");
textField.setText(textField.getText()+"1");
}
else
{
textField3.setText(textField3.getText()+"1");
textField.setText(textField.getText()+"1");
}

}
});
}
return button1;
}

/**
* This method initializes button2
*
* @return java.awt.Button
*/
private Button getButton2() {
if (button2 == null) {
button2 = new Button();
button2.setBounds(new Rectangle(146, 72, 32, 33));
button2.setLabel("2");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"2");
textField.setText(textField.getText()+"2");
}
else
{
textField3.setText(textField3.getText()+"2");
textField.setText(textField.getText()+"2");
}

}
});
}
return button2;
}

/**
* This method initializes button3
*
* @return java.awt.Button
*/
private Button getButton3() {
if (button3 == null) {
button3 = new Button();
button3.setBounds(new Rectangle(30, 114, 32, 33));
button3.setLabel("3");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"3");
textField.setText(textField.getText()+"3");
}
else
{
textField3.setText(textField3.getText()+"3");
textField.setText(textField.getText()+"3");
}
}
});
}
return button3;
}

/**
* This method initializes button4
*
* @return java.awt.Button
*/
private Button getButton4() {
if (button4 == null) {
button4 = new Button();
button4.setBounds(new Rectangle(91, 114, 32, 33));
button4.setLabel("4");
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"4");
textField.setText(textField.getText()+"4");
}

else
{
textField3.setText(textField3.getText()+"4");
textField.setText(textField.getText()+"4");
}

}
});
}
return button4;
}

/**
* This method initializes button5
*
* @return java.awt.Button
*/
private Button getButton5() {
if (button5 == null) {
button5 = new Button();
button5.setBounds(new Rectangle(146, 114, 32, 33));
button5.setLabel("5");
button5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"5");
textField.setText(textField.getText()+"5");
}
else
{
textField3.setText(textField3.getText()+"5");
textField.setText(textField.getText()+"5");
}
}
});
}
return button5;
}

/**
* This method initializes button6
*
* @return java.awt.Button
*/
private Button getButton6() {
if (button6 == null) {
button6 = new Button();
button6.setBounds(new Rectangle(30, 155, 32, 33));
button6.setLabel("6");
button6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"6");
textField.setText(textField.getText()+"6");
}
else
{
textField3.setText(textField3.getText()+"6");
textField.setText(textField.getText()+"6");
}
}
});
}
return button6;
}

/**
* This method initializes button7
*
* @return java.awt.Button
*/
private Button getButton7() {
if (button7 == null) {
button7 = new Button();
button7.setBounds(new Rectangle(91, 155, 32, 33));
button7.setLabel("7");
button7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"7");
textField.setText(textField.getText()+"7");
}
else
{
textField3.setText(textField3.getText()+"7");
textField.setText(textField.getText()+"7");
}
}
});
}
return button7;
}

/**
* This method initializes button8
*
* @return java.awt.Button
*/
private Button getButton8() {
if (button8 == null) {
button8 = new Button();
button8.setBounds(new Rectangle(146, 155, 32, 33));
button8.setLabel("8");
button8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"8");
textField.setText(textField.getText()+"8");
}
else
{
textField3.setText(textField3.getText()+"8");
textField.setText(textField.getText()+"8");
}
}
});
}
return button8;
}

/**
* This method initializes button9
*
* @return java.awt.Button
*/
private Button getButton9() {
if (button9 == null) {
button9 = new Button();
button9.setBounds(new Rectangle(30, 201, 32, 33));
button9.setLabel("9");
button9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"9");
textField.setText(textField.getText()+"9");
}
else
{
textField3.setText(textField3.getText()+"9");
textField.setText(textField.getText()+"9");
}
}
});
}
return button9;
}

/**
* This method initializes button10
*
* @return java.awt.Button
*/
private Button getButton10() {
if (button10 == null) {
button10 = new Button();
button10.setBounds(new Rectangle(91, 201, 32, 33));
button10.setLabel(".");
button10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
textField1.setText(textField1.getText()+".");
else
textField3.setText(textField3.getText()+".");
}
});
}
return button10;
}

/**
* This method initializes button11
*
* @return java.awt.Button
*/
private Button getButton11() {
if (button11 == null) {
button11 = new Button();
button11.setBounds(new Rectangle(270, 73, 32, 30));
button11.setLabel("+");
button11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("+");
textField.setText("");
}
});
}
return button11;
}

/**
* This method initializes button12
*
* @return java.awt.Button
*/
private Button getButton12() {
if (button12 == null) {
button12 = new Button();
button12.setBounds(new Rectangle(270, 115, 32, 30));
button12.setLabel("-");
button12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("-");
textField.setText("");
}
});
}
return button12;
}

/**
* This method initializes button13
*
* @return java.awt.Button
*/
private Button getButton13() {
if (button13 == null) {
button13 = new Button();
button13.setBounds(new Rectangle(270, 157, 32, 30));
button13.setLabel("*");
button13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("*");
textField.setText("");
}
});
}
return button13;
}

/**
* This method initializes button14
*
* @return java.awt.Button
*/
private Button getButton14() {
if (button14 == null) {
button14 = new Button();
button14.setBounds(new Rectangle(270, 199, 32, 30));
button14.setLabel("/");
button14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("/");
textField.setText("");
}
});
}
return button14;
}

/**
* This method initializes button15
*
* @return java.awt.Button
*/
private Button getButton15() {
if (button15 == null) {
button15 = new Button();
button15.setBounds(new Rectangle(146, 201, 32, 33));
button15.setLabel("=");
button15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

//double result;
double result=0;
double a=Double.parseDouble(textField1.getText());
double b=Double.parseDouble(textField3.getText());

if(textField2.getText().equals("+"))
result=a+b;
else
if(textField2.getText().equals("-"))
result=a-b;
else
if(textField2.getText().equals("*"))
result=a*b;
else
if(textField2.getText().equals("/")&&b!=0)
result=a/b;
textField2.setText("");
textField3.setText("");
String str=String.valueOf(result);
textField.setText(str);
textField1.setText(str);

}
});
}
return button15;
}

/**
* This method initializes textField1
*
* @return java.awt.TextField
*/
private TextField getTextField1() {
if (textField1 == null) {
textField1 = new TextField();
textField1.setBounds(new Rectangle(47, 242, 81, 24));
textField1.setText("");
textField1.setName("textfield1");
textField1.setVisible(true);
}
return textField1;
}

/**
* This method initializes textField2
*
* @return java.awt.TextField
*/
private TextField getTextField2() {
if (textField2 == null) {
textField2 = new TextField();
textField2.setBounds(new Rectangle(154, 242, 27, 24));
textField2.setName("textfield2");
}
return textField2;
}

/**
* This method initializes textField3
*
* @return java.awt.TextField
*/
private TextField getTextField3() {
if (textField3 == null) {
textField3 = new TextField();
textField3.setBounds(new Rectangle(211, 241, 81, 24));
textField3.setName("textfield3");
}
return textField3;
}

/**
* This method initializes button16
*
* @return java.awt.Button
*/
private Button getButton16() {
if (button16 == null) {
button16 = new Button();
button16.setBounds(new Rectangle(286, 29, 41, 29));
button16.setLabel("清空");
button16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField.setText("");
textField1.setText("");
textField2.setText("");
textField3.setText("");
}
});
button16.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
System.out.println("mouseEntered()"); // TODO Auto-generated Event stub mouseEntered()

JToolTip jtt=new JToolTip();//工具提示
jtt.setTipText("这是一个清空按钮");

}

});

}
return button16;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new 计算器();
}

/**
* This is the default constructor
*/
public 计算器() {
super();
initialize();

show();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(null);
this.setSize(339, 278);
this.setTitle("Frame");

this.add(getTextField(), null);
this.add(getButton(), null);
this.add(getButton1(), null);
this.add(getButton2(), null);
this.add(getButton3(), null);
this.add(getButton4(), null);
this.add(getButton5(), null);
this.add(getButton6(), null);
this.add(getButton7(), null);
this.add(getButton8(), null);
this.add(getButton9(), null);
this.add(getButton10(), null);
this.add(getButton11(), null);
this.add(getButton12(), null);
this.add(getButton13(), null);
this.add(getButton14(), null);
this.add(getButton15(), null);
this.add(getTextField1(), null);
this.add(getTextField2(), null);
this.add(getTextField3(), null);
this.add(getButton16(), null);
}

} // @jve:decl-index=0:visual-constraint="96,21"
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
szpkloveyou
推荐于2017-10-07 · TA获得超过123个赞
知道答主
回答量:37
采纳率:0%
帮助的人:24.8万
展开全部
这个是我自己写的。非常简单。也是激衡我上学时做的课程设计。
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
class Calculator extends JFrame implements ActionListener{
JPanel panel, panel1, panel2, panel3;
JLabel labelSpace;
JButton buttonCe;
JButton button[];
JButton buttonDot,buttonAdd,buttonSub,buttonMul,buttonDiv;
JButton buttonEqual;
JTextField textAnswer;
boolean clickable;
double vard,answerd;
short key = 0;
int times = 0;
public Calculator() {
super("明蠢做计算器");
clickable = true;
answerd = 0;
textAnswer = new JTextField(20);
textAnswer.setText("");
textAnswer.setEditable(false);
panel = new JPanel();
Container w1Container=this.getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(textAnswer,BorderLayout.CENTER);
panel.add(panel1,BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
labelSpace = new JLabel(" ");
buttonCe = new JButton("Ce"档乱);
buttonCe.addActionListener(this);
panel1.add(panel2,BorderLayout.NORTH);
panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
panel2.add(buttonCe);
panel3 = new JPanel();
panel1.add(panel3,BorderLayout.CENTER);
button = new JButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i));
}
buttonDot = new JButton(".");
buttonAdd = new JButton("+");
buttonSub = new JButton("-");
buttonMul = new JButton("*");
buttonDiv = new JButton("/");
buttonEqual = new JButton("=");
panel3.setLayout(new GridLayout(4,4));
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
w1Container.add(panel);
this.setSize(300,205);
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object temp = event.getSource();
try{
for (int i = 0; i <= 9; i++)
if (temp == button[i] && clickable == true){
textAnswer.setText(textAnswer.getText() + Integer.toString(i));
}
if (temp == buttonDot && clickable == true) {
boolean isDot = false;
if (textAnswer.getText().length() == 0)
isDot = true;
for (int i = 0; i < textAnswer.getText().length(); i++){
if ('.' == textAnswer.getText().charAt(i)){
isDot = true;
break;
}
}
if (isDot == false)
textAnswer.setText(textAnswer.getText() + ".");
}
if ( (temp==buttonAdd||temp==buttonSub||temp==buttonMul||temp==buttonDiv)&&clickable==true){
if(temp == buttonAdd){
if(times==0){
answerd += Double.parseDouble(textAnswer.getText());
times++;
}
textAnswer.setText("");
key = 1;
}
if(temp == buttonSub){
if(times==0){
answerd += Double.parseDouble(textAnswer.getText());
times++;
}
textAnswer.setText("");
key = 2;
}
if(temp == buttonMul){
if(times==0){
answerd += Double.parseDouble(textAnswer.getText());
times++;
}
textAnswer.setText("");
key = 3;
}
if(temp == buttonDiv){
if(times==0){
answerd += Double.parseDouble(textAnswer.getText());
times++;
}
textAnswer.setText("");
key = 4;
}
}
if(temp == buttonEqual && clickable == true) {
if(times>0){
vard = Double.parseDouble(textAnswer.getText());
}
if (key == 1) {
answerd += vard;
textAnswer.setText(Double.toString(answerd));
}
if (key == 2) {
answerd -= vard;
textAnswer.setText(Double.toString(answerd));
}
if (key == 3) {
answerd *= vard;
textAnswer.setText(Double.toString(answerd));
}
if (key == 4) {
if (vard == 0) {
textAnswer.setText("除数不能为零");
clickable = false;
}
else {
answerd /= vard;
textAnswer.setText(Double.toString(answerd));
}
}
}
if (temp == buttonCe) {
vard = answerd = times = 0;
textAnswer.setText("");
clickable = true;
}
}
catch(Exception e){
textAnswer.setText("非法操作");
clickable = false;
}
}

public static void main(String args[]) {
Calculator calculator = new Calculator();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友0df1ab6
推荐于2017-09-17 · TA获得超过147个赞
知道小有建树答主
回答量:194
采纳率:0%
帮助的人:106万
展开全部
这个主要是处理后台的表达式计算,有几种方法,介绍两敬李种,
一种是:jdk6以上用的;类ScriptEngine,用法如下:
public class CalTest {
public static void main(String[] args) throws Exception {
ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
Object result = jse.eval("3*3*(3-1)-3*2");
System.out.println(result );
}
}
另外一种:需要下载jeval.jar的包团纤。用法如下:
public class CalTest {
public static void main(String[] args) throws Exception {
Evaluator evl = new Evaluator();
String exp= "亮或迟2 + (7-5) * 3.14159 * #{x} + sin(#{y})";

evl.putVariable("x", 5);
evl.putVariable("y", 30);

double result = Double.parseDouble(evl.evaluate(exp));
System.out.println(result );

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式