有相应的函数没........
[此贴子已经被作者于2006-10-22 19:02:12编辑过]
我写了一个计数器的程序....
框架都搞好了...就是这有问题
我这有程序 大家帮忙
改下
[CODE]import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame{
private JTextField tf1;
private Container cp;
double c1,c2,r;
int c,i,j;
boolean flag=true;
String s=new String();
String str="";
String vod="";
private String btn[]=
{"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","."
};
JButton b[]=new JButton[16];
public Calculator(){
setTitle("Calculator");
setSize(300,200);
cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
Font ft1=new Font("Times",Font.PLAIN,20);
Font ft2=new Font("too",Font.PLAIN,14);
tf1=new JTextField(26);
jp1.add(tf1);
jp3.setLayout(new GridLayout(4,4));
for(j=0;j<btn.length;j++){
b[j]=new JButton(btn[j]);
jp3.add(b[j]);
b[j].setFont(ft1);
b[j].addActionListener(new CalButton());
}
cp.add(jp1,BorderLayout.NORTH);
cp.add(jp3,BorderLayout.CENTER);
addWindowListener(new WindowDestroyer());
}
class CalButton implements ActionListener
{
public void actionPerformed(ActionEvent e){
for (i=0 ;i<10;i++){
if (e.getSource()==b[i]){
tf1.setText("");
if (flag){
str=str+e.getActionCommand();
tf1.setText(str);
c1=i;
}
else{
str=str+e.getActionCommand();
tf1.setText(str);
c2=i;
}
}
}
if (e.getSource()==b[14]){
tf1.setText("");
r=0;
str=str+e.getActionCommand();
tf1.setText("");
switch(c){
case 0:r=c1+c2;s=Double.toString(r);tf1.setText(s);break;
case 1:r=c1-c2;s=Double.toString(r);tf1.setText(s);break;
case 2:r=c1*c2;s=Double.toString(r);tf1.setText(s);break;
case 3:r=c1/c2;s=Double.toString(r);tf1.setText(s);break;
};
c1=r;
};
for (int k=10 ;k<14;k++){
if (e.getSource()==b[k])
{ tf1.setText("");
c=k-10;
flag=false;
str=str+e.getActionCommand();
tf1.setText(str);}
}
}
}
}
class WindowDestroyer extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public class jsb{
public static void main(String args[]){
Calculator win=new Calculator();
win.setVisible(true);
}
}[/CODE]