⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientfram.java

📁 它能够进行简单的文字聊天和基本的信息交换。
💻 JAVA
字号:
/*
 * ClientFram.java
 * 
 */

/**
 *
 * @author  codewar1
 */
import javax.print.attribute.standard.OutputDeviceAssigned;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
public class ClientFram extends javax.swing.JFrame {
    
    /** Creates new form ClientFram */
    public ClientFram(String ip) {
        this.ip = ip; 
        
        initComponents();
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }//初始化客户端
    
    /** 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=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jShow = new javax.swing.JTextArea();
        jMsg = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLogin = new javax.swing.JButton();
        jLogout = new javax.swing.JButton();
        jSend = new javax.swing.JButton();
        jrcv = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jrecShow = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("peer-chat");
        setBackground(new java.awt.Color(153, 153, 153));
        setName("Client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 51, 51));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(204, 204, 204), null));
        jShow.setBackground(new java.awt.Color(204, 204, 204));
        jShow.setColumns(20);
        jShow.setRows(5);
        jShow.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(153, 153, 153), null));
        jShow.setEnabled(false);
        jScrollPane1.setViewportView(jShow);

        jMsg.setBackground(new java.awt.Color(204, 204, 0));
        jMsg.setColumns(20);

        jLogin.setBackground(new java.awt.Color(204, 204, 204));
        jLogin.setFont(new java.awt.Font("宋体", 1, 12));
        jLogin.setForeground(new java.awt.Color(51, 51, 51));
        jLogin.setText("Connect");
        jLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLoginActionPerformed(evt);
            }
        });

        jLogout.setBackground(new java.awt.Color(204, 204, 204));
        jLogout.setFont(new java.awt.Font("宋体", 1, 12));
        jLogout.setForeground(new java.awt.Color(51, 51, 51));
        jLogout.setText("Disconnect");
        jLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLogoutActionPerformed(evt);
            }
        });

        jSend.setBackground(new java.awt.Color(204, 204, 204));
        jSend.setFont(new java.awt.Font("宋体", 1, 12));
        jSend.setForeground(new java.awt.Color(51, 51, 51));
        jSend.setText("SendMsg");
        jSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSendActionPerformed(evt);
            }
        });

        jrcv.setBackground(new java.awt.Color(204, 204, 204));
        jrcv.setFont(new java.awt.Font("宋体", 1, 12));
        jrcv.setForeground(new java.awt.Color(51, 51, 51));
        jrcv.setText("Rcvifile");
        jrcv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrcvActionPerformed(evt);
            }
        });

        jScrollPane2.setForeground(new java.awt.Color(51, 51, 51));
        jrecShow.setBackground(new java.awt.Color(204, 204, 204));
        jrecShow.setColumns(20);
        jrecShow.setEditable(false);
        jrecShow.setRows(5);
        jScrollPane2.setViewportView(jrecShow);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jSend)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jrcv, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLogout, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLogin, jLogout, jSend, jrcv});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(30, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLogin)
                            .addComponent(jLogout))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSend)
                    .addComponent(jrcv)
                    .addComponent(jMsg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jrcvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrcvActionPerformed
                   startrev();// 按下接收文件
    }//GEN-LAST:event_jrcvActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
                   LogOut();
                  // System.exit(0);// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_formWindowClosed

    private void jSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSendActionPerformed
                   sendMsg();// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_jSendActionPerformed

    private void jLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLogoutActionPerformed
                  LogOut();// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_jLogoutActionPerformed

    private void jLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLoginActionPerformed
                  LogIn();// TODO 将在此处添加您的处理代码:
    }//GEN-LAST:event_jLoginActionPerformed
    
    /**
     * @param args the command line arguments
     */
  /*  public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientFram().setVisible(true);
            }
        });
    }*/
    
    public void LogIn()
    {
        try{
        jShow.setEnabled(true);
        name = jMsg.getText()+":";
      //  jTextField1.setText("127.0.0.1");
       // String ip = jTextField1.getText();
        socket = new Socket(ip,8000);
        socket.setSoTimeout(0);
       //IU = new iu(socket);
        IU = new iu();
      //  output = IU.getOutputStream();
        output = new ObjectOutputStream(socket.getOutputStream());
     
        output.flush();
        
        input = new ObjectInputStream(socket.getInputStream());
        //input = IU.getInputStream();
      // output.writeObject(jMsg.getText());
       // output.write(jMsg.getText().getBytes());
        output.writeObject(IU.encrypt(jMsg.getText()));
        output.flush();
        jMsg.setText(" ");
        
        recvThread = new ClientReceiveThread(socket,output,input,jShow,name,IU);
        recvThread.start();
        
        jLogin.setEnabled(false);
        jLogout.setEnabled(true);
        
        
       
        //jMsg.setText("please input msg");
        
        }
        catch (Exception e)
        {
            jShow.append("Server is unready"+"\n");
            e.printStackTrace();
        }
    }
    public void LogOut()
    {
        jLogin.setEnabled(true);
        jLogout.setEnabled(false);
        jMsg.setText("please input username");
        
        if (socket.isClosed())
        {
            return;
        }
        try
        {
           //output.write("下线请求".getBytes());
        // output.writeObject("下线请求");
           output.writeObject(IU.encrypt("下线请求"));
           output.flush();
           
           input.close();
           output.close();
           socket.close();
           
        }
         catch (Exception e)
        {}
    }
    public void sendMsg()
    {
        String msg =jMsg.getText();
        if (socket.isClosed())
        {
            return;
        }
        try
        {
            //output.write("聊天信息".getBytes());
           // output.writeObject("聊天信息");
             output.writeObject(IU.encrypt("聊天信息"));
             output.flush();
           //output.writeObject(msg);
            // output.write(msg.getBytes());
            output.writeObject(IU.encrypt(msg));
            output.flush();
            jShow.append(name+msg+"\n");
            jMsg.setText("");   
        
       
        
        }
        catch (Exception e)
        {}
    }
    public void  startrev(){
    
         filerec = new filerecer(jrecShow,ip);
         filerec.start();
    }
    public String name;
    Socket socket;
    ObjectOutputStream output;
    ObjectInputStream input;
   // OutputStream output;
   // InputStream input;
    iu IU;
    filerecer filerec;
    String ip ="192.168.1.100";
    ClientReceiveThread recvThread;
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton jLogin;
    private javax.swing.JButton jLogout;
    private javax.swing.JTextField jMsg;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jSend;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jShow;
    private javax.swing.JButton jrcv;
    private javax.swing.JTextArea jrecShow;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -