java经典源代码大全 java代码源码( 四 )


if (d10)
{
ta.setText( "error");
}
else if (d1==0)
{
ta.setText( "0.0");
}
else {
for (int k=1;k=d1 ;k++ )
d4=d4*k;
ta.setText( d4.toString());
}
}
if (e.getActionCommand()=="sin")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sin(3.1415926*d1/180);
ta.setText( d2.toString());
}
}
}
求Java编的简单计算器源代码package edu.hpu.yyf;
import java.awt.*;
import java.awt.event.*;
public class MyCalculator {
private static double d1 = 0.0;
private static double d2 = 0.0;
private static String s1 = new String("0");
private static String s2 = new String("0");
private static char c1 ='0';
private static void judgec1(TextField text){
switch(c1){
case '+':s2 = String.valueOf(d1+d2);d1=d1+d2;d2=0;text.setText(s2);s2="0";break;
case '-':s2 = String.valueOf(d1-d2);d1=d1-d2;d2=0;text.setText(s2);s2="0";break;
case '*':s2 = String.valueOf(d1*d2);d1=d1*d2;d2=0;text.setText(s2);s2="0";break;
case '/':if(d2==0) {text.setText("0");d1=0;break;}s2 = String.valueOf(d1/d2);d1=d1/d2;d2=0;text.setText(s2);s2="0";break;}
}
public static void main(String [] args){
Frame cclt = new Frame("我的计算器");
cclt.setBounds(300,150,300,265);
cclt.setLayout(null);
final TextField text = new TextField();
text.setBounds(10, 30, 280, 35);
text.setText("0");
text.setEditable(false);
Font font = new Font("", 5, 25);
text.setFont(font);
cclt.add(text);
Panel panel = new Panel();
Font font_1 = new Font("", 5, 20);
panel.setFont(font_1);
panel.setBounds(5, 67, 290, 193);
panel.setBackground(Color.GREEN);
panel.setLayout(new GridLayout(5,4,5,5));
cclt.add(panel);
Button space = new Button();
panel.add(space);
Button Backspace = new Button("Backspace");
Font font_2 = new Font("", 0, 14);
Backspace.setFont(font_2);
panel.add(Backspace);
Button CE = new Button("CE");
panel.add(CE);
Button C = new Button("C");
panel.add(C);
Button seven = new Button("7");
panel.add(seven);
Button eight = new Button("8");
panel.add(eight);
Button nine = new Button("9");
panel.add(nine);
Button but = new Button("/");
panel.add(but);
Button four = new Button("4");
panel.add(four);
Button five = new Button("5");
panel.add(five);
Button six = new Button("6");
panel.add(six);
Button ride = new Button("*");
panel.add(ride);
Button one = new Button("1");
panel.add(one);
Button two = new Button("2");
panel.add(two);
Button three = new Button("3");
panel.add(three);
Button substract = new Button("-");
panel.add(substract);
Button zero = new Button("0");
panel.add(zero);
Button space_1 = new Button(" ");
panel.add(space_1);
Button equal = new Button("=");
panel.add(equal);
Button add = new Button("+");
panel.add(add);
CE.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
d1=0;
d2=0;
s1="0";
s2="0";
c1 ='0';
text.setText("0");
}
});
seven.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="7";
else {text.setText(s1+"7");
s1+="7";}
text.setText(s1);
}
});
eight.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(s1.equals("0")) s1="8";
else {text.setText(s1+"8");
s1+="8";}
text.setText(s1);
}
});
nine.addActionListener(new ActionListener(){

推荐阅读