📄 chatonetoone.java
字号:
/* * ChatOneToOne.java * * Created on 2007年11月13日, 下午10:42 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package 一对一聊天;import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;//import java.net.*;//import java.sql.*;/** * * @author baili */public class ChatOneToOne extends Frame{ /** Creates a new instance of ChatOneToOne */ Button clientBtn,serverBtn; TextArea ta; TextField tfaddress,tfport,tftype; int port; clientskt client; serverskt server; boolean iamserver; static ChatOneToOne frm; public ChatOneToOne() { clientBtn=new Button("客户端"); serverBtn=new Button("服务器端"); ta=new TextArea("",10,50, TextArea.SCROLLBARS_BOTH); tfaddress=new TextField("输入IP地址"); tfport=new TextField("输入端口号"); tftype=new TextField(50); tftype.addKeyListener(new TFListener()); ta.setEditable(false); setLayout(new FlowLayout()); add(tfaddress); add(tfport); add(clientBtn); add(serverBtn); add(ta); add(tftype); setSize(400,300); setTitle("一对一聊天程序"); clientBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { port=Integer.parseInt(tfport.getText()); client=new clientskt(tfaddress.getText(),port,frm); client.start(); tfaddress.setEnabled(false); tfport.setEnabled(false); clientBtn.setEnabled(false); serverBtn.setEnabled(false); } }); serverBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { port=Integer.parseInt(tfport.getText()); server=new serverskt(port,frm); server.start(); iamserver=true; tfaddress.setText("成为服务器端"); tfaddress.setEnabled(false); tfport.setEnabled(false); clientBtn.setEnabled(false); serverBtn.setEnabled(false); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); show(); } public static void main(String args[]) { frm=new ChatOneToOne(); } private class TFListener implements KeyListener { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) { ta.append(">"+tftype.getText()+"\n"); if(iamserver) server.dataout(tftype.getText()); else client.dataout(tftype.getText()); tftype.setText(""); } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -