求小游戏代码 我是一个刚接触JAVA的新手,只懂一些简单的语句和语法,看的懂一些小程序,希望哪为好心人给点小游戏代码可以参考参考。另外如果可以的话给点大型程序的构建的思路。不胜感激。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class ChatCliend{public static void main(String[] args){MyFrame mf=new MyFrame();mf.launchFrame();}}class MyFrame extends Frame{TextField text;TextArea text2;Socket s;DataOutputStream dos=null;DataInputStream dis=null;boolean on = false;public void launchFrame(){this.setLocation( 400, 100);this.setSize(320, 500);text=new TextField(10);text2=new TextArea(100,1);text.addActionListener(new TextMonitor());add(text,BorderLayout.SOUTH);add(text2,BorderLayout.CENTER);this.addWindowListener(new WindowMonitor());this.setVisible(true);net();new Thread(new Server()).start();}//此方法联网public void net (){try {s= new Socket("10.50.140.53",8888);dos= new DataOutputStream(s.getOutputStream());dis=new DataInputStream(s.getInputStream());on=true;text2.setText("亲,您已经连接到服务器了,可以和您的朋友聊天了 "+'\n');} catch (UnknownHostException e) {text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");} catch (IOException e) {text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");}}//把内容发送给服务器public void sent(String str){try {dos.writeUTF(str);dos.flush();} catch (IOException e) {e.printStackTrace();}}//这个类用于监听text的行为,给予一定的响应。然后把内容发送给服务器public class TextMonitor implements ActionListener{//本地处理text中的内容 然后调用sent方法 把他发送给服务器public void actionPerformed(ActionEvent e){String str=text.getText().trim();text.setText("");sent(str);}}//这个类用于窗口的关毕class WindowMonitor extends WindowAdapter{public void windowClosing(WindowEvent e){off();System.exit(0);}}//用于起一个多线程 class Server implements Runnable{public void run(){try {while(on){String s1=dis.readUTF();text2.setText(text2.getText()+'\n'+s1+'\n');}} catch (IOException e) {}}}public void off(){try {if(s!=null)s.close();if(dis!=null)dis.close();if(dos!=null)dos.close();} catch (IOException e) {e.printStackTrace();}}} 上面的是客户端mport java.io.*; import java.net.*; import java.nio.CharBuffer; import java.util.*;public class ChatServer {ServerSocket ss=null;Socket s=null;boolean on =false;List<Client> clients=new ArrayList<Client>();public static void main(String[] args) {new ChatServer().start();}public void start(){try {ss=new ServerSocket(8888);on=true;}catch(Exception e){System.out.println("亲,端口已经被占用......");}try{while(on){s=ss.accept();Client c=new Client(s);clients.add(c);new Thread(c).start();System.out.println("已经连上了");}}catch(IOException e) {System.out.println(" 亲,不好意思,服务器已经崩溃...");}}class Client implements Runnable{ private Socket s=null;private boolean on1=false;private DataInputStream dis=null;private DataOutputStream dos=null; public Client(Socket s){ this.s=s;try {dis=new DataInputStream(s.getInputStream());dos=new DataOutputStream(s.getOutputStream());on1=true;} catch (IOException e) {try {if(s != null) s.close();if(dis != null)dis.close();if(dos != null) dos.close();} catch (IOException e1) {System.out.print("load......");} }} public void send(String str){try {dos.writeUTF(str);} catch (IOException e) {System.out.print("load......");}} public void run() {try {while(on1){String str=dis.readUTF();dos.writeUTF(str);System.out.println(str);for(int i=0;i<clients.size();i++){Client c = clients.get(i);c.send(str);}} }catch (IOException e) {System.out.print("load......");} }} }这是服务器端 ip地址根据自己的该 上面写的是我的ip 建议你学GUI吧,那个挺好玩的,还简单,可以提高兴趣 老鸟们帮忙看看,为什么一直抛异常 数据库连接问题 如何动态新建JLabel,就是用线程生成新的JLabel? 内部类的简单问题! 请教各位HashMap如何迭代??? 实现一个函数,在不使用第三个变量下,把两个参数的值对调 内码问题 JCreator 与Wireless 如何配置。在线等待中! 关于Jbuilder中的LookAndFeel(系统显示风格的问题) 高手看一下下面的错误,怎么搞定? 初级问题:如何运行带package的类? getResource()和getResourceAsStream()的用法与区别?
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatCliend{public static void main(String[] args){
MyFrame mf=new MyFrame();
mf.launchFrame();}
}
class MyFrame extends Frame{TextField text;TextArea text2;
Socket s;
DataOutputStream dos=null;
DataInputStream dis=null;
boolean on = false;public void launchFrame(){
this.setLocation( 400, 100);
this.setSize(320, 500);
text=new TextField(10);
text2=new TextArea(100,1);
text.addActionListener(new TextMonitor());
add(text,BorderLayout.SOUTH);
add(text2,BorderLayout.CENTER);
this.addWindowListener(new WindowMonitor());
this.setVisible(true);
net();
new Thread(new Server()).start();
}//此方法联网
public void net (){try {
s= new Socket("10.50.140.53",8888);
dos= new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
on=true;
text2.setText("亲,您已经连接到服务器了,可以和您的朋友聊天了 "+'\n');
} catch (UnknownHostException e) {text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");} catch (IOException e) {text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");}
}//把内容发送给服务器
public void sent(String str){try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {e.printStackTrace();
}
}//这个类用于监听text的行为,给予一定的响应。然后把内容发送给服务器
public class TextMonitor implements ActionListener{//本地处理text中的内容 然后调用sent方法 把他发送给服务器
public void actionPerformed(ActionEvent e){String str=text.getText().trim();
text.setText("");
sent(str);}
}//这个类用于窗口的关毕
class WindowMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e){
off();
System.exit(0);
}
}//用于起一个多线程
class Server implements Runnable{
public void run(){
try {
while(on){
String s1=dis.readUTF();
text2.setText(text2.getText()+'\n'+s1+'\n');
}
} catch (IOException e) {}
}
}
public void off(){
try {
if(s!=null)s.close();
if(dis!=null)dis.close();
if(dos!=null)dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
mport java.io.*;
import java.net.*;
import java.nio.CharBuffer;
import java.util.*;
public class ChatServer {
ServerSocket ss=null;
Socket s=null;
boolean on =false;
List<Client> clients=new ArrayList<Client>();public static void main(String[] args) {
new ChatServer().start();
}public void start(){
try {
ss=new ServerSocket(8888);
on=true;
}catch(Exception e){
System.out.println("亲,端口已经被占用......");
}
try{
while(on){
s=ss.accept();Client c=new Client(s);
clients.add(c);
new Thread(c).start();
System.out.println("已经连上了");
}
}catch(IOException e) {System.out.println(" 亲,不好意思,服务器已经崩溃...");
}
}
class Client implements Runnable{
private Socket s=null;
private boolean on1=false;
private DataInputStream dis=null;
private DataOutputStream dos=null;
public Client(Socket s){
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
on1=true;
} catch (IOException e) {
try {
if(s != null) s.close();
if(dis != null)dis.close();
if(dos != null) dos.close();
} catch (IOException e1) {System.out.print("load......");
}
}
}
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
System.out.print("load......");
}
}
public void run() {try {
while(on1){
String str=dis.readUTF();
dos.writeUTF(str);
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client c = clients.get(i);
c.send(str);
}
}
}catch (IOException e) {
System.out.print("load......");
}
}
}
}
这是服务器端 ip地址根据自己的该 上面写的是我的ip