如图:
排列成这样以后,怎么判断已经完成游戏呢?
1楼是程序代码。
排列成这样以后,怎么判断已经完成游戏呢?
1楼是程序代码。
解决方案 »
- java打印机获得当前打印机
- 注册时正则表达式编写
- 用NIO异步通信的时候,怎么知道是否异常?
- 来来来,编程累了的兄弟们,来欣赏一下alienbat(亡灵法师)这个跳梁小丑和它那一堆马甲的精彩表演吧,放松一下
- Android Studio 安装问题
- 调用url.openConnection().getInputStream()下载文件时报java.io.IOException
- 上传视频技术架构?
- 刚学java.....关于继承的问题...
- 一个小程序,不能理解其输出结果
- J builder5.0中出现neither class nor source found for java.lang.Object
- 关于native方法?
- 关于String对象在内存中的存活 (与其他问题不同)
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;public class PuzzleFrame extends JFrame implements ActionListener
{
public int a=1;
PuzzlePad puzzlePad;
JLabel label;
JButton 开始,暂停,停止;
JMenuBar bar;
JMenu fileMenu;
JMenuItem 排行榜,退出;
Container con=null;
public Time time;
public Timer t;
public PuzzleFrame(String title)
{
super(title);
time = new Time();
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
time.timePass();
label.setText(time.getTime());
}
};
t = new Timer(1000, listener);
label = new JLabel(time.getTime(), JLabel.CENTER);
label.setFont(new Font("仿宋", Font.BOLD, 25));
bar=new JMenuBar();
fileMenu=new JMenu("游戏");
排行榜=new JMenuItem("排行榜");
退出=new JMenuItem("退出");
fileMenu.add(排行榜);
fileMenu.add(退出);
bar.add(fileMenu);
setJMenuBar(bar);
排行榜.addActionListener(this);
退出.addActionListener(this);
开始=new JButton("开始@继续");
暂停=new JButton("暂停");
停止=new JButton("停止");
开始.addActionListener(this);
暂停.addActionListener(this);
暂停.setEnabled(false);
停止.addActionListener(this);
停止.setEnabled(false);
puzzlePad=new PuzzlePad(3,3,50,50);
con=getContentPane();//容器
con.add(puzzlePad,BorderLayout.CENTER);
JPanel pNorth=new JPanel();
pNorth.add(开始);
pNorth.add(暂停);
pNorth.add(停止);
pNorth.add(label);
con.add(pNorth,BorderLayout.NORTH);
con.validate();//验证此容器及其所有子组件。
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
validate();
}
class Time
{
private int hour, min, sec; public Time()
{
hour = 0;
min = 0;
sec = 0;
} public void timePass()
{
sec++;
if(sec == 60)
{
sec = 0;
min++;
if(min == 60)
{
min = 0;
hour++;
if(hour == 24)
{
hour = 0;
}
}
}
}
public String getTime()
{
return String.format("%1$02d : %2$02d : %3$02d", hour, min, sec);
} public void reset()
{
hour = 0;
min = 0;
sec = 0;
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==开始)
{
if (a==1)
{
puzzlePad.随机排列数字();
t.start();
开始.setEnabled(false);
暂停.setEnabled(true);
停止.setEnabled(true);
}
if (a==2)
{
t.start();
开始.setEnabled(false);
暂停.setEnabled(true);
停止.setEnabled(true);
}
}
else if(e.getSource()==排行榜)
{
JOptionPane.showMessageDialog(null,"数据库不存在!");
}
else if(e.getSource()==退出)
{
dispose();
}
else if(e.getSource()==暂停)
{
t.stop();
a=2;
开始.setEnabled(true);
暂停.setEnabled(false);
//JOptionPane.showMessageDialog(null,"失去焦点!");
}
else if(e.getSource()==停止)
{
time.reset();
label.setText(time.getTime());
a=1;
开始.setEnabled(true);
暂停.setEnabled(false);
停止.setEnabled(false);
}
}
public static void main(String args[])
{
PuzzleFrame MoBan = new PuzzleFrame("魔板游戏");
MoBan.setBounds(300,100,440,360);
MoBan.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Vector;
public class PuzzlePad extends JPanel implements ActionListener
{
int rows ,colums;
int width,height;
SquarePoint point[][];
JButton block[][];
Vector vector;
int leftX=50,leftY=50;
public PuzzlePad(int r,int c,int w,int h)
{
setLayout(null);
vector=new Vector();
rows=r;
colums=c;
width=w;
height=h;
block=new JButton[rows][colums];
point=new SquarePoint[rows][colums];
for(int i=0;i<rows;i++)
{
for(int j=0;j<colums;j++)
{
if((i==rows-1)&&(j==colums-1))
{
}
else
{
block[i][j]=new JButton();
block[i][j].setSize(width,height);
block[i][j].addActionListener(this);
vector.add(block[i][j]);
}
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<colums;j++)
{
point[i][j]=new SquarePoint(50+j*width,50+i*height);
if((i==rows-1)&&(j==colums-1))
{
}
else
{
point[i][j].setBlock(block[i][j],this);
}
}
}
} public void actionPerformed(ActionEvent e)
{
JButton button=(JButton)e.getSource();
int x=button.getBounds().x;
int y=button.getBounds().y;
int m=0,n=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<colums;j++)
{
if(point[i][j].getX()==x&&point[i][j].getY()==y)
{
m=i;
n=j;
}
}
}
int 上=Math.max(0,m-1);
int 下=Math.min(rows-1,m+1);
int 左=Math.max(0,n-1);
int 右=Math.min(colums-1,n+1);
if(point[上][n].get有方块()==false)
{
point[上][n].setBlock(button,this);
point[m][n].set有方块(false);
}
else if(point[下][n].get有方块()==false)
{
point[下][n].setBlock(button,this);
point[m][n].set有方块(false);
}
else if(point[m][左].get有方块()==false)
{
point[m][左].setBlock(button,this);
point[m][n].set有方块(false);
}
else if(point[m][右].get有方块()==false)
{
point[m][右].setBlock(button,this);
point[m][n].set有方块(false);
}
}
public void 随机排列数字()
{
vector.removeAllElements();
for(int i=0;i<rows;i++)
{
for(int j=0;j<colums;j++)
{
if((i==rows-1)&&(j==colums-1))
{
}
else
{
vector.add(block[i][j]);
}
}
}
int i=1;
while(vector.size()>0)
{
int n=(int)(Math.random()*vector.size());
JButton b=(JButton)vector.elementAt(n);
b.setText(""+i);
b.setIcon(null);
b.setBorder(BorderFactory.createLineBorder(Color.blue,1));
vector.remove(n);
i++;
}
repaint();
}
public void paintComponent(Graphics g)//绘制背景
{
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(leftX,leftY,colums*width,rows*height);
}
}
这是SquarePoint类import java.awt.*;
import javax.swing.JButton;public class SquarePoint
{
int x,y;
boolean 有方块;
JButton block=null;
Container con=null;
public SquarePoint(int x,int y)
{
this.x=x;
this.y=y;
}
public boolean get有方块()
{
return 有方块;
}
public void set有方块(boolean boo)
{
有方块=boo;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setBlock(JButton block,Container con)
{
this.con=con;
this.block=block;
con.add(block);
block.setLocation(x,y);
有方块=true;
con.validate();
}
public JButton getBlock()
{
return block;
}
}
如果正确结束游戏有多种结果的话按照每种可能去判断了
不少啊,time的class 在PuzzleFrame里定义了