Done这是我写的一个服务端程序,能实现功能,但是,窗口只有那么小一点,为什么?
这是我写的一个服务端程序,能实现功能,但是,窗口只有那么小一点,为什么? import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MySocketServer {
public static void main(String args[]){
new ServerService(8000,10);
}
}
class ServiceFrame extends Frame implements Runnable{
ServerService fatherListener;
Socket connectedClient;
TextArea serverMeg;
ServiceFrame(ServerService sv,Socket s){
fatherListener=sv;
connectedClient=s;
setTitle("服务器端Socket窗口");
setLayout(new BorderLayout());
serverMeg=new TextArea(10,50);
add("Center",serverMeg);
setVisible(true);
InetAddress clientAddress=connectedClient.getInetAddress();
serverMeg.append("Client connected"+"from\n"+clientAddress.toString()+".\n");
}
public void run(){
try{
BufferedReader sIn=new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));
PrintWriter sOut=new PrintWriter(connectedClient.getOutputStream());
sOut.print("Hello! Welcome connect to our server!\n");
sOut.flush();
String s=sIn.readLine();
while(! s.equals("Bye")){
serverMeg.append("Client 端输入的信息为:"+s+"\n");
s=sIn.readLine();
}
connectedClient.close();
}catch(Exception e){}
fatherListener.addMeg("Client"+"closed."+"\n");
dispose();
}
}
class ServerService extends Frame{
ServerSocket m_sListener;
TextArea listenerMeg;
public ServerService (int Port,int Count){
try{
m_sListener=new ServerSocket(Port ,Count);
setTitle("服务器端监听服务窗口");
this.addWindowListener(new WinAdpt());
setLayout(new BorderLayout());
listenerMeg=new TextArea("监听已启动\n",10,50);
add("Center",listenerMeg);
setVisible(true);
while(true){
Socket connected=m_sListener.accept();
InetAddress clientAddress=connected.getInetAddress();
listenerMeg.append("Client"+"connected"+clientAddress.toString()+".\n");
ServiceFrame myST1=new ServiceFrame(this,connected);
Thread myST2=new Thread(myST1);
myST2.start();
}
}catch(IOException e){}
}
public void addMeg(String s ){
listenerMeg.append(s);
}
}
class WinAdpt extends WindowAdapter{
public void windowClosing(WindowEvent e){
(e.getWindow()).dispose();
System.exit(0);
}
}
[[it] 本帖最后由 zzxwill 于 2008-6-7 13:29 编辑 [/it]]
----------------解决方案--------------------------------------------------------
serverMeg=new TextArea(10,50);
listenerMeg=new TextArea("监听已启动\n",10,50);
把这个的大小重新设置一下呢
或者用setSize方法指定一下窗口大小
用awt的组件比较少
还是用swing的好用
[[it] 本帖最后由 freish 于 2008-4-8 10:35 编辑 [/it]]
----------------解决方案--------------------------------------------------------