📄 tryclient.java
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JToolBar;
import java.net.InetAddress;
public class TryClient
{
public static void main(String args[])
{
TServer app=new TServer();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.show();
app.setConnectIp();
app.runclient(); //开始开始运行服务器
}
}
class TServer extends JFrame
{
JButton button[]=new JButton[9]; //9个按钮,用于接受输入并显示图案
JTextArea textArea; //文本域,用于显示对方传送过来的信息
JButton select1,select2,exit,restart,startgame; //分别用于图案选择与退出铵钮
JToolBar toolbar=new JToolBar("工具栏");
int record[]=new int[9]; //用于记录9个面板按钮的当前状态
JPanel panel=new JPanel();
Icon icon[]=new Icon[2];
Icon image[]=new Icon[2]; //贴图的图标
Icon backcolor=new ImageIcon("bground.gif");
int imageType; //用于记录两张图标应该选择哪一张
Socket client;
String chatserver;
ObjectOutputStream output;
ObjectInputStream input; //定义输入输出流,用于接收数据
boolean start=true; //判断游戏是否已经开始
String order; //专门用于发送消息的字符串
boolean pressed; //判断是否已经按下过一次
int counter; //用于记录当前方棋子的数目
int totalnum,servernum,clientnum; //记录棋盘上棋子的总数
public TServer()
{
super("客户端游戏");
Container cpp=getContentPane();
toolbar=new JToolBar("工具栏"); //创建工具栏
counter=0;
totalnum=0;
imageType=0;
ActionListener command=new Actionhandler(); //添加事件监听器
cpp.setLayout(new BorderLayout());
textArea=new JTextArea(4,20);
icon[0]=new ImageIcon("circle.gif");
icon[1]=new ImageIcon("line.gif");
select1=new JButton("",icon[0]);
select2=new JButton("",icon[1]);
startgame=new JButton("startGame");
startgame.addActionListener(command);
exit=new JButton(" Exit ");
exit.addActionListener(command);
restart=new JButton("Restart");
restart.addActionListener(command);
image[0]=new ImageIcon("cimage.gif");
image[1]=new ImageIcon("limage.gif"); //装载图标
toolbar.add(select1);
toolbar.addSeparator();
toolbar.add(select2);
toolbar.addSeparator();
toolbar.add(startgame);
toolbar.addSeparator();
toolbar.add(exit);
toolbar.addSeparator();
toolbar.add(restart); //创建工具栏
cpp.add(toolbar,BorderLayout.NORTH);
panel.setSize(200,200);
panel.setLayout(new GridLayout(3,3)); //在框架中另外创建一个面板类
int i=0;
for( i=0;i<9;i++)
{
button[i]=new JButton(""); //创建桌面上按钮
button[i].setBackground(new Color(25,80,10));
button[i].addActionListener(command);
panel.add(button[i]); //把按钮添加到面板中
}
for(i=0;i<9;i++)
record[i]=-1; //初始化记录数组
cpp.add(panel,BorderLayout.CENTER);
cpp.add(new JScrollPane(textArea),BorderLayout.SOUTH); //安排桌面的布局
/*
try{
host=InetAddress.getLocalHost();
}
catch(Exception e)
{
}
chatserver=host.getHostAddress();
*/
setSize(400,400);
setVisible(true);
}//构造方法结束
public void setConnectIp()
{
chatserver=JOptionPane.showInputDialog(TServer.this,"请输入服务器端主机的IP地址");
}
public void runclient()
{
//建立服务器,接受信息,并处理信息
try
{
while ( true )
{
connectToServer(); //连接到服务器
getStreams(); //得到对方发送过来的消息
processConnection(); //处理消息
closeConnection(); //关闭连接
}
}
catch ( EOFException eofException )
{
System.out.println( "Client terminated connection" );
}
//处理输入输出异常
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}//end_runServer()
private void getStreams() throws IOException
{
//建立输入流对象
output = new ObjectOutputStream(
client.getOutputStream() );
//强行输出缓冲区
output.flush();
//建立输出流对象
input = new ObjectInputStream(
client.getInputStream() );
textArea.append( "\nGot I/O streams\n" );
}
private void closeConnection() throws IOException
{
textArea.append( "\nUser terminated connection" );
output.close();
input.close();
client.close();
}
// 连接到服务器
private void connectToServer() throws IOException
{
textArea.setText( "Attempting connection\n" );
//创建Socket,与服务器端连接
client = new Socket(
InetAddress.getByName( chatserver ), 5000 );
//显示连接方面的信息
textArea.append( "Connected to: " +
client.getInetAddress().getHostName() );
}
private void processConnection() throws IOException
{
//发送连接成功的信息到服务器端
String message = "SERVER>>> Connection successful,do you want to start game?";
output.writeObject( message );
output.flush();
//处理客户端发送过来的消息
do {
try
{
message = ( String ) input.readObject();
textArea.append( "\nServer>>>" + message );
if(message.substring(0,5).equals("start")) //对方发出开始的命令
{
start=false;
if(message.charAt(11)=='0') imageType=1;
else imageType=0; //选择贴图的图标
}
else if(message.substring(0,5).equals("order")) //对方贴子的操作
{
int i=0;
pressed=true; //对方已经按下,自己方可以接着下
i=Integer.parseInt(message.substring(6,7));
if(imageType==0)
{
record[i]=1; //对方的子
button[i].setIcon(image[1]); //贴上图标
servernum++;
totalnum++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -