服务器端:
package www.QQQ;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.*;
import java.util.Date;
SimpleDateFormat;
import javax.swing.*;
public class ServerDemo extends JFrame implements ActionListener,Runnable{
JButton cancel=new JButton("取消");
JButton submit=new JButton("发送");
JTextArea text1=new JTextArea();
JTextArea text2=new JTextArea();
JTextArea text4=new JTextArea();
JTextArea text3=new JTextArea("服务器端");
String a="";
static int press=0;
static String x="";
String t="";
public ServerDemo() {
this.setLayout(null);
text1.setBackground(Color.white);
text1.setForeground(Color.black);
text2.setBackground(Color.white);
text2.d);
text4.);
text3.setForeground(Color.BLUE);
JLabel label=new JLabel(new ImageIcon("image2/b.jpg"));
text3.setBounds(10,5,50,15);
text2.setBounds(5,250,290,120);
text1.setBounds(5,25,290,215);
text4.setBounds(1, 244, 298, 2);
cancel.setBounds(60,380,60,30);
sumbit.setBounds(180,380,60,30);
label.setBounds(303,2,194,456);
text1.setEditable(false);
text3.setEditable(false);
cancel.addActionListener(this);
sumbit.addActionListener(this);
this.add(text3);
this.add(text2);
this.add(text1);
this.add(text4);
this.add(label);
this.add(cancel);
this.add(submit);
this.setSize(490,460);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
ServerDemo sd=new ServerDemo();
sd.Server();
}
public void actionPerformed(ActionEvent e) {
Source()==cancel){
text2.setText("");
}
Source()==submit){
Text().length()>0){
Date nowTime = new Date();
SimpleDateFormat matter = new SimpleDateFormat("MM月dd日 HH:mm:ss");
t=matter.format(nowTime);
a="服务器说: "+Text();
x=x+"\n"+a+" ("+t+")";
text1.setText(x);
text2.setText("");
press=1;
}
}
}
public void Server() {
new Thread(this).start();入门的java游戏小程序
try {
ServerSocket ss=new ServerSocket(9203);
Socket s=ss.accept();
OutputStream OutputStream();
PrintStream pr=new PrintStream(out,true);
while (true) {
if(press==1&& a!=""){
pr.println(a);
press=0;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
ServerSocket sk=new ServerSocket(9204);
Socket s=sk.accept();
InputStream InputStream();
InputStreamReader is=new InputStreamReader(in);
BufferedReader br=new BufferedReader(is);
while(true){
String adLine();
Date nowTime = new Date();
SimpleDateFormat matter = new SimpleDateFormat("MM月dd日 HH:mm:ss");
t=matter.format(nowTime);
x=x+"\n"+x1+" ("+t+")";
text1.setText(x);
text2.setText("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端:
package www.QQ;
import java.util.Date;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.*;
SimpleDateFormat;
public class ClientDemo extends JFrame implements ActionListener,Runnable{
JButton cancel=new JButton("取消");
JButton submit=new JButton("发送");
JTextArea text1=new JTextArea();
JTextArea text2=new JTextArea();
JTextArea text3=new JTextArea("客户端");
JTextArea text4=new JTextArea();
String a="";
static String x="";
String t="";
static int press=0;
public ClientDemo() {
this.setLayout(null);
text1.setBackground(Color.white);
text1.setForeground(Color.black);
text2.setBackground(Color.white);
text2.d);
text4.);
text3.setForeground(Color.BLUE);
JLabel label=new JLabel(new ImageIcon("image1/a.jpg"));
text3.setBounds(10,5,40,15);
text2.setBounds(5,250,290,120);
text1.setBounds(5,25,290,215);
text4.setBounds(1, 244, 298, 2);
cancel.setBounds(60,380,60,30);
submit.setBounds(180,380,60,30);
label.setBounds(303,2,194,456);
text1.setEditable(false);
text3.setEditable(false);
cancel.addActionListener(this);
submit.addActionListener(this);
this.add(text3);
this.add(text2);
this.add(text1);
this.add(text4);
this.add(label);
this.add(cancel);
this.add(submit);
this.setSize(490,460);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
ClientDemo cd=new ClientDemo();
cd.Server();
}
public void actionPerformed(ActionEvent e) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论