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

📄 app.java

📁 点对点聊天系统 支持文字和语音聊天。同时是服务器和客户端。App.formApp.javaShowMsg.javaTCPClient.javaTCPServer.javaUDPCapture.java
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnConApp, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, btnConRec, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel10Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(txtHost, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnConApp)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnConRec)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel2.add(jPanel10);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Link"));
        btnLinkl.setText("Link");
        btnLinkl.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLinklMouseClicked(evt);
            }
        });

        btnLinku.setText("Unlink");
        btnLinku.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLinkuMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel6Layout.createSequentialGroup()
                .add(21, 21, 21)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, btnLinku, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, btnLinkl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                .add(69, 69, 69))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(btnLinkl)
                .add(15, 15, 15)
                .add(btnLinku)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel2.add(jPanel6);

        getContentPane().add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnVoiAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnVoiAMouseClicked
// accept voice
        if(state==1){
            System.out.println(other);
            uc=new UDPCapture(other);
            uc.start();
            up=new UDPPlay();
            up.start();
        } else if(state==2){
            uc=new UDPCapture(tcps.client.getInetAddress().getHostAddress());
            uc.start();
            up=new UDPPlay();
            up.start();
        } else{
            append("状态错误");
        }
        btnVoiA.setEnabled(false);
        btnVois.setEnabled(false);
        btnVoic.setEnabled(true);
        
    }//GEN-LAST:event_btnVoiAMouseClicked
    
    private void btnConRecMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnConRecMouseClicked
// Contra port recovery
        othertcp=20000;
        otherudp=20001;
        other="127.0.0.1";
        txtHost.setText(other);
    }//GEN-LAST:event_btnConRecMouseClicked
    
    private void btnConAppMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnConAppMouseClicked
// Contra port apply
        other=txtHost.getText().trim();
        append("Contra "+othertcp+" "+otherudp+" "+other);
    }//GEN-LAST:event_btnConAppMouseClicked
    
    private void btnClearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnClearMouseClicked
// button clear
        txtDia.setText("");
    }//GEN-LAST:event_btnClearMouseClicked
    
    private void btnSendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSendMouseClicked
// button send
        String str=txtDia.getText().trim();
        if(str==null||str.equals("")){
            append("发送内容不能为空");
        } else{
            if(state==1){
                tcpc.send(str);
                append("client:"+txtDia.getText().trim());
            } else if(state==2){
                tcps.send(txtDia.getText().trim());
                append("server:"+txtDia.getText().trim());
            } else{
                append("状态错误");
            }
            txtDia.setText("");
        }
    }//GEN-LAST:event_btnSendMouseClicked
    
    private void btnLinkuMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLinkuMouseClicked
// Link unlink
        if(tcpc!=null){
            tcpc.send("END");
            tcpc.close();
        }
        
        state=0;
        
        btnSers.setEnabled(true);
        btnSerc.setEnabled(true);
        btnVois.setEnabled(false);
        btnVoic.setEnabled(false);
        btnConApp.setEnabled(true);
        btnConRec.setEnabled(true);
        btnLinkl.setEnabled(true);
        this.setTitle("");
    }//GEN-LAST:event_btnLinkuMouseClicked
    
    private void btnLinklMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLinklMouseClicked
// Link link
        tcpc=new TCPClient(other,othertcp,this);
        Thread th=new Thread(tcpc);
        th.start();
        append("Link to "+other+":"+othertcp);
        state=1;
        
        btnSers.setEnabled(false);
        btnSerc.setEnabled(false);
        btnVois.setEnabled(true);
        btnVoic.setEnabled(true);
        btnConApp.setEnabled(false);
        btnConRec.setEnabled(false);
        btnLinku.setEnabled(true);
        this.setTitle("Client");
    }//GEN-LAST:event_btnLinklMouseClicked
    
    private void btnVoicMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnVoicMouseClicked
// Voice close
        if(uc!=null)
            uc.stop();
        if(up!=null)
            up.stop();
        
    }//GEN-LAST:event_btnVoicMouseClicked
    
    private void btnVoisMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnVoisMouseClicked
// Voice start
        
        
        if(state==1){
            tcpc.send("client request voice");
            append("client request voice");
            uc=new UDPCapture(other);
            uc.start();
            up=new UDPPlay();
            up.start();
        } else if(state==2){
            tcps.send("server request voice");
            append("server request voice");
            
            uc=new UDPCapture(tcps.client.getInetAddress().getHostAddress());
            uc.start();
            up=new UDPPlay();
            up.start();
        } else{
            append("状态错误");
        }
        btnVoiA.setEnabled(false);
        btnVois.setEnabled(false);
        btnVoic.setEnabled(true);
        
    }//GEN-LAST:event_btnVoisMouseClicked
    
    private void btnSercMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSercMouseClicked
// Service close
        if(tcps!=null){
            tcps.send("END");
            tcps.finish();
            
            try {
                if(tcps.client!=null)
                    tcps.client.close();
                if(tcps.server!=null)
                    tcps.server.close();
                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            append("TCP Server has been closed!");
            state=0;
            
            //设置其他一些按钮可用
            btnConApp.setEnabled(true);
            btnConRec.setEnabled(true);
            btnLinkl.setEnabled(true);
            btnSers.setEnabled(true);
            //设置其他一些按钮不可用
            btnLinku.setEnabled(false);
            btnVoic.setEnabled(false);
            btnVois.setEnabled(false);
            this.setTitle("");
        }
    }//GEN-LAST:event_btnSercMouseClicked
    
    private void btnSersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSersMouseClicked
// Service start
        //  if(tcps==null){
        tcps=new TCPServer(this);
        Thread td=new Thread(tcps);
        td.start();
        append("TCP Server Is Started!");
        state=2;
        
        //设置其他一些按钮不可用
        btnConApp.setEnabled(false);
        btnConRec.setEnabled(false);
        btnLinku.setEnabled(false);
        btnLinkl.setEnabled(false);
        
        btnSerc.setEnabled(true);
        btnVois.setEnabled(true);
        btnVoic.setEnabled(true);
        this.setTitle("Server");
        // } else{
        //   append("TCP Server has been started!");
        // }
        
    }//GEN-LAST:event_btnSersMouseClicked
    
    void setSta(String str){
        lblSta.setText(str);
    }
    
    void append(String str){
        txtCon.append(str+"\n");
    }
    
    public void showMsg(final String str){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                txtCon.append(str+ "\r\n");
            }
        });
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new App().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnConApp;
    private javax.swing.JButton btnConRec;
    private javax.swing.JButton btnLinkl;
    private javax.swing.JButton btnLinku;
    private javax.swing.JButton btnSend;
    private javax.swing.JButton btnSerc;
    private javax.swing.JButton btnSers;
    private javax.swing.JButton btnVoiA;
    private javax.swing.JButton btnVoic;
    private javax.swing.JButton btnVois;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblSta;
    private javax.swing.JTextArea txtCon;
    private javax.swing.JTextArea txtDia;
    private javax.swing.JTextField txtHost;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

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