一个c/s的聊天室(2)
一个c/s的聊天室(2)
class Frame2_jTextField1_keyAdapter extends KeyAdapter {
private Frame2 adaptee;
Frame2_jTextField1_keyAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jTextField1_keyPressed(e);
}
}
class Frame2_jButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame2_jButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame2_jRadioButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}
class Frame2_jRadioButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton1_actionPerformed(e);
}
}
//聊天界面
public class Frame1 extends JFrame implements Runnable{
Thread thread;
JPanel contentPane;
Frame2 frame=new Frame2();
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JScrollPane jScrollPane3 = new JScrollPane();
JList jList1 = new JList();
Socket socket;
int port =10000;
DataInputStream in;
DataOutputStream out;
Vector vector=new Vector();
String wenBen;
String str;
String xinxi;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
out.writeUTF("下线了:"+frame.name+"/"+frame.sex);
out.close() ;
in.close() ;
socket.close();
}
catch(Exception ee){
}
finally{
frame.dispose() ;
dispose();
}
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setTitle("聊天界面");
this.setResizable(false);
setSize(new Dimension(602, 515));
frame.setVisible(true);
jScrollPane1.setBounds(new Rectangle(2, 2, 445, 320));
jScrollPane2.setBounds(new Rectangle(1, 333, 448, 95));
jButton1.setBounds(new Rectangle(93, 441, 78, 31));
jButton1.setText("发送");
jButton2.setBounds(new Rectangle(185, 441, 76, 31));
jButton2.setText("清空");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jTextArea1.setEditable(false);
jScrollPane3.setBounds(new Rectangle(458, 6, 139, 424));
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextArea2.addKeyListener(new Frame1_jTextArea2_keyAdapter(this));
contentPane.add(jScrollPane2);
jScrollPane2.getViewport().add(jTextArea2);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1);
contentPane.add(jScrollPane3);
contentPane.add(jButton1);
contentPane.add(jButton2);
jScrollPane3.getViewport().add(jList1);
this.setVisible(false);
try {
socket = new Socket("liu.dhcn.net", port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
setVisible(true);
jTextArea2.setText("无法与服务器联接,请退出...") ;
}
if (thread == null) {
thread = new Thread(this);
thread.start();
}
frame.setClection(socket, in, out);
}
public boolean setClection1() {
frame.setClection(socket, in, out);
return frame.bool;
}
public void setClection2() {
wenBen = jTextArea2.getText();
if (wenBen.equals("")) {
jTextArea2.setText("please say a word:");
return;
}
try {
out.writeUTF(frame.name + " : " + wenBen);
jTextArea2.setText("") ;
} catch (Exception ee) {
}
}
public void run() {
String sl = null;
String ss = null;
String s = null;
while (true) {
try {
sl = in.readUTF();
} catch (Exception ee) {
vector.removeAllElements();
jList1.setListData(vector);
jTextArea2.setText("巳经与服务器断开...");
return;
}
if(sl.startsWith("可以") ){
frame.setVisible(false);
setVisible(true);
setTitle("昵称..." + frame.getName());
continue;
}
else if(sl.startsWith("不可以")){
frame.jTextField1.setText("请重新输入昵称") ;
continue;
}
else if (sl.startsWith("新的朋友")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.addElement(s);
jList1.setListData(vector);
continue;
}
else if (sl.startsWith("下线了")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.removeElement(s);
jList1.setListData(vector);
continue;
}
jTextArea1.append(sl + "\n");
}
} (未完待续)
class Frame2_jTextField1_keyAdapter extends KeyAdapter {
private Frame2 adaptee;
Frame2_jTextField1_keyAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.jTextField1_keyPressed(e);
}
}
class Frame2_jButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame2_jButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame2_jRadioButton2_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton2_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}
class Frame2_jRadioButton1_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jRadioButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton1_actionPerformed(e);
}
}
//聊天界面
public class Frame1 extends JFrame implements Runnable{
Thread thread;
JPanel contentPane;
Frame2 frame=new Frame2();
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JTextArea jTextArea2 = new JTextArea();
JScrollPane jScrollPane3 = new JScrollPane();
JList jList1 = new JList();
Socket socket;
int port =10000;
DataInputStream in;
DataOutputStream out;
Vector vector=new Vector();
String wenBen;
String str;
String xinxi;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
out.writeUTF("下线了:"+frame.name+"/"+frame.sex);
out.close() ;
in.close() ;
socket.close();
}
catch(Exception ee){
}
finally{
frame.dispose() ;
dispose();
}
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setTitle("聊天界面");
this.setResizable(false);
setSize(new Dimension(602, 515));
frame.setVisible(true);
jScrollPane1.setBounds(new Rectangle(2, 2, 445, 320));
jScrollPane2.setBounds(new Rectangle(1, 333, 448, 95));
jButton1.setBounds(new Rectangle(93, 441, 78, 31));
jButton1.setText("发送");
jButton2.setBounds(new Rectangle(185, 441, 76, 31));
jButton2.setText("清空");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jTextArea1.setEditable(false);
jScrollPane3.setBounds(new Rectangle(458, 6, 139, 424));
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextArea2.addKeyListener(new Frame1_jTextArea2_keyAdapter(this));
contentPane.add(jScrollPane2);
jScrollPane2.getViewport().add(jTextArea2);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1);
contentPane.add(jScrollPane3);
contentPane.add(jButton1);
contentPane.add(jButton2);
jScrollPane3.getViewport().add(jList1);
this.setVisible(false);
try {
socket = new Socket("liu.dhcn.net", port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
setVisible(true);
jTextArea2.setText("无法与服务器联接,请退出...") ;
}
if (thread == null) {
thread = new Thread(this);
thread.start();
}
frame.setClection(socket, in, out);
}
public boolean setClection1() {
frame.setClection(socket, in, out);
return frame.bool;
}
public void setClection2() {
wenBen = jTextArea2.getText();
if (wenBen.equals("")) {
jTextArea2.setText("please say a word:");
return;
}
try {
out.writeUTF(frame.name + " : " + wenBen);
jTextArea2.setText("") ;
} catch (Exception ee) {
}
}
public void run() {
String sl = null;
String ss = null;
String s = null;
while (true) {
try {
sl = in.readUTF();
} catch (Exception ee) {
vector.removeAllElements();
jList1.setListData(vector);
jTextArea2.setText("巳经与服务器断开...");
return;
}
if(sl.startsWith("可以") ){
frame.setVisible(false);
setVisible(true);
setTitle("昵称..." + frame.getName());
continue;
}
else if(sl.startsWith("不可以")){
frame.jTextField1.setText("请重新输入昵称") ;
continue;
}
else if (sl.startsWith("新的朋友")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.addElement(s);
jList1.setListData(vector);
continue;
}
else if (sl.startsWith("下线了")) {
StringTokenizer st = new StringTokenizer(sl, ":");
ss = null;
s = null;
ss = st.nextToken();
s = st.nextToken();
vector.removeElement(s);
jList1.setListData(vector);
continue;
}
jTextArea1.append(sl + "\n");
}
} (未完待续)
- 上篇文章:一个c/s的聊天室(1)
- 下篇文章:一个c/s的聊天室(3)