📄 game.java
字号:
public void back()
{
this.qiBan.back();
}
public void loss()
{
int mes = JOptionPane.showConfirmDialog(this.qiBan.game,"确定认输吗?","认输",JOptionPane.WARNING_MESSAGE);
if(mes!=JOptionPane.YES_OPTION)
return;
if(this.qiBan.ifBlack)
this.qiBan.setStr(" 游戏结束,白胜");
else
this.qiBan.setStr(" 游戏结束,黑胜");
this.qiBan.setGameOver(true);
this.endGame();
}
public void qiuHe()
{
int mes = JOptionPane.showConfirmDialog(this.qiBan.game,"确定求和吗?","求和",JOptionPane.WARNING_MESSAGE);
if(mes!=JOptionPane.YES_OPTION)
return;
if(this.qiBan.ifRobot)
{
this.qiBan.setStr("游戏结束,双方和棋");
this.qiBan.setGameOver(true);
}
else
{
this.qiBan.people.qiuHe();
}
}
public void change()
{
this.qiBan.change();
if(this.qiBan.ifRobot)
this.qiBan.robot.next();
else
{
this.qiBan.people.change();
this.qiBan.people.next();
}
}
public void exit()
{
chatPanel.close();
this.qiBan.people.exit();
System.exit(0);
}
public void paint(Graphics g)
{
if(background==null||background.getWidth(this)<0
||background.getHeight(this)<0)
background = this.getToolkit().getImage("images\\background2.JPG");
g.drawImage(background,0,20,this);
}
}
class FunctionPanel extends JPanel
{
Game game;
Button start,back,loss,qiuHe;
TextArea text;
public FunctionPanel(Game game)
{
this.game = game;
//组件初始化
start = new Button("开始");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Start();
}
});
back = new Button("悔棋");
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//后需更改
back();
}
});
loss = new Button("认输");
loss.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//后需更改
loss();
}
});
qiuHe = new Button("求和");
qiuHe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//后需更改
qiuHe();
}
});
text = new TextArea();
text.setEditable(false);
text.setText("欢迎使用小文版五子棋\n设置好游戏难度后\n按开始进行游戏\n更多帮助信息请查看帮助菜单");
//添加到面板上
this.setLayout(null);
this.add(text);
this.add(start);
this.add(back);
this.add(loss);
this.add(qiuHe);
text.setBounds(20,30,160,75);
start.setBounds(60,125,80,20);
back.setBounds(60,155,80,20);
loss.setBounds(60,185,80,20);
qiuHe.setBounds(60,215,80,20);
//面板设置
this.setOpaque(false);
this.setSize(200,300);
}
public void setText(String str)
{
this.text.setText(str);
}
public void Start()
{
game.startGame();
}
public void startGame()
{
start.setEnabled(false);
back.setEnabled(true);
loss.setEnabled(true);
qiuHe.setEnabled(true);
}
public void endGame()
{
start.setEnabled(true);
back.setEnabled(false);
loss.setEnabled(false);
qiuHe.setEnabled(false);
}
public void back()
{
game.back();
}
public void change()
{
game.change();
}
public void loss()
{
game.loss();
}
public void qiuHe()
{
game.qiuHe();
}
}
/*聊天面板已完工作:)
*不能自动换行,留待下次再完成
*/
class ChatPanel extends JPanel implements Runnable
{
//发送IP
String IP;
//聊天内容输入筐
TextField jfChat;
//姓名
String name = "你自己";
//显示面板
TextArea textChat;
//发送按纽
Button sendButton;
//用与接收和发送数据的Socket
DatagramSocket ds;
//接收数据的线程
Thread receive;
//发送开关
//boolean ifSend = false;
public ChatPanel()
{
//Socket
try
{
ds = new DatagramSocket(5000);
}
catch(Exception e)
{
e.printStackTrace();
}
//接收线程
receive = new Thread(this);
receive.start();
//GUI
this.sendButton = new Button("发送");
this.textChat = new TextArea();
textChat.setFont(new Font(null,Font.PLAIN,20));
jfChat = new TextField(15);
textChat.setEditable(false);
//textChat.setBackground(Color.pink.brighter());
this.setSize(200,300);
this.setOpaque(false);
this.setLayout(null);
this.add(jfChat);
this.add(textChat);
this.add(this.sendButton);
textChat.setBounds(0,0,195,275);
jfChat.setBounds(0,275,155,20);
sendButton.setBounds(155,275,40,20);
//发送
jfChat.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*if(!ifSend)
return;*/
try
{
String str = name+": "+jfChat.getText();
DatagramPacket dpSend = new DatagramPacket(
str.getBytes(),str.getBytes().length,
InetAddress.getByName(IP),5000);
ds.send(dpSend);
}
catch(Exception ex)
{
ex.printStackTrace();
}
textChat.append("你说 : "+jfChat.getText()+"\n");
jfChat.setText("");
}
});
this.sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*if(!ifSend)
return;*/
try
{
String str = name+": "+jfChat.getText();
DatagramPacket dpSend = new DatagramPacket(
str.getBytes(),str.getBytes().length,
InetAddress.getByName(IP),5000);
ds.send(dpSend);
}
catch(Exception ex)
{
ex.printStackTrace();
}
textChat.append("你说: "+jfChat.getText()+"\n");
jfChat.setText("");
}
});
}//end ChatPanen()
public void setIP(String IP,String name)
{
this.IP = IP;
this.name = name;
//this.ifSend = true;
}
public void run()
{
while(!ds.isClosed())
{
byte[] buf = new byte[1024];
DatagramPacket dpReceive = new DatagramPacket
(buf,buf.length);
try
{
ds.receive(dpReceive);
}
catch(Exception e)
{
if(ds.isClosed())
return;
e.printStackTrace();
}
String str = new String(buf,0,dpReceive.getLength());
this.textChat.append(str+"\n");
}
}//run()
public void close()
{
this.receive = null;
this.ds.close();
}
}//end class ChatFrame
class Test
{
public static void main( String [] args)
{
new Game().setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -