📄 chatroomclient.java
字号:
/* * Cient.java * * Created on 2008年4月1日, 下午4:42 */import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*; /** * * @author Owner */public class ChatRoomClient extends javax.swing.JFrame implements Runnable { PrintWriter out; BufferedReader in=null; Socket sock; public final static int DEFAULT_PORT=6666; //create Thread to Read information from Server Thread readThread; boolean isTrue=true; //thread can go on runing ?? /** Creates new form Cient */ public ChatRoomClient() { try{ initComponents(); }catch(Exception e){ e.printStackTrace(); } } public void sendInformation() { out.println(jTextField2.getText()); out.flush(); } public void exit() { try{//send "Client exit!" to Server! out.println("Client exit!"); out.flush(); }catch(Exception exc){} //endConnect(); try{//close IOstream sock.close(); in.close(); out.close(); }catch(IOException ioe){} finally{ System.exit(0); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel2 = new javax.swing.JPanel(); jButton2 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowActivated(java.awt.event.WindowEvent evt) { formWindowActivated(evt); } }); jPanel2.setPreferredSize(new java.awt.Dimension(50, 50)); jPanel2.setVerifyInputWhenFocusTarget(false); jButton2.setText("发送"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel1.setText("消息"); org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() .add(jLabel1) .add(18, 18, 18) .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE) .add(18, 18, 18) .add(jButton2) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel2Layout.createSequentialGroup() .addContainerGap(17, Short.MAX_VALUE) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel1) .add(jButton2)) .addContainerGap()) ); getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END); jButton1.setText("连接"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextField1.setText("input Server address here!"); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(28, 28, 28) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 246, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 30, Short.MAX_VALUE) .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton1)) .addContainerGap(12, Short.MAX_VALUE)) ); getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START); jScrollPane1.setPreferredSize(new java.awt.Dimension(122, 333)); jScrollPane1.setRequestFocusEnabled(false); jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.setPreferredSize(new java.awt.Dimension(120, 190)); jScrollPane1.setViewportView(jTextArea1); getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed startConnect(); // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed sendInformation(); // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivatedthis.addWindowListener(new ChatFrame_WindowAdapter(this)); // TODO add your handling code here: }//GEN-LAST:event_formWindowActivated public void processMsg(String msg) { jTextArea1.append(msg); jTextArea1.append("\n"); } public void startConnect() { try{ sock=new Socket(jTextField1.getText(),DEFAULT_PORT); if(sock!=null){//connection successed processMsg("Connect successfully!"); } in=new BufferedReader(new InputStreamReader(sock.getInputStream())); out=new PrintWriter(sock.getOutputStream()); }catch(IOException ex){ processMsg(ex.toString()); processMsg("Connect failed!"); } readThread=new Thread(this); readThread.start(); } public void run(){ String msg; isTrue=true; while(isTrue){ try{ msg=in.readLine(); if(msg!=null){ processMsg(msg); } Thread.sleep(1000); }catch(IOException e){ processMsg(e.toString()); }catch(InterruptedException ei){ processMsg(ei.toString()); } } //endConnect(); try{//服务器退出关闭连接和相关的"流" sock.close(); in.close(); out.close(); }catch(IOException ioe){} } /** * @param args the command line arguments */ public static void main(String args[]) { ChatRoomClient c=new ChatRoomClient();c.show(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration//GEN-END:variables} class ChatFrame_WindowAdapter extends java.awt.event.WindowAdapter{ ChatRoomClient chatFrame; public ChatFrame_WindowAdapter(ChatRoomClient chatFrame){ this.chatFrame=chatFrame; } @Override public void windowClosing(WindowEvent e){//exit program chatFrame.exit();//reference to the method exit() in ChatRoomClient. }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -