package qq.client;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class ClientChatUI extends JFrame implements ActionListener {
private String userName;
Label JLabel1,JLabel2;
TextArea JTextArea;
TextField JTextField;
Choice JChoice;
Button btn;
Panel p,p1,p2;
String s;
public ClientChatUI(String userName) //构造函数
{
this.userName=userName;
initUI();
showFrame();
}
public void showFrame()
{
this.setTitle("netjava:欢迎你"+this.userName);
setLayout(new FlowLayout(0));//设定对齐方式 为0则左对齐
setSize(300,500);
this.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); //窗体关闭时程序退出
setVisible(true);
}
public void initUI()
{
Label JLabel1=new Label("接收到的消息:");
Label JLabel2=new Label("发送给:");
TextArea JTextArea=new TextArea(20,37);
TextField JTextField=new TextField(15);
Choice JChoice=new Choice();
JChoice.add("何浩");
JChoice.add("段兄");
JChoice.add("包子");
Button btn=new Button("发送");
Panel p=new Panel();
Panel p1=new Panel();
Panel p2=new Panel();
p1.add(JLabel2);
p1.add(JTextField);
p2.add(JChoice);
p2.add(btn);
p.setLayout(new BorderLayout());
p.add(p1,BorderLayout.NORTH);
p.add(p2,BorderLayout.SOUTH);
setLayout(new BorderLayout());
add(JLabel1,BorderLayout.NORTH);
add(JTextArea,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
JTextField.addActionListener(this);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource()==btn)
{
JTextArea.append(userName+"对"+s+"说"+JTextField.getText());
JTextField.setText("");
}
}
public static void main(String[] args)
{
new ClientChatUI("zhh");
}
}
------解决方案--------------------