📄 chatserver.java
字号:
/* * ChatServer.java * * Created on 2008年4月9日, 下午11:22 */package org.Adam;import java.net.*;import java.io.*;/** * * @author Administrator */public class ChatServer extends javax.swing.JFrame { public static int port=8888; public ServerSocket serversocket=null; UserLinkList userlink; ServerListen listenThread; Node node; /** Creates new form ChatServer */ public ChatServer() { initComponents(); this.setLocationRelativeTo(this); } /** 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() { jToolBar1 = new javax.swing.JToolBar(); 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(); jTextField1 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); showStatus = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jToolBar1.setRollover(true); jButton1.setText("Start"); jButton1.setFocusable(false); jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jToolBar1.add(jButton1); jButton2.setText("Exit"); jButton2.setFocusable(false); jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jToolBar1.add(jButton2); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "ALL" })); jButton3.setText("Send"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); showStatus.setEditable(false); jLabel1.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(jToolBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) .add(layout.createSequentialGroup() .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 317, 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, 414, Short.MAX_VALUE) .add(layout.createSequentialGroup() .addContainerGap() .add(jLabel1) .add(27, 27, 27) .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(226, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 357, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(18, 18, 18) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel1)) .add(10, 10, 10) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jButton3) .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, 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: StartService(); }//GEN-LAST:event_jButton1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: sendSystemMessage(); }//GEN-LAST:event_jButton3ActionPerformed public void StartService(){ try{ serversocket=new ServerSocket(port,10); this.jTextArea1.append("服务端已经启动,在"+port+"端口侦听...\n"); }catch(Exception f){ f.getMessage(); } userlink=new UserLinkList(); listenThread=new ServerListen(serversocket,jTextArea1,showStatus,jComboBox1,userlink); listenThread.start(); } public void sendSystemMessage(){ String tosomebody=this.jComboBox1.getSelectedItem().toString(); String message=this.jTextField1.getText()+"\n"; this.jTextArea1.append(message); if(tosomebody.equals("ALL")){ sendMessageToAll(message); }else{ node=userlink.findUser(tosomebody); try{ node.output.writeObject("系统信息"); node.output.flush(); node.output.writeObject(message); node.output.flush(); }catch(Exception f){ f.getMessage(); } } this.jTextField1.setText(""); } public void sendMessageToAll(String message){ int count=userlink.getCount(); int i=0; while(i<count){ node=userlink.findUser(i); if(node==null){ i++; continue; } try{ node.output.writeObject("系统信息"); node.output.flush(); node.output.writeObject(message); node.output.flush(); }catch(Exception f){ } i++; } this.jTextField1.setText(""); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ChatServer().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.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JToolBar jToolBar1; private javax.swing.JTextField showStatus; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -