飘遥诗涯 2011-12-16 09:19
浏览 226
已采纳

求帮忙改改,使能运行!!!

package javac;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class Server extends JFrame {
private ObjectOutputStream out;
private ObjectInputStream in;

private ServerSocket ss;

private Socket socket;
private JTextField tf_message;

private Server  frame;

private JButton btn_send;

private JTextArea ta_show;

private JScrollPane scrollPane1;

public Server()
{
super ("Server-服务器端程序");
Container container = getContentPane();
container.setLayout( new FlowLayout() );
tf_message=new JTextField( 10 );
container.add( tf_message );
btn_send = new JButton( "发送" );
btn_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_sendActionPerformed(e);
}

    private void btn_sendActionPerformed(ActionEvent e) {
        // TODO 自动生成方法存根

    }
});
container.add( btn_send );
setSize( 275, 100 );
setVisible( true );
Box box = Box.createHorizontalBox();
ta_show = new JTextArea( 5, 12 );
ta_show.setEditable(false );
box.add( new JScrollPane(ta_show) ); 
container.add( box );      
setSize( 500, 400 );
setVisible( true );
frame = new Server();
frame.setVisible(true);
try {
    ss = new  ServerSocket(8888);
    frame.show("等待连接,请稍候……");
    socket = ss.accept();
    frame.show(" 已与客户端建立连接!");
  out=new ObjectOutputStream(socket.getOutputStream());
    frame.setOut(out);
    out.flush();
    in = new ObjectInputStream(socket.getInputStream());
    String message = "";

    while (true) {
        try {
            message = (String) in.readObject();
            System.out.println(message);
            if (message.equals(Constant.CONNECT_QUIT)) {
                frame.setOut(null);
                break;
            }
            frame.show("客户端>> " + message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 frame.show("客户端" + socket.getInetAddress().getHostName()+ "中断了连接!");
  out.close();
    in.close();
    socket.close();
}
catch (IOException ioException) {
    ioException.printStackTrace();
}

}

public static void main(String args[]) {
Server application = new Server();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}
public void setOut(ObjectOutputStream out) {
this.out = out;
}
public void show(String mess) {
ta_show.append(mess + "\n");
ta_show.setCaretPosition(ta_show.getText().length());
}

  • 写回答

2条回答

  • 不爱吃萝卜的兔子 2011-12-16 11:08
    关注

    [code="java"]
    public Server() {
    super("Server-服务器端程序");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    tf_message = new JTextField(10);
    container.add(tf_message);
    btn_send = new JButton("发送");
    btn_send.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    btn_sendActionPerformed(e);
    }

            private void btn_sendActionPerformed(ActionEvent e) {
                // TODO 自动生成方法存根
    
            }
        });
        container.add(btn_send);
        setSize(275, 100);
        setVisible(true);
        Box box = Box.createHorizontalBox();
        ta_show = new JTextArea(5, 12);
        ta_show.setEditable(false);
        box.add(new JScrollPane(ta_show));
        container.add(box);
        setSize(500, 400);
        setVisible(true);
    
        this.setVisible(true);
        try {
            ss = new ServerSocket(8888);
            this.show("等待连接,请稍候……");
            Thread.sleep(2000);
            socket = ss.accept();
            this.show(" 已与客户端建立连接!");
            out = new ObjectOutputStream(socket.getOutputStream());
            this.setOut(out);
            out.flush();
            in = new ObjectInputStream(socket.getInputStream());
            String message = "";
    
            while (true) {
                try {
                    message = (String) in.readObject();
                    System.out.println(message);
                    if (message.equals("test")) {
                        this.setOut(null);
                        break;
                    }
                    this.show("客户端>> " + message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this
                    .show("客户端" + socket.getInetAddress().getHostName()
                            + "中断了连接!");
            out.close();
            in.close();
            socket.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    [/code]

    楼主改了下,你看可以用不

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀