📄 clientjframe.java.svn-base
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * ClientJFrame.java * * Created on 2008-11-21, 14:06:45 */package safechatclient;import mytools.Packet;import mytools.DSA;import mytools.AES;import java.io.*;import java.net.*;import java.security.InvalidKeyException;import java.util.logging.Level;import java.util.logging.Logger;import javax.crypto.BadPaddingException;import javax.crypto.IllegalBlockSizeException;import javax.swing.JOptionPane;/** * * @author DJ尐舞 */public class ClientJFrame extends javax.swing.JFrame { Socket client; InputStream in; BufferedReader br; OutputStream out; BufferedWriter bw; Packet p; String userName; AES clientAES = new AES(); Thread currentThread; boolean isConnected = false; int registerState = 0; boolean loginState = false; /** Creates new form ClientJFrame */ public ClientJFrame() {// try { initComponents(); connectButton.doClick(); // AES.generateKey(); //生成密钥 // DSA.generatekey(); //生成密钥对prikey.dat与pubkey.dat// } catch (NoSuchAlgorithmException ex) {// System.out.println("生成密钥对失败:机子上不存生成AES的算法库");// } } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); chatContent = new javax.swing.JTextArea(); jTextField1 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); connectButton = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); editArea = new javax.swing.JTextArea(); sendButton = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); userNameButton = new javax.swing.JTextField(); regButton = new javax.swing.JButton(); loginButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); chatContent.setColumns(20); chatContent.setEditable(false); chatContent.setRows(5); chatContent.setBorder(javax.swing.BorderFactory.createTitledBorder("聊天窗口")); jScrollPane1.setViewportView(chatContent); jTextField1.setText("localhost"); jLabel1.setText("主机"); connectButton.setText("连接"); connectButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connectButtonActionPerformed(evt); } }); editArea.setColumns(20); editArea.setRows(5); jScrollPane2.setViewportView(editArea); sendButton.setText("发送"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); jLabel2.setText("用户名"); regButton.setText("注册"); regButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { regButtonActionPerformed(evt); } }); loginButton.setText("登陆"); loginButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loginButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sendButton, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(userNameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(loginButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(regButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(userNameButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(regButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(sendButton) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * 连接按钮事件,创建一个线程来建立连接 * @param evt */ private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed if (isConnected) { try { in.close(); out.close(); client.close(); } catch (IOException ex) { Logger.getLogger(ClientJFrame.class.getName()).log(Level.SEVERE, null, ex); } connectButton.setText("连接"); chatContent.append("已断开与服务器的连接\n"); isConnected = false; } else { connectButton.setText("连接中..."); currentThread = new Thread() { private void unConnectProcess() throws IOException { loginState = false; loginButton.setVisible(true); in.close(); out.close(); client.close(); connectButton.setText("连接"); chatContent.append("掉线啦!\n"); isConnected = false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -