Topic: 关于Awt事件处理的错误

  Print this page

1.关于Awt事件处理的错误 Copy to clipboard
Posted by: ice0819
Posted on: 2008-10-12 09:27


import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class Atest extends Frame {
Checkbox checkbox;
Checkbox checkbox2;
Panel panel ;
TextField textfield;

public Atest(){
   checkbox=new Checkbox("boy");
   checkbox2=new Checkbox("girl");
   panel=new Panel();
   panel.add(checkbox);
   panel.add(checkbox2);
   panel.setLayout(new GridLayout(2,1));
   textfield=new TextField("请输入您的用户名",2);
   textfield.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent aevt){
       Scanner reader=new Scanner(System.in);
       String input=reader.next();
      
       System.out.println("input"+input);
       reader.close();
     }
   });
   checkbox.addItemListener(itemlist);
   checkbox2.addItemListener(itemlist);
   add(panel,BorderLayout.WEST);
   add(textfield,BorderLayout.SOUTH);
   pack();
  
   setVisible(true);
  addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent evt){System.exit(0);}});
}
itemlistener itemlist=new itemlistener();
class itemlistener implements ItemListener{
   public void itemStateChanged(ItemEvent evt){
     if(checkbox.getState()){checkbox2.setState(false);}
     if(checkbox2.getState())checkbox.setState(false);
    
    
    
    
   }
}


为什么在TextField获得输入之后,无法响应Checkbox的事件,而且窗口也关闭不了了。

2.Re:关于Awt事件处理的错误 [Re: ice0819] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-10-14 20:54

> 为什么在TextField获得输入之后,无法响应Checkbox的事件,而且窗口也关闭不了了。
In the GUI action event, it is *not* correct to direct from the System standard input. i.e. use System.in. To be more accurate, you give up the GUI thread (process?) to the system standard input thread(process?) which blocks your current GUI client.

Therefore, you should replace the following LOD. (lines of code)

//Scanner reader=new Scanner(System.in);
//String input=reader.next();
String input = textfield.getText();

Jiafan

3.Re:关于Awt事件处理的错误 [Re: ice0819] Copy to clipboard
Posted by: ice0819
Posted on: 2008-10-14 21:02

I hava got the answer in dream of the day brfore yesterday,but thanks you all the same!

4.Re:关于Awt事件处理的错误 [Re: ice0819] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-10-15 19:18

You got the answer in your dream?
wow...... somebody must hire this guy!!!

5.Re:关于Awt事件处理的错误 [Re: ice0819] Copy to clipboard
Posted by: ice0819
Posted on: 2008-10-15 19:25

但愿如此吧。I ‘m struggling for that。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923