本文共 5675 字,大约阅读时间需要 18 分钟。
package com.wode.test;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.zip.DeflaterOutputStream;public class Myserver extends Thread { private ServerSocket serversocket; public Myserver(int port) throws IOException { serversocket = new ServerSocket(port); //设置服务器超时时间 serversocket.setSoTimeout(10000); } @Override public void run() { while (true) { try { Socket socket = serversocket.accept(); DataOutputStream dStream = new DataOutputStream(socket.getOutputStream()); dStream.writeUTF("我是网吧小妹 你要干嘛?"); DataInputStream doStream = new DataInputStream(socket.getInputStream()); String string = doStream.readUTF(); switch (string) { case "你好": dStream.writeUTF("网吧小妹也对你说你好"); break; case "我爱你": dStream.writeUTF("我也爱你"); break; case "充值": dStream.writeUTF("充值成功"); break; case "买烟": dStream.writeUTF("给你烟"); break; case "约吗": dStream.writeUTF("叔叔不约"); break; case "买饮料": dStream.writeUTF("给你饮料"); break; case "伤心": dStream.writeUTF("心若向阳 无惧悲伤"); break; case "滚蛋": dStream.writeUTF("别对我那么凶嘛"); break; case "失恋": dStream.writeUTF("天涯何处无芳草 何必单恋一支花"); break; default: dStream.writeUTF("我不知道你在说什么诶= ="); break; } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { Thread server = new Myserver(9876); server.start(); }}package com.wode.test;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class MrchenUser extends JFrame { private JLabel jlLabel; private JTextField txtinput; public MrchenUser() { this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); JPanel jPanel = new JPanel(); jPanel.setLayout(null); jPanel.setBounds(1, 1, 500, 400); jlLabel = new JLabel("我是网吧小妹 你要干嘛?"); jlLabel.setFont(new Font("正楷", Font.BOLD, 20)); jlLabel.setBounds(0, 0, 400, 80); jPanel.add(jlLabel); txtinput = new JTextField(); txtinput.setBounds(0, 135, 400, 100); jPanel.add(txtinput); txtinput.addFocusListener(new txtinput(this, jlLabel)); JButton okbtn = new JButton("发送"); okbtn.setBounds(245, 240, 60, 30); jPanel.add(okbtn); okbtn.addActionListener(new okListener(txtinput, jlLabel)); JButton cancelbtn = new JButton("取消"); cancelbtn.setBounds(320, 240, 60, 30); jPanel.add(cancelbtn); this.add(jPanel); this.setVisible(true); } public static void main(String[] args) { new MrchenUser(); } static class okListener implements ActionListener { private JTextField txtinput; private JLabel jlLabel; public okListener(JTextField txtinput, JLabel jlLabel) { this.txtinput = txtinput; this.jlLabel = jlLabel; } @Override public void actionPerformed(ActionEvent arg0) { try { Socket socket = new Socket("localhost", 9876); DataInputStream dStream = new DataInputStream(socket.getInputStream()); String string = dStream.readUTF(); jlLabel.setText(string); DataOutputStream doStream = new DataOutputStream(socket.getOutputStream()); doStream.writeUTF(txtinput.getText()); DataInputStream dStream1 = new DataInputStream(socket.getInputStream()); String string1 = dStream.readUTF(); jlLabel.setText(string1); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } static class txtinput implements FocusListener { private JTextField txtinput; private JLabel jlLabel; public txtinput(JTextField txtinput, JLabel jlLabel) { this.txtinput = txtinput; this.jlLabel = jlLabel; } @Override public void focusGained(FocusEvent arg0) { if (txtinput.getText().length() != 0) { jlLabel.setText("我是网吧小妹 你要干嘛?"); } } @Override public void focusLost(FocusEvent arg0) { if (txtinput.getText().length() == 0) { jlLabel.setText("我是网吧小妹 你要干嘛?"); } } }}
转载地址:http://ujgfk.baihongyu.com/