当前位置: 代码迷 >> J2SE >> 不用内部类实现清空text,该如何解决
  详细解决方案

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

热度:100   发布时间:2016-04-24 02:15:16.0
不用内部类实现清空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("");    }}
  相关解决方案