代码如下,
问问大虾,
不用内部类怎么清空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(""); }}