这个程序是一个简易聊天程序,包括两个文件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(); } }
这是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(); } }
解决方案 »
- jquery-1.6.4.min.js报错,系统调用Jquery也报错了一大堆
- 用java实现矩阵运算 行列式运算 组合数计算
- 怎么在java控制台中打印出byte数组的数据及中文怎么处理 ?
- InstallAnywhere 8 Enterprise 如何打包程序中的参数
- SUN公司最新发布的JDK1.5 为什么装好了以后没有tool.jar文件???
- 如何确定FILE类型,是PLAIN TEXT还是BINARY的
- 用Jtable做数据管理的界面,不过当用户edit时,按add或update
- 高分相赠!!!如何捕捉throws的异常????
- 问个关于JBuilder的问题(JBuilder 6)
- Oracle中大字段问题,数据大了就写不进去了。
- java为何用String类型保存大文本字符会出现字符丢失问题
- 窗口画点的问题程序
public static void main(String[] args) throws Exception{
//将当前类作线程启动
serverTwo run = new serverTwo();
Thread t=new Thread(run);
t.start();
}
另一个类 改法类似,就可以看见界面了
jframe.setVisible(true);
jframe.setSize();
....
这些代码是用于显示SWING界面的代码。如果没有的话将不会看到界面,因为JFrame默认是不显示出来的。如果愿意可以看哈我的一个资源。那资源和你需要的差不多。也是一个简单的聊天程序。希望有用。