如何主函数取出TextField中的内容
在主函数中如何取出TestField中的内容?import java.awt.*;
import java.awt.event.*;
public class Demo1
{String str1="";
String str2="";
String str3="";
public Demo1(){
this.launch();
}
public void launch(){
Frame f=new Frame("Encrypt");
f.setLayout(null);
Label l1=new Label("文件");
Label l2=new Label("密钥K");
Label l3=new Label("密钥G");
TextField tf1=new TextField(20);
TextField tf2=new TextField(20);
TextField tf3=new TextField(20);
Button b1=new Button("加密");
Button b2=new Button("解密");
Button b3=new Button("取消");
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
f.setLayout(new GridLayout(4,1));
f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
p1.setLayout(new BorderLayout());
p2.setLayout(new BorderLayout());
p3.setLayout(new BorderLayout());
p4.setLayout(new FlowLayout());
p1.add(l1,"West");
p1.add(tf1,"East");
p2.add(l2,"West");
p2.add(tf2,"East");
p3.add(l3,"West");
p3.add(tf3,"East");
p4.add(b1,"East");
p4.add(b2,"East");
p4.add(b3,"East");
f.setVisible(true);
f.pack();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);}
});
tf1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TextField tf=(TextField)e.getSource();
str1=tf.getText();
tf.setText("");}
});
tf2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TextField tf=(TextField)e.getSource();
str2=tf.getText();
tf.setText("");}
});
tf3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
TextField tf=(TextField)e.getSource();
str3=tf.getText();
tf.setText("");}
});
}
}
主函数
public class Main{
public static void main(String[] args){
Demo1 d1=new Demo1();
System.out.println(d1.str1);
}
}
怎么在TestField中输入的字符没打印出啊?
----------------解决方案--------------------------------------------------------
这是因为你修改TextField控件的内容不会引发ActionEvent事件,也就是你相应的actionPerformed方法根本不会调用,相应的String对象也就不会存储TextField控件的内容了,要想实时取得TextFiled的内容,我想还是要使用焦点事件和焦点侦听器了。
----------------解决方案--------------------------------------------------------
回复 2楼 ygp_sfec
我使用了啊,能不能帮我修改下,谢谢了 ----------------解决方案--------------------------------------------------------
我运行过了,你这个程序不能显示TextFiled内容的关键在于你在关闭窗口和取消按钮的响应函数中使用了System.exit();方法,使用这个方法的结果是无条件退出程序的运行,也就是说你主函数(main)中,运行了 Demo1 d1=new Demo1();
后程序已经直接退出,在它后面的System.out.println(d1.str1);根本不会执行,因此就不会显示的的d1.str1的内容了,要实现你的意图,将下面两个方法按照如下方法修改:
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
// System.exit(1); 改为
removeNotify(); }
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//System.exit(0); 改为
removeNotify();}
});
然后把System.exit()方法的执行放在main()方法的最后就可以了。
----------------解决方案--------------------------------------------------------
错了,System.exit()方法不能加入main()方法中,否则窗口就一闪而过,无法进行操作,要在main()中退出程序,你的有条件执行System.exit()方法。
----------------解决方案--------------------------------------------------------