读书人

不用内部类实现清空text,该如何解决

发布时间: 2012-03-09 21:42:55 作者: rapoo

不用内部类实现清空text
代码如下,
问问大虾,
不用内部类怎么清空text(我用的是内部类)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Example10_7{
public static void main(String[] args) {

MyWindow win = new MyWindow();

}
}

class MyWindow extends JFrame{
JTextField text;
Polices police;

MyWindow(){
setLayout(new FlowLayout());
text = new JTextField(15);
add(text);
police = new Polices();
text.addActionListener(police);
setBounds(300,400,200,150);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


class Polices implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
System.out.println(str);
System.out.println(str.length());
text.setText("");
}
}
}

[解决办法]
当然可以 ,放在外面就是了

Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Example10_7 {    public static void main(String[] args) {        MyWindow win = new MyWindow();    }}class MyWindow extends JFrame {    JTextField text;    Polices police;    MyWindow() {        setLayout(new FlowLayout());        text = new JTextField(15);        add(text);        police = new Polices(text);        text.addActionListener(police);        setBounds(300, 400, 200, 150);        setVisible(true);        validate();        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    }class Polices implements ActionListener {    JTextField t;    public Polices(JTextField textfield) {        t = textfield;    }        public void actionPerformed(ActionEvent e) {        String str = e.getActionCommand();        System.out.println(str);        System.out.println(str.length());        t.setText("");    }} 

读书人网 >J2SE开发

热点推荐