📄 tclient.java
字号:
import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;import java.io.*;public class TClient extends JFrame implements Runnable,ActionListener{ JTextField info = new JTextField(10); List list = new List(20); InputStream is; OutputStream os; BufferedOutputStream bos; BufferedInputStream bis; public TClient() { super("TCP客户端"); setSize(300,400); JPanel p = new JPanel(); JLabel l = new JLabel("消息"); p.add(l); p.add(info); add(p,"South"); add(list,"Center"); } public void init(Socket s) { try{ is = s.getInputStream(); os = s.getOutputStream(); bos= new BufferedOutputStream(os); bis= new BufferedInputStream(is); info.addActionListener(this); new Thread(this).start(); }catch(Exception e){} } public void run() { try{ while(true) { byte [] b = new byte[1024]; int i = bis.read(b); list.add(new String(b,0,i),0); } }catch(Exception e){} } public void actionPerformed(ActionEvent e) { try{ byte [] b = info.getText().getBytes(); bos.write(b); bos.flush(); info.setText(""); }catch(Exception ec){} } public static void main(String [] args) { try{ System.out.println("正在连接服务器端"); Socket sk = new Socket(InetAddress.getByName("localhost"),8866); TClient tc = new TClient(); tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tc.setVisible(true); tc.init(sk); }catch(Exception e){System.out.println("尝试连接失败...");} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -