Title:小游戏 *
*
Description:将方块摆放成“崛起中华,此志不渝”
Copyright:Copyright(c)2010
*/
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.util.Timer;
importjava.util.TimerTask;
publicclassp14extendsJFrameimplementsActionListener
{//类开始
//实例变量
JButton[][]block=newJButton[3][3];
JTabbedPanenewContentPane;
Timertime=newTimer();
inttimeneed=0;
//钩爪方法
publicp14()
{
setTitle("排字游戏作者:xxxx");
setSize(400,458);
setResizable(false);
Dimensionscrsize=Toolkit.getDefaultToolkit().getScreenSize();
intscrw=scrsize.width;
intscrh=scrsize.height;
this.setBounds(((scrw-this.getWidth())/2),
((scrh-this.getHeight())/2),this.getWidth(),this.getHeight());
addWindowListener(
newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
);
//JTabbedPane
newContentPane=newJTabbedPane();//标签管理机器
newContentPane.setOpaque(true);
setContentPane(newContentPane);
JPanelgamePane=newJPanel(newBorderLayout());
newContentPane.add("游戏",gamePane);
JPanelblockPane=newJPanel(newGridLayout(3,3));//用来摆放字方块
gamePane.add(blockPane,BorderLayout.CENTER);//将方块板加到游戏板的中间
//JButton[][]block=newJButton[3][3];
String[]wordOnBlock={"崛","起","中","华","此","志","不","渝",""};
intm=0;
for(inti=0;i<=2;i++)//i控制行
{
for(intj=0;j<=2;j++)//j控制列
{
block[i][j]=newJButton();
block[i][j].setFont(newFont("宋体",Font.BOLD,16));
block[i][j].addActionListener(this);
block[i][j].setText(wordOnBlock[m]);
m++;
blockPane.add(block[i][j]);
}
}//循环嵌套结束
JPanelbuttonPane=newJPanel(newFlowLayout(FlowLayout.RIGHT));
gamePane.add(buttonPane,BorderLayout.SOUTH);
JButton[]buttonBlow=newJButton[2];
for(inti=0;i<=1;i++)
{
buttonBlow[i]=newJButton();
buttonPane.add(buttonBlow[i]);
buttonBlow[i].addActionListener(this);
}
buttonBlow[0].setText("开始");
buttonBlow[1].setText("结束");
//第二个标签:关于游戏
JPanelaboutTheGame=newJPanel();
Fontft=newFont("隶书",Font.BOLD,16);
JLabelaboutGame1=newJLabel
("
这是本人的第一个游戏,
"+
"将打乱的字排列成:
"+
"崛起中华,此志不渝。
"+
"难免有一些漏洞和不足之处,望海涵指正!
"+
"
,JLabel.CENTER);
aboutGame1.setFont(ft);
aboutTheGame.add(aboutGame1,BorderLayout.CENTER);
newContentPane.addTab("关于我们",aboutTheGame);
}//构造方法结束
publicvoidrandomBlock()
{
intx=(int)Math.random()*3;
inty=(int)Math.random()*3;
Stringstr=null;
for(inti=0;i<=2;i++)
{
for(intj=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();
}
publicvoidstart()
{
//intshijian=(int)
///timeneed=time.purge();
time.schedule(newTimerTask(){publicvoidrun()
{
timeneed++;
cancel();
start();
}
},1000);
///TimerTasktimeneeded=newTimerTask();
//timeneeded.
}
publicvoidshowResult()
{
Stringstr="";
Stringfinished="你没有完成游戏!";
for(inti=0;i<=2;i++)
{
for(intj=0;j<=2;j++)
{
str=str+block[i][j].getText().toString();
}
}
if(str.equals("崛起中华此志不渝"))
{f