是要用applet查看器运行的,麻烦各位帮下忙,看一下为什么不行。谢谢
import java.applet.*;
import java.awt.*;
import java.awt.event.*; public class sj4_1 extends Applet implements ActionListener{
private Button[] b=new Button[17];
private String[] bt={"1","2","3","+","-","4","5","6","*","/","7","8","9","C","0",".","="};
private TextField tf1=new TextField(10);
private String input="";
private Label l=new Label("结果");
private int oper=0,oldoper=0,newoper=0; //记录运算过程中的运算符
private double answer,num1=0.0,num2=0.0,num3=0.0;
private final int ADD=1,SUB=2,MUL=3,DIV=4;
private boolean firstpress=true,morenums=false,equals=false,clearscreen=false,dec=false,doubleclick=false;
public void init(){
for (int i=0;i<17;i++){
b[i]=new Button(bt[i]);
b[i].addActionListener(this);
}
add(tf1);
add(b[13]);
add(b[10]);
add(b[11]);
add(b[12]);
add(b[9]);
add(b[5]);
add(b[6]);
add(b[7]);
add(b[8]);
add(b[0]);
add(b[1]);
add(b[2]);
add(b[4]);
add(b[14]);
add(b[15]);
add(b[16]);
add(b[3]);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()!=b[3]&&e.getSource()!=b[4]&&e.getSource()!=b[8]&&e.getSource()!=b[9]&&e.getSource()!=b[13]&&e.getSource()!=b[16]){
if(clearscreen){
clearScreen();
clearscreen=false; //是否按下第一个按键
}
if(e.getSource()==b[0]){
input+="1";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[1]){
input+="2";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[2]){
input+="3";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[5]){
input+="4";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[6]){
input+="5";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[7]){
input+="6";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[10]){
input+="7";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[11]){
input+="8";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[12]){
input+="9";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[14]){
input+="0";
tf1.setText(input);
showAnswer(input);
}
else if(e.getSource()==b[15]){
if(dec==false){ //是否输入一个浮点数
dec=true;
input+="."; //输入为小数点
showAnswer(input);
}
}
}
if(e.getSource()==b[3]){
clearscreen=false;
dec=false;
oper=ADD; //将运算符设置为加
clickCheck(input); //检查是否为双击运算符
if(doubleclick==false)
processNumbers(); //如果没有双击,继续处理
input=""; //记录输入值的变量清空
}
if(e.getSource()==b[4]){
clearscreen=false;
dec=false;
oper=SUB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[8]){
clearscreen=false;
dec=false;
oper=MUL;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[9]){
clearscreen=false;
dec=false;
oper=DIV;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[13]){
clearScreen();
}
if(e.getSource()==b[16]){
equals=true;
clearscreen=false;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input=Double.toString(answer);
}
}
public void processNumbers(){ //如果没有双击,继续处理
if(firstpress){
if(equals){
num1=answer;
equals=false;
}
else
num1=Double.valueOf(input).doubleValue();
oldoper=oper;
}
}
public double claculate(int oper,double number1,double number2){
double answer=0.0;
switch(oper){
case ADD:
answer=number1+number2;
break;
case SUB:
answer=number1-number2;
break;
case MUL:
answer=number1*number2;
break;
case DIV:
answer=number1/number2;
break;
}
return answer;
}
public void showAnswer(String s){
double answer;
answer =Double.valueOf(s).doubleValue();
if(dec)
tf1.setText(Double.toString(answer));
else
tf1.setText(s);
}
public boolean clickCheck(String s){
if(s=="")
doubleclick=true;
else
doubleclick=false;
return doubleclick;
}
public void clearScreen(){
oper=0;
input="";
answer=0;
dec=false;
morenums=false;
firstpress=true;
equals=false;
showAnswer(Integer.toString((int)answer));
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*; public class sj4_1 extends Applet implements ActionListener{
private Button[] b=new Button[17];
private String[] bt={"1","2","3","+","-","4","5","6","*","/","7","8","9","C","0",".","="};
private TextField tf1=new TextField(10);
private String input="";
private Label l=new Label("结果");
private int oper=0,oldoper=0,newoper=0; //记录运算过程中的运算符
private double answer,num1=0.0,num2=0.0,num3=0.0;
private final int ADD=1,SUB=2,MUL=3,DIV=4;
private boolean firstpress=true,morenums=false,equals=false,clearscreen=false,dec=false,doubleclick=false;
public void init(){
for (int i=0;i<17;i++){
b[i]=new Button(bt[i]);
b[i].addActionListener(this);
}
add(tf1);
add(b[13]);
add(b[10]);
add(b[11]);
add(b[12]);
add(b[9]);
add(b[5]);
add(b[6]);
add(b[7]);
add(b[8]);
add(b[0]);
add(b[1]);
add(b[2]);
add(b[4]);
add(b[14]);
add(b[15]);
add(b[16]);
add(b[3]);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()!=b[3]&&e.getSource()!=b[4]&&e.getSource()!=b[8]&&e.getSource()!=b[9]&&e.getSource()!=b[13]&&e.getSource()!=b[16]){
if(clearscreen){
clearScreen();
clearscreen=false; //是否按下第一个按键
}
if(e.getSource()==b[0]){
input+="1";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[1]){
input+="2";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[2]){
input+="3";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[5]){
input+="4";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[6]){
input+="5";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[7]){
input+="6";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[10]){
input+="7";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[11]){
input+="8";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[12]){
input+="9";
tf1.setText(input);
showAnswer(input);
}
if(e.getSource()==b[14]){
input+="0";
tf1.setText(input);
showAnswer(input);
}
else if(e.getSource()==b[15]){
if(dec==false){ //是否输入一个浮点数
dec=true;
input+="."; //输入为小数点
showAnswer(input);
}
}
}
if(e.getSource()==b[3]){
clearscreen=false;
dec=false;
oper=ADD; //将运算符设置为加
clickCheck(input); //检查是否为双击运算符
if(doubleclick==false)
processNumbers(); //如果没有双击,继续处理
input=""; //记录输入值的变量清空
}
if(e.getSource()==b[4]){
clearscreen=false;
dec=false;
oper=SUB;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[8]){
clearscreen=false;
dec=false;
oper=MUL;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[9]){
clearscreen=false;
dec=false;
oper=DIV;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input="";
}
if(e.getSource()==b[13]){
clearScreen();
}
if(e.getSource()==b[16]){
equals=true;
clearscreen=false;
clickCheck(input);
if(doubleclick==false)
processNumbers();
input=Double.toString(answer);
}
}
public void processNumbers(){ //如果没有双击,继续处理
if(firstpress){
if(equals){
num1=answer;
equals=false;
}
else
num1=Double.valueOf(input).doubleValue();
oldoper=oper;
}
}
public double claculate(int oper,double number1,double number2){
double answer=0.0;
switch(oper){
case ADD:
answer=number1+number2;
break;
case SUB:
answer=number1-number2;
break;
case MUL:
answer=number1*number2;
break;
case DIV:
answer=number1/number2;
break;
}
return answer;
}
public void showAnswer(String s){
double answer;
answer =Double.valueOf(s).doubleValue();
if(dec)
tf1.setText(Double.toString(answer));
else
tf1.setText(s);
}
public boolean clickCheck(String s){
if(s=="")
doubleclick=true;
else
doubleclick=false;
return doubleclick;
}
public void clearScreen(){
oper=0;
input="";
answer=0;
dec=false;
morenums=false;
firstpress=true;
equals=false;
showAnswer(Integer.toString((int)answer));
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JApplet implements ActionListener,KeyListener
{
double num1;
double num2;
int tag;
int i;
int result;
String s = new String();
JTextField jft = new JTextField();
JButton[] jbt=new JButton[10];
JButton jbtAdd = new JButton("+");
JButton jbtSub = new JButton("-");
JButton jbtMul = new JButton("*");
JButton jbtPoint = new JButton(".");
JButton jbtEqual = new JButton("=");
JButton jbtDiv = new JButton("/");
JButton jbtBack = new JButton("Back");
JButton jbtCE =new JButton("CE");
JButton jbtC = new JButton("C");
public void init()
{
for(i=0;i<10;i++) jbt[i]=new JButton(String.valueOf(i));
Container container = getContentPane();
container.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4,4));
for(i=7;i<=9;i++) p1.add(jbt[i]);
p1.add(jbtAdd);
for(i=4;i<=6;i++) p1.add(jbt[i]);
p1.add(jbtSub);
for(i=1;i<=3;i++) p1.add(jbt[i]);
p1.add(jbtMul);
p1.add(jbt[0]);
p1.add(jbtPoint);
p1.add(jbtEqual);
p1.add(jbtDiv);
jft.setHorizontalAlignment(JTextField.RIGHT);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,3));
p3.add( jbtBack );
p3.add( jbtCE );
p3.add( jbtC );
JPanel p4 = new JPanel();
p4.setLayout(new BorderLayout());
p4.add(p3 ,BorderLayout.NORTH);
p4.add(p1 ,BorderLayout.CENTER);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(jft ,BorderLayout.NORTH);
p2.add(p4 ,BorderLayout.CENTER);
container.add( p2,BorderLayout.CENTER);
for(i=0;i<10;i++) jbt[i].addActionListener(this);
jbtAdd.addActionListener(this);
jbtSub.addActionListener(this);
jbtMul.addActionListener(this);
jbtDiv.addActionListener(this);
jbtPoint.addActionListener(this);
jbtEqual.addActionListener(this);
jbtBack.addActionListener(this);
jbtCE.addActionListener(this);
jbtC.addActionListener(this);
jft.addKeyListener(this);
}
public static void main(String[] args)
{ JFrame frame = new JFrame("Calculator");
Calculator applet=new Calculator();
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(200,250);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
throws RuntimeException
{
try
{
for(i=0;i<10;i++)
if ( e.getSource() == jbt[i] ) jft.setText( ( jft.getText() )+i);
if ( e.getSource() == jbtPoint ) jft.setText( ( jft.getText() )+".");
if ( e.getSource() == jbtAdd)
{
num1=Double.parseDouble( jft.getText() );
tag=1;
jft.setText("");
}
if ( e.getSource() == jbtSub)
{
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=2;
}
if ( e.getSource() == jbtMul)
{
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=3;
}
if ( e.getSource() == jbtDiv)
{
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=4;
}
if ( e.getSource() == jbtEqual)
{
s=jft.getText();
num2=Double.parseDouble(s);
if(num1>=100000||num2>=100000) throw new RuntimeException("The data is overflow!");
jft.setText("");
calculate();
}
if ( e.getSource() == jbtCE) jft.setText("0");
if ( e.getSource() == jbtC){
jft.setText("");
this.tag=0;
}
if ( e.getSource() == jbtBack){
s=jft.getText();
s=s.substring(0,s.length()-1);
jft.setText(s);
}
}
catch(Exception ex)
{
System.out.println(ex);
jft.setText("The data is overflow!");
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e) throws RuntimeException
{
try
{
switch (e.getKeyChar())
{
case '+': {
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=1;
break;
}
case '-': {
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=2;
break;
}
case '*': {
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=3;
break;}
case '/': {
num1=Double.parseDouble( jft.getText() );
jft.setText("");
tag=4;
break;
}
case '=': {
s=jft.getText();
s=s.substring(1,s.length());
num2=Double.parseDouble(s);
jft.setText("");
calculate();
break;
}
}
if(num1>=100000||num2>=100000) throw new RuntimeException("The data is overflow!");
}
catch(Exception ex)
{
System.out.println(ex);
jft.setText("The data is overflow!");
}
}
public void calculate()
{
try
{
if(tag==1) jft.setText(""+(num1+num2));
if(tag==2) jft.setText(""+(num1-num2));
if(tag==3) jft.setText(""+(num1*num2));
if(tag==4)
{
if(num2==0) throw new RuntimeException("Denominator cannot be 0!");
else jft.setText(""+(num1/num2));
}
}
catch(Exception ex)
{
System.out.println(ex);
jft.setText("Denominator cannot be 0!");
}
}
}