- 论坛徽章:
- 0
|
大哥~改回来了还是不行啊
- import java.net.*;
- import java.io.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.event.*;
- public class Client implements Runnable
- {
- Thread thread;
- static Socket clientS;
- static boolean end = true;
- static boolean startEcho = false;
- static BufferedReader in;
- static PrintStream out;
- static String addr = "127.0.0.1";
- static int port = 1100;
- static String exit = "exit";
- static JButton Button_1 = new JButton("连接"); //定义连接按钮.
- static JButton Button_2 = new JButton("中断"); //定义退出按钮.
- static final JTextArea Jarea_1 = new JTextArea(13,33); //定义输出框 行数,列数
- static final JTextField Jtext_1 = new JTextField(30); //定义聊天框
- static final JTextField Jtext_2 = new JTextField(20); //定义IP地址框
- public static void main(String args[])
- {
- Client client = new Client();
- client.FrameStart();
- client.ListenerStart();
-
- } // end main()
- public void FrameStart()
- {
- //创建窗体
- JFrame frame = new JFrame("【点对点聊天程序 - 客户端】 vision 1.0");
- Jtext_2.setText("127.0.0.1");
- //创建面板 1, 2, 3
- JPanel panel_1 = new JPanel(); //输入
- JPanel panel_2 = new JPanel(); //工具栏
- JPanel panel_3 = new JPanel(); //输出
- Jarea_1.setEditable(false); //输出框不可以输入字符.
- Jarea_1.setBackground(Color.white);
- ScrollPane Scroll = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); //定义滚动条.
-
- //添加组件到面板.
- panel_1.add(new JLabel("聊天内容:")); //添加标签
- panel_1.add(Jtext_1); //聊天内容输入框.
- panel_2.add(new JLabel("目标IP:"));
- panel_2.add(Jtext_2); //IP输入框.
- panel_2.add(Button_1); //连接按钮.
- panel_2.add(Button_2); //退出按钮.
- panel_3.add(Jarea_1);
- panel_3.setBorder(BorderFactory.createTitledBorder("聊天框"));
- Scroll.add(panel_3); //滚动条绑定面板 1_3
-
- //添加面板 1 ,2 到窗体的指定位置
- frame.getContentPane().add("South",panel_1); //输入框
- frame.getContentPane().add("North",panel_2);
- frame.getContentPane().add("Center",Scroll);
-
- //设置关闭窗体工作方式
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- //获得当前计算机屏幕尺寸.
- Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
- //设置窗口显示位置.
- int x = (dim.width)/2-150;
- int y = (dim.height)/2-250;
- //移动窗口到(x , y)坐标处,这里是屏幕的中央.
- frame.setLocation(x,y);
- //设置窗口大小
- frame.setSize(420,400);
- //设置背景颜色.
- frame.setBackground(Color.red);
-
- //显示窗体
- frame.setVisible(true);
- }
- public boolean conn() //连接目标的方法.
- {
- try
- {
- clientS=new Socket(addr,port);
- out=new PrintStream(new BufferedOutputStream(clientS.getOutputStream()));
- in=new BufferedReader(new InputStreamReader(clientS.getInputStream()));
- return true;
- } catch (Exception e){return false;}
- }
- public void ListenerStart() //启动监听事件的方法.
- {
- //创建连接按钮监听事件. 使用以后会导致程序无反映。
- ActionListener btnListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- { //点击相关按钮后的事件.
- if (evt.getActionCommand().equals("连接"))
- {
- if (((Jtext_2.getText()).length())<=0) //输入内容不能为空.
- {
- Jarea_1.append("\n^_^ 好像没有输入内容哦!");
- } else {
- Jarea_1.append("\n稍等...");
- addr = Jtext_2.getText();
- Jarea_1.append("\n正在连接: "+Jtext_2.getText());
- if (conn())
- {
- ClientStart();
- } else {
- Jarea_1.append("\n无法连接 "+addr);
- }
-
- }
- }
- }
- };
- //创建退出按钮事件
- ActionListener exitListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- { //点击相关按钮后的事件.
- if (evt.getActionCommand().equals("中断"))
- {
- try
- {
- out.println("exit"); //写入缓冲区
- out.flush(); //把缓冲区内容写入到输出流中.
- } catch (Exception e){
- Jarea_1.append("\n还没有建立连接无法中断.");
- }
- }
- }
- };
- //创建聊天框输入监听事件
- ActionListener textListener = new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- try
- {
- if (!(Jtext_1.getText()).equals(""))
- {
- out.println(Jtext_1.getText()); //写入缓冲区
- out.flush(); //把缓冲区内容写入到输出流中.
- Jarea_1.append("\n"+Jtext_1.getText()); //获得输入框输入,添加到输出框.
- Jtext_1.setText(""); //清空输入框.
- } else {
- Jarea_1.append("\n请输入内容.");
- }
- } catch (Exception e){
- Jarea_1.append("\n请先连接对方...");
- }
- }
- };
-
- //添加监听对象.
- Button_1.addActionListener(btnListener); //连接.
- Button_2.addActionListener(exitListener); //退出.
- Jtext_1.addActionListener(textListener); //聊天内容输入框
-
- }
- public void ClientStart() //启动方法.
- {
- try
- {
- while (end)
- {
- thread = new Thread(this);
- thread.start();
- Jarea_1.append("\n"+addr+" 连接成功");
- String aline;
- while ((aline=in.readLine())!=null)
- {
- Jarea_1.append("\n"+aline);
- if (aline.equals("exit")) //如果服务器返回 "exit" 则跳出回圈.
- {
- end=false;
- thread.interrupt();
- break;
- }
- }
- //关闭各种流对象和 Socket 对象
- in.close();
- out.close();
- clientS.close();
- Jarea_1.append("\n"+addr+" 断开连接.");
- System.exit(0); //结束程序
- }
- }
- catch (Exception e)
- {
- Jarea_1.append("\n"+addr+" 无法连接.");
- }
- }//end ClientStart
- public void finalize()
- {
- try
- {
- clientS.close();
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- } //end finalize
- public void run()
- {
- } //end run
- }//end class
复制代码
[ 本帖最后由 bossyt 于 2007-1-16 11:17 编辑 ] |
|