这个程序是一个简易聊天程序,包括两个文件serverTwo.java和clientTwo.java。编译没有问题.麻烦高手看一下为什么运行在eclipse上没有界面显示出来。 
这是serverTwo.java: 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.swing.*; public class serverTwo extends JFrame implements ActionListener,Runnable{ /** 
* @param args 
*/ 
    
//声明数据通信类对象和窗体对象 
InetAddress addr; 
ServerSocket se; 
Socket so; 
DataOutputStream out; 
DataInputStream in; 
JButton btn; 
JTextField jtf; 
JTextArea jta; 
JScrollPane jsp; 
serverTwo()throws Exception{ 
//构建可视化窗体 
btn=new JButton("发送"); 
jtf=new JTextField(35); 
btn.addActionListener(this); 
this.setLayout(new BorderLayout()); 
jta=new JTextArea(); 
JPanel jp=new JPanel(); 
jp.add(jtf); 
jp.add(btn); 
//创建滚动条面板 
jsp=new JScrollPane(); 
jsp.setViewportView(jta); 
this.setTitle("Server"); 
this.add(BorderLayout.SOUTH,jp); 
this.add(jsp); 
this.setSize(300,200); 
this.setVisible(true); 
//创建ServerSocket 
addr=InetAddress.getByName("AB4E9866CF784D0"); 
se=new ServerSocket(10000); 
System.out.println("服务器启动....."); 
so=se.accept(); 
System.out.println("客户端运行....."); 
//创建数据流 
in=new DataInputStream(so.getInputStream()); 
out=new DataOutputStream(so.getOutputStream()); 
} public void run(){ 
//接受客户端消息 
try{ 
while(true){ 
jta.append("client:"+in.readUTF()); 
jta.append("\n"); 


catch (Exception ex){ 
System.out.println(ex.getMessage()); 


public void actionPerformed(ActionEvent e){ 
try{ 
//向客户端发送消息 
out.writeUTF(jtf.getText()); 
out.flush(); 
jta.append("me:"+jtf.getText()); 
jta.append("\n"); } 
catch(Exception ex) 

System.out.println(ex.getMessage()); 


public static void main(String[] args) throws Exception{ 
//将当前类作线程启动 
Thread t=new Thread(); 
t.start(); 


这是clientTwo.java: 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; import javax.swing.*; 
public class clientTwo extends JFrame implements ActionListener,Runnable{ /** 
* @param args 
*/ 
Socket so; 
DataOutputStream out; 
DataInputStream in; 
JButton btn; 
JTextField jtf; 
JTextArea jta; 
JScrollPane jsp; 
clientTwo()throws Exception{ 
//构建可视化窗体 
btn=new JButton("发送"); 
jtf=new JTextField(35); 
btn.addActionListener(this); 
this.setLayout(new BorderLayout()); 
jta=new JTextArea(); 
JPanel jp=new JPanel(); 
jp.add(jtf); 
jp.add(btn); 
//创建滚动条面板 
jsp=new JScrollPane(); 
jsp.setViewportView(jta); 
this.setTitle("Client"); 
this.add(BorderLayout.SOUTH,jp); 
this.add(jsp); 
this.setSize(300,200); 
this.setVisible(true); 
//创建客户端套接字并创建输入输出流 
so=new Socket("AB4E9866CF784D0",1000); 
in=new DataInputStream(so.getInputStream()); 
out=new DataOutputStream(so.getOutputStream()); 

public void run(){ 
//接受服务器返回的消息 
try{ 
while(true){ 
jta.append("Server:"+in.readUTF()); 
jta.append("\n"); 


catch (Exception ex){ 
System.out.println(ex.getMessage()); 


public void actionPerformed(ActionEvent e){ 
try{ 
//向客户端发送消息 
out.writeUTF(jtf.getText()); 
out.flush(); 
jta.append("me:"+jtf.getText()); 
jta.append("\n"); } 
catch(Exception ex) 

System.out.println(ex.getMessage()); 


public static void main(String[] args) throws Exception{ 
// TODO Auto-generated method stub 
Thread t=new Thread(); 
t.start(); } }

解决方案 »

  1.   

    又见这个问题:我在另一个帖子已经回复你了
    public static void main(String[] args) throws Exception{ 
    //将当前类作线程启动 
    serverTwo run = new serverTwo();
    Thread t=new Thread(run); 
    t.start(); 

     另一个类 改法类似,就可以看见界面了
      

  2.   

    没看见如下代码:
    jframe.setVisible(true);
    jframe.setSize();
    ....
    这些代码是用于显示SWING界面的代码。如果没有的话将不会看到界面,因为JFrame默认是不显示出来的。如果愿意可以看哈我的一个资源。那资源和你需要的差不多。也是一个简单的聊天程序。希望有用。