这是我写的一个小程序,是一个很简单的计算的程序,只有加减乘除,可是总是弄不对,我做好外壳了,高手能帮我把那加减乘除4个方法实现了吗?谢谢了import java.awt.*;
import java.awt.event.*;public class MyCalculator extends Frame {
//int a,b,c;
Frame f = new Frame("Small Calculator");
Panel p = new Panel();
Button b1= new Button("加");
Button b2= new Button("减");
Button b3= new Button("乘");
Button b4= new Button("除");
TextField t1 = new TextField("",20);
TextField t2 = new TextField("",20);
TextField t3 = new TextField("",20);
Label la1 = new Label("请输入一个数");
Label la2 = new Label("请输入另一个数");
Label la3 = new Label("结果");
Label la4 = new Label();
double a;
double b;
double c;
public MyCalculator () {
//int a = (int)t1.getText();
//int b = (int)t2.getText();
//int c = (int)t3.getText();
f.add(p);
p.add(la1);
p.add(t1);
p.add(la2);
p.add(t2);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(la3);
p.add(t3);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
f.setLayout(new BorderLayout(5,5));
// b1.addActionListener(new ButtonListener1());
}
/*class ButtonListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
a = (Double.parseDouble(t1.getText()));
b = (Double.parseDouble(t2.getText()));
c = (Double.parseDouble(t3.getText()));
c = a+b;
t3.setText((Double.parseDouble(t1.getText()))+(Double.parseDouble(t2.getText())));
}
}
class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(((Double.parseDouble(t1.getText())) ==null) & ((Double.parseDouble(t2.getText())) ==null)) {
}
}
}*/
class ButtonListener3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t3.setText() = t1.getText()*t2.getText();
}
}
class ButtonListener4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if( ((Double.parseDouble(t2.getText())) ==0)) {
System.out.println("除数不能为0");
}
}
}
public static void main (String [] args) {
MyCalculator a = new MyCalculator();
}
}
import java.awt.event.*;public class MyCalculator extends Frame {
//int a,b,c;
Frame f = new Frame("Small Calculator");
Panel p = new Panel();
Button b1= new Button("加");
Button b2= new Button("减");
Button b3= new Button("乘");
Button b4= new Button("除");
TextField t1 = new TextField("",20);
TextField t2 = new TextField("",20);
TextField t3 = new TextField("",20);
Label la1 = new Label("请输入一个数");
Label la2 = new Label("请输入另一个数");
Label la3 = new Label("结果");
Label la4 = new Label();
double a;
double b;
double c;
public MyCalculator () {
//int a = (int)t1.getText();
//int b = (int)t2.getText();
//int c = (int)t3.getText();
f.add(p);
p.add(la1);
p.add(t1);
p.add(la2);
p.add(t2);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(la3);
p.add(t3);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
f.setLayout(new BorderLayout(5,5));
// b1.addActionListener(new ButtonListener1());
}
/*class ButtonListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
a = (Double.parseDouble(t1.getText()));
b = (Double.parseDouble(t2.getText()));
c = (Double.parseDouble(t3.getText()));
c = a+b;
t3.setText((Double.parseDouble(t1.getText()))+(Double.parseDouble(t2.getText())));
}
}
class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(((Double.parseDouble(t1.getText())) ==null) & ((Double.parseDouble(t2.getText())) ==null)) {
}
}
}*/
class ButtonListener3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
t3.setText() = t1.getText()*t2.getText();
}
}
class ButtonListener4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if( ((Double.parseDouble(t2.getText())) ==0)) {
System.out.println("除数不能为0");
}
}
}
public static void main (String [] args) {
MyCalculator a = new MyCalculator();
}
}
import java.awt.event.*;
public class MyCalculator extends Frame {
Frame f = new Frame("Small Calculator"); Panel p = new Panel(); Button add= new Button("加");
Button decrease= new Button("减");
Button multiply= new Button("乘");
Button divide= new Button("除"); TextField t1 = new TextField("",20);
TextField t2 = new TextField("",20);
TextField t3 = new TextField("",20); Label la1 = new Label("请输入一个数");
Label la2 = new Label("请输入另一个数");
Label la3 = new Label("结果");
Label la4 = new Label();
double a;
double b;
double c;
public static String s;
public MyCalculator () {
p.add(la1);
p.add(t1); p.add(la2);
p.add(t2); p.add(add);
p.add(decrease);
p.add(multiply);
p.add(divide);
p.add(la3);
p.add(t3);
f.add(p);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
f.setLayout(new BorderLayout(5,5));
add.addActionListener(new ButtonListener1());
decrease.addActionListener(new ButtonListener2());
multiply.addActionListener(new ButtonListener3());
divide.addActionListener(new ButtonListener4());
}class ButtonListener1 implements ActionListener {public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(t1.getText());
b = Double.parseDouble(t2.getText());
c = a+b;
s=Double.toString(c);
t3.setText(s);
} }
class ButtonListener2 implements ActionListener { public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(t1.getText());
b = Double.parseDouble(t2.getText());
c=a-b;
s=Double.toString(c);
t3.setText(s);} }
class ButtonListener3 implements ActionListener { public void actionPerformed(ActionEvent e) {
a = Double.parseDouble(t1.getText());
b = Double.parseDouble(t2.getText());
c=a*b;
s=Double.toString(c);
t3.setText(s);
}}
class ButtonListener4 implements ActionListener {public void actionPerformed(ActionEvent e) { if( ((Double.parseDouble(t2.getText())) ==0)) {
java.awt.Dialog dialog=new java.awt.Dialog(f, "除数不能为");
t3.setText("除数不能为0!");
}else{
a = Double.parseDouble(t1.getText());
b = Double.parseDouble(t2.getText());
c=a/b;
c+=a%b;
s=Double.toString(c);
t3.setText(s);
}
}}
public static void main (String [] args) {
MyCalculator a = new MyCalculator();
}
}
a = Double.parseDouble(t1.getText());
b = Double.parseDouble(t2.getText());
c=a/b;
s=Double.toString(c);
String s1=s.substring(0,16);
t3.setText(s1);
}
import java.awt.event.*;
public class My_Cal
{
public static void main(String arg[])
{
My_Frame1 mc1=new My_Frame1();
mc1.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}});
}
}
class My_Frame1 extends Frame
{
static String a=new String("");
static String b=new String("");
static String c=new String("");
Panel p1=new Panel();
TextField t1=new TextField("",20);
// b1.setLabel("adf");
Button b1[]=new Button[10];
Button b2[]={new Button("+"),new Button("-"),new Button("*"),new Button("/"),new Button("=")};
Menu menu1=new Menu("编辑");
Menu menu2=new Menu("设置");
Menu menu3=new Menu("帮助");
MenuBar mb1=new MenuBar();
MenuItem mi11=new MenuItem("复制");
MenuItem mi12=new MenuItem("粘贴");
MenuItem mi21=new MenuItem("color");
MenuItem mi31=new MenuItem("帮助主题");
MenuItem mi32=new MenuItem("关于");
My_Frame1()
{
mb1.add(menu1);
mb1.add(menu2);
mb1.add(menu3);
menu1.add(mi11);
menu1.add(mi12);
menu2.add(mi21);
menu3.add(mi31);
menu3.add(mi32);
this.setTitle("我的计算器");
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(4,4,5,5));
for(int j=0;j<4;j++)
{
p1.add(b2[j]);
b2[j].addActionListener(new A_mycal());
}
for(int i=0;i<10;i++)
{
b1[i]=new Button(" "+i);
p1.add(b1[i]);
b1[i].addActionListener(new A_mycal());
}
p1.add(b2[4]);
b2[4].addActionListener(new A_mycal());
// b1[1].setBounds(10,10,40,25);
this.setMenuBar(mb1);
this.add(t1,BorderLayout.NORTH);
this.add(p1,BorderLayout.CENTER);
this.setBounds(350,300,200,200);
this.setResizable(false);
this.setVisible(true);
}
class A_mycal implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object o1=e.getSource();
for(int i=0;i<10;i++)
if(o1==b1[i])
{
// a=a+b1[i].getLabel();
a=a+i;
t1.setText(a);
break;
}
for(int j=0;j<4;j++)
if(o1==b2[j])
{
t1.setText("");
b=a;
a="";
c=b2[j].getLabel();
break;
}
if(o1==b2[4])
{ if(a.equals(""))
a="0";
if(b.equals(""))
b="0";
if(c.equals("+"))
{
int r=Integer.parseInt(b)+Integer.parseInt(a);
t1.setText(""+r);
a="";
b="";
}
if(c.equals("-"))
{int r=Integer.parseInt(b)-Integer.parseInt(a);
t1.setText(""+r);
a="";
b="";}
if(c.equals("*"))
{int r=Integer.parseInt(b)*Integer.parseInt(a);
t1.setText(""+r);
a="";
b="";}
if(c.equals("/"))
{if(a.equals("0"))
t1.setText("分母不能为0");
else
{
int r=Integer.parseInt(b)/Integer.parseInt(a);
t1.setText(""+r);
}
a="";
b="";
}
}
}
}
}