import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JTextField implements ActionListener
{
Frame cal;
double num_show,num_back,result;
double pow;
char oper;
boolean point_pressed,oper_pressed;
JTextField tf;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button b_plus,b_minus,b_time,b_divide,b_equal,b_point,b_clear;
public void display()
{
num_show=num_back=result=0;
pow=1;
oper='+';
point_pressed=oper_pressed=false;
cal=new Frame("Calculator");
cal.setSize(140,200);
cal.setBackground(Color.white);
cal.setLocation(442,284);
cal.setLayout(new FlowLayout(FlowLayout.LEFT));
cal.setResizable(false);
cal.setCursor(Cursor.HAND_CURSOR);
tf=new JTextField(10);
tf.setEditable(false);
tf.setHorizontalAlignment(tf.RIGHT);
cal.add(tf);
b0=new Button("0");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b_plus=new Button("+");
b_minus=new Button("-");
b_time=new Button("*");
b_divide=new Button("/");
b_equal=new Button("=");
b_point=new Button("..");
b_clear=new Button("clear");
cal.add(b1);
cal.add(b2);
cal.add(b3);
cal.add(b_plus);
cal.add(b4);
cal.add(b5);
cal.add(b6);
cal.add(b_minus);
cal.add(b7);
cal.add(b8);
cal.add(b9);
cal.add(b_time);
cal.add(b_point);
cal.add(b0);
cal.add(b_equal);
cal.add(b_divide);
cal.add(b_clear);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b_plus.addActionListener(this);
b_minus.addActionListener(this);
b_time.addActionListener(this);
b_divide.addActionListener(this);
b_equal.addActionListener(this);
b_point.addActionListener(this);
b_clear.addActionListener(this);
cal.addWindowListener(new WinClose());
cal.setVisible(true);
}
public static void main(String arg[])
{
Calculator cc=new Calculator();
cc.display();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b_clear)
{
num_show=num_back=result=0;
oper='+';
pow=1;
point_pressed= oper_pressed=false;
tf.setText("");
}
if(e.getSource()==b0)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+0;
tf.setText(String.valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+0*pow;
tf.setText(String.valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b1)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+1;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+1*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b2)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+2;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+2*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b3)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+3;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+3*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b4)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+4;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+4*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b5)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+5;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+5*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b6)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+6;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+6*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b7)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+7;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+7*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b8)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+8;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+8*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b9)
{
if(this.point_pressed == false)
{
this.num_show=this.num_show*10+9;
tf.setText((new String()).valueOf(this.num_show));
}
if(this.point_pressed == true)
{
this.num_show=this.num_show+9*pow;
tf.setText((new String()).valueOf(this.num_show));
pow=pow/10;
}
}
if(e.getSource()==b_plus)
{
switch(oper)
{
case '+':
this.num_back=this.num_back+this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '-':
this.num_back=this.num_back-this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '*':
this.num_back=this.num_back*this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '/':
this.num_back=this.num_back/this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
}
oper='+';
pow=1;
point_pressed=false;
}
if(e.getSource()==b_minus)
{
switch(oper)
{
case '+':
this.num_back=this.num_back+this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '-':
this.num_back=this.num_back-this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '*':
this.num_back=this.num_back*this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '/':
this.num_back=this.num_back/this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
}
oper='-';
pow=1;
point_pressed=false;
}
if(e.getSource()==b_time)
{
switch(oper)
{
case '+':
this.num_back=this.num_back+this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '-':
this.num_back=this.num_back-this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '*':
this.num_back=this.num_back*this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '/':
this.num_back=this.num_back/this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
}
oper='*';
pow=1;
point_pressed=false;
}
if(e.getSource()==b_divide)
{
switch(oper)
{
case '+':
this.num_back=this.num_back+this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '-':
this.num_back=this.num_back-this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '*':
this.num_back=this.num_back*this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '/':
this.num_back=this.num_back/this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
}
oper='/';
pow=1;
point_pressed=false;
}
if(e.getSource()==b_point)
{
if(point_pressed==false)
{
point_pressed=true;
pow=pow/10;
}
}
if(e.getSource()==b_equal)
{
switch(oper)
{
case '+':
this.num_back=this.num_back+this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '-':
this.num_back=this.num_back-this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '*':
this.num_back=this.num_back*this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
case '/':
this.num_back=this.num_back/this.num_show;
this.num_show=0;
tf.setText((new String()).valueOf(this.num_back));
break;
}
pow=1;
point_pressed=false;
}
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
无聊ing~~写了个计算器大小程序~~贴出来分享一下
-
- 帖子: 10
- 注册时间: 2006-09-28 15:25
无聊ing~~写了个计算器大小程序~~贴出来分享一下
上次由 tuliangde 在 2006-10-16 17:18,总共编辑 1 次。
The God said: " let there be light." I denied. So there be darkness.
-
- 帖子: 10
- 注册时间: 2006-09-28 15:25
-
- 帖子: 26
- 注册时间: 2006-07-20 15:44
大的
root@zrt-desktop:/home/zrt# javac /home/zrt/Desktop/Calculator.java
注意: /home/zrt/Desktop/Calculator.java 使用或覆盖了已过时的 API。
注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。
root@zrt-desktop:/home/zrt# java /home/zrt/Desktop/Calculator
Exception in thread "main" java.lang.NoClassDefFoundError: /home/zrt/Desktop/Calculator
这是怎么回事阿
帮我解决一下
注意: /home/zrt/Desktop/Calculator.java 使用或覆盖了已过时的 API。
注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。
root@zrt-desktop:/home/zrt# java /home/zrt/Desktop/Calculator
Exception in thread "main" java.lang.NoClassDefFoundError: /home/zrt/Desktop/Calculator
这是怎么回事阿
帮我解决一下
-
- 帖子: 16
- 注册时间: 2006-10-18 1:21
-
- 帖子: 10
- 注册时间: 2006-09-28 15:25
Re: 大的
环境变量设置的问题.77519697 写了:root@zrt-desktop:/home/zrt# javac /home/zrt/Desktop/Calculator.java
注意: /home/zrt/Desktop/Calculator.java 使用或覆盖了已过时的 API。
注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。
root@zrt-desktop:/home/zrt# java /home/zrt/Desktop/Calculator
Exception in thread "main" java.lang.NoClassDefFoundError: /home/zrt/Desktop/Calculator
这是怎么回事阿
帮我解决一下
运行的时候用下面这个命令就可以了
$java -cp /home/zrt/Desktop/ Calculator