📄 replaceblockgame.java
字号:
/**
* <p>Title:小游戏 </p>
*
* <p>Description:将方块摆放成“我爱中华,此志不渝” </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: 黄绍莽</p>
*
* @author 黄绍莽
* @version 1.0
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.Timer;
import java.util.TimerTask;
public class ReplaceBlockGame extends JFrame implements ActionListener
{//类开始
//实例变量
JButton[][] block=new JButton[3][3];
JTabbedPane newContentPane;
Timer time=new Timer();
int timeneed=0;
//钩爪方法
public ReplaceBlockGame()
{
setTitle("排字游戏");
setSize(400,400);
setResizable(false);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
//JTabbedPane
newContentPane=new JTabbedPane();//标签管理机器
newContentPane.setOpaque(true);
setContentPane(newContentPane);
JPanel gamePane=new JPanel(new BorderLayout());
newContentPane.add("游戏",gamePane);
JPanel blockPane=new JPanel(new GridLayout(3,3));//用来摆放 字方块
gamePane.add(blockPane,BorderLayout.CENTER);//将 方块板 加到 游戏板 的中间
//JButton[][] block=new JButton[3][3];
String[] wordOnBlock={"我","爱","中","华","此","志","不","渝",""};
int m=0;
for(int i=0;i<=2;i++)//i控制行
{
for(int j=0;j<=2;j++)//j控制列
{
block[i][j]=new JButton();
block[i][j].addActionListener(this);
block[i][j].setText(wordOnBlock[m]);
m++;
blockPane.add(block[i][j]);
}
}//循环嵌套结束
JPanel buttonPane=new JPanel(new FlowLayout(FlowLayout.RIGHT));
gamePane.add(buttonPane,BorderLayout.SOUTH);
JButton[] buttonBlow=new JButton[2];
for(int i=0;i<=1;i++)
{
buttonBlow[i]=new JButton();
buttonPane.add(buttonBlow[i]);
buttonBlow[i].addActionListener(this);
}
buttonBlow[0].setText("Begin");
buttonBlow[1].setText("Stop");
//第二个标签:关于游戏
JPanel aboutTheGame=new JPanel();
Font ft=new Font("黑体",Font.BOLD,18);
JLabel aboutGame1=new JLabel("<html><br>这是本人的第一个游戏;</br>" +
"<br>将打乱的字排列成:<br>" +
"<br>我爱中华,此志不渝!<br>" +
"<br>难免有一些漏洞和不足之处,望海涵指正!!</br>" +
"<br>山大软件:黄绍莽</br>" +
"<br>油箱:yuletianxia750@126.com</br>" +
"<br>QQ:290868594</br></html>",JLabel.CENTER);
aboutGame1.setFont(ft);
aboutTheGame.add(aboutGame1,BorderLayout.CENTER);
newContentPane.addTab("关于我们", aboutTheGame);
}//钩爪方法结束
public void randomBlock()
{
int x=(int)Math.random()*3;
int y=(int)Math.random()*3;
String str=null;
for(int i=0;i<=2;i++)
{
for(int j=0;i<=2;i++)
{
x=(int)(Math.random()*3);
y=(int)(Math.random()*3);
str=block[i][j].getText().toString();
block[i][j].setText(block[x][y].getText().toString());
block[x][y].setText(str);
}
}
this.validate();
newContentPane.validate();
}
public void start()
{
//int shijian=(int)
///timeneed =time.purge();
time.schedule(new TimerTask(){public void run()
{
timeneed++;
cancel();
start();
}
},1000);
///TimerTask timeneeded=new TimerTask();
//timeneeded.
}
public void showResult()
{
String str="";
String finished="你没有完成游戏!!";
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
str=str+block[i][j].getText().toString();
}
}
if(str.equals("我爱中华此志不渝"))
{finished="恭喜你!你已经成功地完成了游戏要求!!太棒了!!";}
JOptionPane.showMessageDialog(null, "<html><br>"
+finished+"</br>"+
"<br>你所用的时间为:</br>"+timeneed
+"秒</br></html>", "成绩", JOptionPane.OK_OPTION);
timeneed=0;
}
public static void main(String[] argss)
{
ReplaceBlockGame shiyan=new ReplaceBlockGame();
//shiyan.pack();
shiyan.setVisible(true);
}
//完成actionListener************
public void actionPerformed(ActionEvent e)
{
//if(e instanceof )
if(e.getActionCommand().equals("Begin"))
{
//System.out.print("");
randomBlock();
start();
}
else if(e.getActionCommand().equals("Stop"))
{
showResult();
randomBlock();
}
else
{
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
if(e.getSource()==block[i][j])
{
if(i+1<=2&&block[i+1][j].getText().toString().equals(""))
{
block[i+1][j].setText(block[i][j].getText().toString());
block[i][j].setText("");
}
else if(i-1>=0&&block[i-1][j].getText().toString().equals(""))
{
block[i-1][j].setText(block[i][j].getText().toString());
block[i][j].setText("");
}
else if(j-1>=0&&block[i][j-1].getText().toString().equals(""))
{
block[i][j-1].setText(block[i][j].getText().toString());
block[i][j].setText("");
}
else if(j+1<=2&&block[i][j+1].getText().toString().equals(""))
{
block[i][j+1].setText(block[i][j].getText().toString());
block[i][j].setText("");
}
else
{}
}
}
}
}
}
//******************************
}//雷结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -