С??ϷԴ????

核心提示我有java的,你可以看看:一个拼图import java.lang.Math.*;import java.awt.event.*;import java.awt.*;import javax.swing.*;class MainFrame

我有java的,你可以看看:一个拼图

import java.lang.Math.*;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class Mainframe extends Jframe implements ActionListener{ //定义整个框架

private JButton[] jb = new JButton[8];

private JButton jbs = new JButton("开 局");

private JButton jbres = new JButton("重新开始");

private JPanel jp1 = new JPanel();

private JPanel jp2 = new JPanel();

private int[] n = new int[9];

private int[] n1 = new int[9];

private int position = 8,p,q;

private boolean bl,startbl=false;

private JLabel jl = new JLabel();

private int count = 0;

private JLabel jl1 = new JLabel(" "+Integer.toString(0));

public Mainframe(){ //框架的构造方法

int i;

for(int j = 0; j < n.length; j++){

n[j] = j;

n1[j] = n[j];

}

for(i = 0; i < jb.length; i++){ //给每个按钮赋相应的值,并注监听器

jb[i] = new JButton(Integer.toString(i+1));

jb[i].setFont(new Font("宋体",Font.BOLD,48));

jp2.add(jb[i]);

jb[i].addActionListener(this);

}

for(i = 0; i < n.length; i++){

if(n[i] == position)

jp2.add(jl);

else

jp2.add(jb[n[i]]);

}

jp2.setLayout(new GridLayout(3,3));//注册监听器

jbs.addActionListener(this);

jbres.addActionListener(this);

jp1.add(jbs);

jp1.add(jbres);

jp1.add(jl1);

jp1.setLayout(new FlowLayout()); //将jp1设置为流布局

setLayout(new BorderLayout()); //整体布局为边界布局

this.add("North",jp1);

this.add("Center",jp2);

this.setTitle("拼图游戏");

this.setBounds(100,100,300,350);

this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); //实现关闭按钮

this.setResizable(false);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){ //实现按钮的事件

if(e.getSource()==jbres){ // 重新开始按钮事件

for(int j = 0; j<n.length;j++)

n[j] = n1[j];

reShow();

startbl=true;

count = 0;

jl1.setText(" "+Integer.toString(0));

}

else if(e.getSource()==jbs) //开局按钮事件

this.Init();

else if(startbl){ //按钮1-8移动事件

for(int i = 0; i < jb.length; i++)

if(e.getSource() == jb[i]){

//System.out.println(i+1);

for(int a=0;a<n.length;a++){

if(n[a]==i)

p=a;

if(n[a]==position)

q=a;

}

}

if(p != 0 && p != 1 && p != 2)

if((p-3) == q)

swap(p,q);

if(p != 0 && p != 3 && p != 6)

if((p-1) == q)

swap(p,q);

if(p != 2 && p != 5 && p != 8)

if((p+1) == q)

swap(p,q);

if(p != 6 && p != 7 && p != 8)

if((p+3) == q)

swap(p,q);

}

}

public void swap(int x,int y){ //按钮1-8与空白交换

int z;

z = n[x];

n[x] = n[y];

n[y]=z;

jl1.setText(" "+Integer.toString(++count));

reShow();

win();

}

public void Init(){ //随机产生游戏界面

int i=0,j,x;

boolean bl ;

while(i<9){

bl = true;

x=(int)(Math.random()*9);

for(j=0;j<i;j++)

if(n[j] == x)

bl=false;

if(bl){

n [i++] = x;

n1[i-1] = x;

}

}

reShow();

startbl=true;

count = 0;

jl1.setText(" "+Integer.toString(0));

}

public void reShow(){ //对游戏界面的重写

for(int i = 0; i < n.length; i++){

if(n[i] == position)

jp2.add(jl);

else

jp2.add(jb[n[i]]);

}

jp2.revalidate();

}

public void win(){ //判断是否成功

boolean winbl=true;

for(int i=0;i<n.length;i++)

if(n[i]!=i)

winbl=false;

if(winbl){

JOptionPane.showMessageDialog(this,"祝贺你,你成功了! "+"你用了"+Integer.toString(count)+"步","",JOptionPane.INFORMATION_MESSAGE);

startbl=false;

}

}

}

public class Collage { // 主函数类

public static void main(String[] args){

new Mainframe();

}

}

自已以前编的,不是很好,你就参考参考吧

 
友情链接
鄂ICP备19019357号-22