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中的组件,但不知道怎么改才能让它有效,各位帮帮忙,原文件在附件里,帮忙看一下