java 如何清空发送框_JTextField如何清空文本框的内容

我写了一个计数器的程序....

框架都搞好了...就是这有问题

我这有程序 大家帮忙

改下

[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

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]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值