当前位置: 代码迷 >> Java相关 >> JTextField如何清空文本框的内容
  详细解决方案

JTextField如何清空文本框的内容

热度:293   发布时间:2006-10-22 18:12:31.0
JTextField如何清空文本框的内容
JTextField如何清空文本框的内容!!!!
有相应的函数没........

[此贴子已经被作者于2006-10-22 19:02:12编辑过]

搜索更多相关的解决方案: 空文  JTextField  

----------------解决方案--------------------------------------------------------
你想问什么啊,如果要获取内容就用getText()
----------------解决方案--------------------------------------------------------

是清空..
一般不是用 变量.setText("");
但是我的用了没有用.....
以前的字符还在上面那......


----------------解决方案--------------------------------------------------------

我写了一个计数器的程序....
框架都搞好了...就是这有问题
我这有程序 大家帮忙
改下
[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]


----------------解决方案--------------------------------------------------------

你的程序没有错误,但是现在你的程序并没有清空JTextField里面的东西啊!就是用*.settext("");


----------------解决方案--------------------------------------------------------

6楼的 用jTextField1.setDocument(new PlainDocument());是不是也可以啊?


----------------解决方案--------------------------------------------------------
我的问题解决了 不是setText("");没用
是因为我的程序里的setText(str);
字符串str
再次使用之前没有清空...
[CODE]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);
str=" ";//加上这条就好了!!1}

}[/CODE]

[此贴子已经被作者于2006-10-23 18:21:49编辑过]


----------------解决方案--------------------------------------------------------