当前位置: 代码迷 >> Java相关 >> [求助]监听事件失效
  详细解决方案

[求助]监听事件失效

热度:248   发布时间:2006-12-09 01:00:54.0
[求助]监听事件失效

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class abc extends Frame implements ActionListener,KeyListener
{
TextField tf,name_txt;
TextArea ta;
Panel p,p1;
Button button,button1,button2;
Label label;
CheckboxGroup sex;
Checkbox box1,box2;

public abc()
{
tf=new TextField(25);
name_txt=new TextField(10);
ta=new TextArea();
label=new Label("输入昵称");
sex=new CheckboxGroup();
box1=new Checkbox("男",false,sex);
box2=new Checkbox("女",false,sex);
button=new Button("send");
button1=new Button("进入聊天室");
button2=new Button("退出聊天室");
p=new Panel();
p1=new Panel();


p.add(ta);
p.add(tf);
p.add(button);
p1.add(label);p1.add(name_txt);
p1.add(box1);p1.add(box2);
p1.add(button1);p1.add(button2);
p.add(p1);
add(p);
button.addActionListener(this);
tf.addKeyListener(this);


addWindowListener(new WindowDestroyer());
}

public void actionPerformed(ActionEvent e)
{
String s,str;
s=name_txt.getText()+"["+sex.getSelectedCheckbox().getLabel()+"]"+"说:";
if(e.getSource()==button)
{
str=tf.getText();

tf.setText("");

ta.append(s+str+"\n");
}
if(e.getSource()==button1)
{
ta.append("欢迎进入聊天室!"+"\n");

}

}

public void keyPressed(KeyEvent e)
{
String s;
s=name_txt.getText()+"["+sex.getSelectedCheckbox().getLabel()+"]"+"说:";
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if((e.getModifiers()&InputEvent.CTRL_MASK)!=0)
{
ta.append(s+tf.getText()+"\n");
tf.setText("");
}
}


}

public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}

}


class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

public class ex extends Frame
{
public static void main(String arg[])
{
abc win=new abc();
win.setSize(500,300);
win.setVisible(true);

}
}
运行时,这里面的button1("进入聊天室")按键的监听事件失效,好像是因为button1是面板P里面的面板P1中的组件,但不知道怎么改才能让它有效,各位帮帮忙,原文件在附件里,帮忙看一下

搜索更多相关的解决方案: import  java  awt  监听  失效  

----------------解决方案--------------------------------------------------------
运行时,这里面的button1("进入聊天室")按键的监听事件失效,好像是因为button1是面板P里面的面板P1中的组件,但

不知道怎么改才能让它有效,各位帮帮忙,原文件在附件里,帮忙看一下

----------------解决方案--------------------------------------------------------

我没有看到你哪行监听了button1和button2

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class abc extends Frame implements ActionListener,KeyListener
{
TextField tf,name_txt;
TextArea ta;
Panel p,p1;
Button button,button1,button2;
Label label;
CheckboxGroup sex;
Checkbox box1,box2;

public abc()
{
tf=new TextField(25);

name_txt=new TextField(10);
ta=new TextArea();
label=new Label("输入昵称");
sex=new CheckboxGroup();
box1=new Checkbox("男",false,sex);
box2=new Checkbox("女",false,sex);
button=new Button("send");
button1=new Button("进入聊天室");
button1.addActionListener(this);
button2=new Button("退出聊天室");
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
p=new Panel();
p1=new Panel();


p.add(ta);
p.add(tf);
p.add(button);
p1.add(label);p1.add(name_txt);
p1.add(box1);p1.add(box2);
p1.add(button1);p1.add(button2);
p.add(p1);
add(p);
button.addActionListener(this);
tf.addKeyListener(this);


addWindowListener(new WindowDestroyer());
}

public void actionPerformed(ActionEvent e)
{
String s,str;
s=name_txt.getText()+"["+sex.getSelectedCheckbox().getLabel()+"]"+"说:";
if(e.getSource()==button)
{
str=tf.getText();

tf.setText("");

ta.append(s+str+"\n");
}
if(e.getSource()==button1)
{
ta.append("欢迎进入聊天室!"+"\n");

}

}

public void keyPressed(KeyEvent e)
{
String s;
s=name_txt.getText()+"["+sex.getSelectedCheckbox().getLabel()+"]"+"说:";
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if((e.getModifiers()&InputEvent.CTRL_MASK)!=0)
{
ta.append(s+tf.getText()+"\n");
tf.setText("");
}
}


}

public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}

}


class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

public class ex extends Frame
{
public static void main(String arg[])
{
abc win=new abc();
win.setSize(500,300);
win.setVisible(true);

}
}


----------------解决方案--------------------------------------------------------

原来如此,好低级的错误啊,真晕啦,居然把那两句话弄掉了


----------------解决方案--------------------------------------------------------

谢谢啦


----------------解决方案--------------------------------------------------------
  相关解决方案