📄 clientechat.java
字号:
/* * ClienteChat.java * * Created on 25 de abril de 2006, 13:26 */import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;public class ClienteChat extends JFrame {// Version 1.0 Miguel Angel Abi醤 Septiembre 2002public static void main(String[] args) throws IOException, ConnectException{ boolean packFrame = false; try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); if(args.length <= 0) { System.out.println("Falta la IP"); JOptionPane.showMessageDialog(null,"Falta la direccion IP", "Informaci髇 para el usuario", JOptionPane.WARNING_MESSAGE); } else { ClienteChat f=new ClienteChat(args[0]); if (packFrame) { f.pack(); } else { f.validate(); } Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = f.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } f.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); f.setVisible(true); } } catch(Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null,"Error en el gestor de apariencia y"+ "estilo", "Informaci髇 para el usuario", JOptionPane.WARNING_MESSAGE); System.exit(0); } } JPanel contentPane; JTextField txtMensaje = new JTextField(); JTextArea txtArea = new JTextArea(); JButton btnCerrar = new JButton(); Socket scliente; Sesion sesion; PrintStream salida; BufferedReader entrada; JScrollPane scrollPane = new JScrollPane(); JLabel lblTexto = new JLabel(); public ClienteChat(String ip) { enableEvents(AWTEvent.WINDOW_EVENT_MASK); txtArea.setEditable(false); try { iniciar(); /* InetAddress direccion = InetAddress.getLocalHost(); byte direccionIp[] = direccion.getAddress(); int dirip[] = new int[4]; for(int i = 0; i<4; i++) { if(direccionIp[i] < 0) { dirip[i] = 128+ (128+direccionIp[i]); } else { dirip[i] = direccionIp[i]; } } String ip = String.valueOf(dirip[0])+"." + String.valueOf(dirip[1])+"." + String.valueOf(dirip[2])+"." + String.valueOf(dirip[3]);*/ scliente=new Socket(ip, 9000); salida=new PrintStream(scliente.getOutputStream()); entrada= new BufferedReader(new InputStreamReader(scliente.getInputStream())); sesion=new Sesion(this); sesion.start(); } catch(Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null,"Fallo en la conexi髇", "Informaci髇 para el usuario", JOptionPane.WARNING_MESSAGE); System.exit(0); } } private void iniciar() throws Exception { contentPane = (JPanel) this.getContentPane(); txtMensaje.setBounds(new Rectangle(117, 49, 256, 29)); txtMensaje.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { txtMensaje_actionPerformed(e); } }); contentPane.setLayout(null); this.setSize(new Dimension(400, 400)); this.setTitle("Chat"); btnCerrar.setText("Cerrar"); btnCerrar.setBounds(new Rectangle(150, 323, 76, 29)); btnCerrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { btnCerrar_actionPerformed(e); } }); scrollPane.setBounds(new Rectangle(9, 106, 370, 205)); lblTexto.setBackground(Color.red); lblTexto.setFont(new java.awt.Font("Dialog", 1, 12)); lblTexto.setText("Escriba texto:"); lblTexto.setBounds(new Rectangle(17, 49, 87, 32)); contentPane.add(txtMensaje, null); contentPane.add(btnCerrar, null); contentPane.add(lblTexto, null); contentPane.add(scrollPane, null); scrollPane.getViewport().add(txtArea, null); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { salida.close();System.exit(0); } } void txtMensaje_actionPerformed(ActionEvent e) { salida.println(txtMensaje.getText()); txtMensaje.setText(""); } void btnCerrar_actionPerformed(ActionEvent e) { salida.close(); System.exit(0); }}/* InetAddress direccion = InetAddress.getLocalHost(); byte direccionIp[] = direccion.getAddress(); int dirip[] = new int[4]; for(int i = 0; i<4; i++) { if(direccionIp[i] < 0) { dirip[i] = 128+ (128+direccionIp[i]); } else { dirip[i] = direccionIp[i]; } } String ip = String.valueOf(dirip[0])+"." + String.valueOf(dirip[1])+"." + String.valueOf(dirip[2])+"." + String.valueOf(dirip[3]); Socket scliente= new Socket(ip,9000); BufferedReader entrada = new BufferedReader(new InputStreamReader(scliente.getInputStream())); PrintWriter salida = new PrintWriter(scliente.getOutputStream(),true); BufferedReader entradaConsola = new BufferedReader(new InputStreamReader(System.in)); new threadCliente(entrada).start(); while(true) salida.println(entradaConsola.readLine()); } }class threadCliente extends Thread{ BufferedReader entrada; public threadCliente(BufferedReader entrada)throws IOException { this.entrada = entrada; } public void run() { String linea; try { while((linea=entrada.readLine())!=null) System.out.println(linea); } catch(IOException e) { JOptionPane.showMessageDialog(null,"Error en la comunicacion","Informacion para el usuario",JOptionPane.WARNING_MESSAGE); } finally { System.exit(0); } }*///}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -