📄 chatclient.java
字号:
/* * ChatClient.java * * Created on 2008年4月10日, 下午8:41 */package org.Adam;import java.net.*;import javax.swing.JOptionPane;import java.io.*;/** * * @author Administrator */public class ChatClient extends javax.swing.JFrame { String username="Adam"; String ip="127.0.0.1"; int port=8888; int type=0; UserConf userconf; ClientReceive recvThread; Socket socket; ObjectInputStream input; ObjectOutputStream output; /** Creates new form ChatClient */ public ChatClient() { initComponents(); init(); } public void init(){ this.setLocationRelativeTo(this); userconf=new UserConf(); this.username=userconf.Username; System.out.println(username); } /** 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() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jComboBox1 = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jComboBox2 = new javax.swing.JComboBox(); jCheckBox1 = new javax.swing.JCheckBox(); jTextField1 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); showStatus = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Connect"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Exit"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "ALL" })); jLabel1.setText("SendTo:"); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "微笑地", "高兴地", "轻轻地", "生气地", "小心地", "静静地" })); jCheckBox1.setText("悄悄话"); jButton3.setText("Send"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); showStatus.setEditable(false); jLabel2.setText("表情: "); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE) .add(layout.createSequentialGroup() .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(40, 40, 40) .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) .add(layout.createSequentialGroup() .add(12, 12, 12) .add(jLabel1) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 13, Short.MAX_VALUE) .add(jLabel2) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(26, 26, 26) .add(jCheckBox1) .add(26, 26, 26)) .add(layout.createSequentialGroup() .add(43, 43, 43) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 267, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jButton3) .addContainerGap(34, Short.MAX_VALUE)) .add(showStatus, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jButton1) .add(jButton2)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 389, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(18, 18, 18) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel1) .add(jCheckBox1) .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel2)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jButton3)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(showStatus, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: System.exit(0); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: Connect(); }//GEN-LAST:event_jButton1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: SendMessage(); }//GEN-LAST:event_jButton3ActionPerformed public void Connect(){ try{ socket=new Socket(ip,port); }catch(Exception e){ JOptionPane.showConfirmDialog( this,"不能连接到指定的服务器。\n请确认连接设置是否正确。","提示", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE); return; } try{ output=new ObjectOutputStream(socket.getOutputStream()); output.flush(); input=new ObjectInputStream(socket.getInputStream()); output.writeObject(username); output.flush(); recvThread=new ClientReceive(socket,output,input,this.jComboBox1,this.jTextArea1,this.showStatus); recvThread.start(); this.jTextArea1.append("连接服务器 "+ip+":"+port+" 成功...\n"); type=1; }catch(Exception ex){ ex.getMessage(); } } public void SendMessage(){ String tosomebody=(String)this.jComboBox1.getSelectedItem(); String status=""; if(this.jCheckBox1.isSelected()){ status="悄悄话"; } String action=(String)this.jComboBox2.getSelectedItem(); String message=this.jTextField1.getText(); if(socket.isClosed()){ return; } try{ output.writeObject("聊天信息"); output.flush(); output.writeObject(tosomebody); output.flush(); output.writeObject(status); output.flush(); output.writeObject(action); output.flush(); output.writeObject(message); output.flush(); }catch(Exception e){ e.getMessage(); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ChatClient().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField showStatus; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -