📄 chatclientframe.java~1~
字号:
package chatclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.net.*;import java.util.*;import java.lang.Thread;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2007</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class chatClientFrame extends JFrame { JPanel contentPane; JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); PaneLayout paneLayout1 = new PaneLayout(); XYLayout xYLayout1 = new XYLayout(); JPanel jPanel4 = new JPanel(); JLabel jLabel1 = new JLabel(); JTextField jTextField1 = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField jTextField2 = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField jTextField3 = new JTextField(); JButton jButton1 = new JButton(); BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel5 = new JPanel(); JPanel jPanel6 = new JPanel(); PaneLayout paneLayout2 = new PaneLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JList jList1 = new JList(); JLabel jLabel4 = new JLabel(); PaneLayout paneLayout3 = new PaneLayout(); JPanel jPanel7 = new JPanel(); JPanel jPanel8 = new JPanel(); JPanel jPanel9 = new JPanel(); PaneLayout paneLayout4 = new PaneLayout(); JPanel jPanel3 = new JPanel(); JPanel jPanel10 = new JPanel(); JPanel jPanel11 = new JPanel(); PaneLayout paneLayout5 = new PaneLayout(); PaneLayout paneLayout6 = new PaneLayout(); JScrollPane jScrollPane2 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JRadioButton jRadioButton1 = new JRadioButton(); XYLayout xYLayout2 = new XYLayout(); JRadioButton jRadioButton2 = new JRadioButton(); XYLayout xYLayout3 = new XYLayout(); JPanel jPanel12 = new JPanel(); PaneLayout paneLayout7 = new PaneLayout(); JScrollPane jScrollPane3 = new JScrollPane(); JScrollPane jScrollPane4 = new JScrollPane(); JTextArea jTextArea2 = new JTextArea(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); XYLayout xYLayout4 = new XYLayout(); JButton jButton5 = new JButton(); XYLayout xYLayout5 = new XYLayout(); ButtonGroup buttonGroup1 = new ButtonGroup(); Socket socket=null; PrintStream ps=null; Listen listen=null; int count=0; //Construct the frame public chatClientFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(paneLayout1); this.setSize(new Dimension(423, 394)); this.setTitle("chatClient"); jPanel1.setLayout(xYLayout1); jPanel4.setLayout(borderLayout1); jLabel1.setText("服务器IP"); jPanel1.setBackground(UIManager.getColor("control")); jTextField1.setText("192.168.1.2"); jLabel2.setText("服务器端口"); jTextField2.setText("4000"); jLabel3.setText("昵称"); jTextField3.setText("SAM"); jButton1.setText("连接"); jButton1.addActionListener(new chatClientFrame_jButton1_actionAdapter(this)); jPanel6.setLayout(paneLayout2); jLabel4.setText("目前用户数"); jPanel5.setLayout(paneLayout3); jPanel2.setLayout(paneLayout4); jPanel7.setLayout(paneLayout5); jPanel3.setLayout(paneLayout6); jRadioButton1.setText("广播发送"); jPanel10.setLayout(xYLayout2); jRadioButton2.setText("选择发送"); jPanel11.setLayout(xYLayout3); jPanel12.setLayout(paneLayout7); jButton2.setText("发送"); jButton2.addActionListener(new chatClientFrame_jButton2_actionAdapter(this)); jButton3.setText("退出"); jButton3.addActionListener(new chatClientFrame_jButton3_actionAdapter(this)); jButton4.setText("发送语音请求"); jPanel8.setLayout(xYLayout4); jButton5.setText("发送文件"); jPanel9.setLayout(xYLayout5); jTextArea1.setEditable(false); jTextArea1.setText(""); jTextArea2.setEnabled(true); jTextArea2.setEditable(false); jTextArea2.setText(""); jPanel1.add(jPanel4, new XYConstraints(-1, 0, 101, 203)); jPanel4.add(jPanel5, BorderLayout.SOUTH); jPanel5.add(jLabel4, new PaneConstraints("jLabel4", "jLabel4", PaneConstraints.ROOT, 1.0f)); jPanel4.add(jPanel6, BorderLayout.CENTER); jPanel6.add(jScrollPane1, new PaneConstraints("jScrollPane1", "jScrollPane1", PaneConstraints.ROOT, 1.0f)); jScrollPane1.getViewport().add(jList1, null); jPanel1.add(jLabel1, new XYConstraints(4, 206, -1, -1)); jPanel1.add(jTextField1, new XYConstraints(2, 222, 96, -1)); jPanel1.add(jLabel2, new XYConstraints(4, 248, -1, 14)); jPanel1.add(jTextField2, new XYConstraints(2, 263, 94, -1)); jPanel1.add(jLabel3, new XYConstraints(4, 289, -1, -1)); jPanel1.add(jTextField3, new XYConstraints(3, 304, 93, -1)); jPanel1.add(jButton1, new XYConstraints(9, 333, 71, -1)); jPanel2.add(jPanel7, new PaneConstraints("jPanel7", "jPanel7", PaneConstraints.ROOT, 0.5f)); jPanel7.add(jPanel3, new PaneConstraints("jPanel3", "jPanel3", PaneConstraints.ROOT, 0.5f)); jPanel3.add(jScrollPane2, new PaneConstraints("jScrollPane2", "jScrollPane2", PaneConstraints.ROOT, 1.0f)); jScrollPane2.getViewport().add(jTextArea1, null); jPanel7.add(jPanel10, new PaneConstraints("jPanel10", "jPanel3", PaneConstraints.BOTTOM, 0.5614035f)); jPanel10.add(jRadioButton1, new XYConstraints(9, 4, -1, -1)); jPanel10.add(jRadioButton2, new XYConstraints(113, 5, -1, -1)); jPanel7.add(jPanel11, new PaneConstraints("jPanel11", "jPanel10", PaneConstraints.BOTTOM, 0.79591835f)); jPanel11.add(jPanel12, new XYConstraints(0, 3, 321, 67)); jPanel12.add(jScrollPane3, new PaneConstraints("jScrollPane3", "jScrollPane3", PaneConstraints.ROOT, 0.5f)); jPanel11.add(jButton2, new XYConstraints(233, 76, 70, -1)); jPanel11.add(jButton3, new XYConstraints(140, 76, 77, -1)); jScrollPane3.getViewport().add(jScrollPane4, null); jScrollPane4.getViewport().add(jTextArea2, null); jPanel2.add(jPanel8, new PaneConstraints("jPanel8", "jPanel7", PaneConstraints.BOTTOM, 0.34771574f)); jPanel8.add(jButton4, new XYConstraints(9, 10, 105, -1)); jPanel2.add(jPanel9, new PaneConstraints("jPanel9", "jPanel8", PaneConstraints.BOTTOM, 0.620438f)); jPanel9.add(jButton5, new XYConstraints(11, 9, -1, -1)); contentPane.add(jPanel1, new PaneConstraints("jPanel1", "jPanel1", PaneConstraints.ROOT, 0.5f)); contentPane.add(jPanel2, new PaneConstraints("jPanel2", "jPanel1", PaneConstraints.RIGHT, 0.7635934f)); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton1_actionPerformed(ActionEvent e) { if(socket==null){ try{ socket=new Socket(jTextField1.getText(),Integer.parseInt(jTextField2.getText())); if(socket!=null){ ps=new PrintStream(socket.getOutputStream()); StringBuffer info=new StringBuffer("PEOPLE:"); String userinfo=jTextField3.getText()+":"+InetAddress.getLocalHost().toString(); ps.println(info.append(userinfo)); ps.flush(); listen=new Listen(this,jTextField3.getText(),socket); listen.start(); jTextArea1.append(jTextField3.getText()+"登陆成功"+"\n"); count++; jLabel4.setText("目前用户数:"+String.valueOf(count)); } else{ jTextArea1.append(jTextField3.getText()+"登陆失败\n"); } } catch(IOException ee){ jTextArea1.append(jTextField3.getText()+"登陆失败\n"); disconnect(); } } } void jButton2_actionPerformed(ActionEvent e) { if(socket!=null){ StringBuffer message=new StringBuffer("MSG:"); try{ String msg=new String(jTextField1.getText()); } catch(Exception ex){ } if(jRadioButton1.isSelected()){ message.append("BROAD:"); } else{ message.append(jList1.getAnchorSelectionIndex()+":"); } message.append(listen.name+":"); ps.println(message.append(jTextArea2.getText())); jTextArea2.setText(""); ps.flush(); } } class Listen extends Thread{ String name=null; DataInputStream dis=null; PrintStream ps=null; Socket socket=null; chatClientFrame parent=null; public Listen(chatClientFrame p,String n,Socket s){ parent=p; name=n; socket=s; try{ dis=new DataInputStream(s.getInputStream()); ps=new PrintStream(s.getOutputStream()); } catch(IOException e){ parent.jTextArea1.append("Error: "+e+"\n"); parent.disconnect(); } } public void run(){ String message=null; while(true){ try{ message=dis.readLine(); } catch(IOException e){ parent.jTextArea1.append("Error: "+e+"\n"); parent.disconnect(); } if(message==null){ parent.listen=null; parent.socket=null; ((DefaultListModel) parent.jList1.getModel()).removeAllElements(); return; } StringTokenizer str=new StringTokenizer(message,":"); String keyword=str.nextToken(); if(keyword.equalsIgnoreCase("PEOPLE")){ count=0; ((DefaultListModel)parent.jList1.getModel()).removeAllElements(); while(str.hasMoreTokens()){ String st=str.nextToken(); DefaultListModel dld = new DefaultListModel() ; parent.jList1.setModel(dld); dld.addElement(st); count++; } jLabel4.setText("目前用户数: "+String.valueOf(count)); } else if(keyword.equalsIgnoreCase("MSG")) { String broadcast=str.nextToken(); String usr=str.nextToken(); parent.jTextArea1.append(usr+":\n"); parent.jTextArea1.append(str.nextToken("\0")); parent.jTextArea1.append("\n"); } else if(keyword.equalsIgnoreCase("QUIT")){ String who=str.nextToken(); if(who.equalsIgnoreCase("SERVER")){ parent.jTextArea1.append("服务器关闭\n"); try{ parent.listen.stop(); parent.listen=null; parent.socket.close(); parent.socket=null; } catch(IOException e){parent.jTextArea1.append("Error: "+e +"\n");} ((DefaultListModel)parent.jList1.getModel()).removeAllElements(); return; } else{parent.jTextArea1.append(who+"已经离开\n");} } else if(keyword.equalsIgnoreCase("FAULTNAME")) {parent.jTextArea1.append("用户名错误\n");} } } } public void disconnect(){ if(socket!=null){ try{ ps.println("QUIT:"+listen.name); ps.flush(); socket.close(); count--; } catch(IOException e){ jTextArea1.append("Error:"+e); } } } void jButton3_actionPerformed(ActionEvent e) { disconnect(); System.exit(0); }}class chatClientFrame_jButton1_actionAdapter implements java.awt.event.ActionListener { chatClientFrame adaptee; chatClientFrame_jButton1_actionAdapter(chatClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class chatClientFrame_jButton2_actionAdapter implements java.awt.event.ActionListener { chatClientFrame adaptee; chatClientFrame_jButton2_actionAdapter(chatClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); }}class chatClientFrame_jButton3_actionAdapter implements java.awt.event.ActionListener { chatClientFrame adaptee; chatClientFrame_jButton3_actionAdapter(chatClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -