📄 puzzle.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class Pintu extends JFrame implements ActionListener,ItemListener
{
public static int m=9;
JFrame f;
JPanel p1;
LayTime UseTime;
JTable Table=new JTable(m);
JButton start;
JButton end;
MenuBar mb;
Menu m_games,m_help;
MenuItem item1,item2,item6,item7,item8;
CheckboxMenuItem item3,item4,item5;
Container con;
boolean already=false;
int icon_size=100,Width=3,Height=3;
Logo abo_logo; //关于拼图
Help help1; //帮助文件
public Pintu()
{
f=new JFrame("拼图游戏");
p1=new JPanel();
UseTime = new LayTime();
con=f.getContentPane(); //获得一个Container容器对象
con.setLayout(null);
start=new JButton("开始");
end=new JButton("结束");
abo_logo=new Logo(f);
help1=new Help(f);
menuCreate();
abo_logo.str();
f.setSize(600,600);
f.setVisible(true);
f.setResizable(false);
con.add(Table);
p1.setBackground(Color.gray);
p1.setBounds(80,40,400,50);
con.add(p1);
Font msgFnt=new Font("Dialog",Font.ITALIC,25);
UseTime.setFont(msgFnt);
p1.add(UseTime);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ System.exit(0); } });
Table.setBounds(80,100,400,400);
Table.hideChess(" ",m);
start.setFont(msgFnt);
end.setFont(msgFnt);
start.addActionListener(this);
end.addActionListener(this);
con.add(start);
con.add(end);
start.setBounds(65,510,100,30);
end.setBounds(400,510,100,30);
abo_logo.setVisible(false);
}
public void menuCreate()
{
MenuBar mb=new MenuBar();
Menu m_game=new Menu("游戏");
MenuItem item1=new MenuItem("开局",new MenuShortcut(KeyEvent.VK_S));
MenuItem item2=new MenuItem("重来",new MenuShortcut(KeyEvent.VK_R));
item3=new CheckboxMenuItem("低级");
item4=new CheckboxMenuItem("高级");
MenuItem item6=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_Q));
Menu m_help=new Menu("帮助");
MenuItem item7=new MenuItem("关于",new MenuShortcut(KeyEvent.VK_A));
MenuItem item8=new MenuItem("帮助说明",new MenuShortcut(KeyEvent.VK_I));
f.setMenuBar(mb);
mb.add(m_game);
m_game.add(item1);
m_game.addSeparator();
m_game.add(item2);
m_game.addSeparator();
m_game.add(item3);
m_game.add(item4);
m_game.addSeparator();
m_game.add(item6);
mb.add(m_help);
m_help.add(item7);
m_help.addSeparator();
m_help.add(item8);
item3.setEnabled(true);
item4.setEnabled(true);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addItemListener(this);
item4.addItemListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String temp=e.getActionCommand();
if(temp.equals("开始"))
{
Table.startChess(m);
UseTime.stopShow();
UseTime.setText("时间:0");
UseTime.start();
already=true;
}
if(temp.equals("开局"))
{
Table.startChess(m);
UseTime.stopShow();
UseTime.setText("时间:0");
UseTime.start();
already=true;
}
if(temp.equals("结束"))
{
System.exit(0);
}
if(temp.equals("重来"))
{
UseTime.stopShow();
UseTime.setText("时间:0");
already=true;
item3.setEnabled(true);
item4.setEnabled(true);
}
if(temp.equals("退出"))
{
System.exit(0);
}
if(temp.equals("关于"))
{
abo_logo.setLocation(f.getX()+f.getSize().width/2-200,f.getY()+f.getSize().height/2-140);
abo_logo.setModal(true);
abo_logo.str();
}
if(temp.equals("帮助说明"))
{
help1.setLocation(f.getX(),f.getY()+80);
help1.setVisible(true);
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==item3)
{
m=9;
Table=new JTable(m);
UseTime.stopShow();
UseTime.setText("时间:0");
item3.setEnabled(true);
item4.setEnabled(false);
new Pintu();
f.setVisible(true);
f.setSize(600,600);
}
if(e.getSource()==item4)
{
m=16;
Table=new JTable(m);
UseTime.stopShow();
UseTime.setText("时间:0");
item3.setEnabled(false);
item4.setEnabled(true);
new Pintu();
f.setVisible(true);
f.setSize(600,600);
}
}
class JTable extends JPanel implements ActionListener
{
JButton keyboard[];
int fx[]=new int[4];
int opfx[];
JTable(int m)
{
keyboard=new JButton[m];
int k=(int)Math.sqrt(m);
opfx=new int[k];
fx[0]=-k;
fx[1]=-1;
fx[2]=1;
fx[3]=k;
for(int i=1;i<=k;i++)
{
opfx[i-1]=k-i;
}
int i,t,x;
int n=1;
x=k;
setLayout(new GridLayout(x,x,0,0));
for(i=0;i<m-1;i++,n++)
{
keyboard[i]=new JButton(String.valueOf(n));
keyboard[i].setBackground(Color.gray);
keyboard[i].addActionListener(this);
add(keyboard[i]);
}
keyboard[m-1]=new JButton(" ");
keyboard[m-1].setBackground(Color.gray);
keyboard[m-1].addActionListener(this);
add(keyboard[m-1]);
}
private void delay(int times)
{
int i;
for(i=0;i<times;i++);
}
private int whichChess(String st,int m)//判断点击的哪个控件
{
int i;
String ss;
for(i=0;i<m;i++)
{
ss=keyboard[i].getText();
if(keyboard[i].getText().equals(st)) return i;
}
return -1;
}
private boolean rightChess(int m)//判断是否排序正确
{
int i;
int n=1;
for(i=0;i<m-1;i++,n++)
{
if(!keyboard[i].getText().equals(String.valueOf(n))) return false;
}
return true;
}
private boolean hideChess(String st,int m)//把点击的控件隐藏
{
int i;
i=whichChess(st,m);
if(i!=-1) {keyboard[i].setVisible(false);return true;}
else return false;
}
private boolean showChess(String st,int m)
{
int i;
i=whichChess(st,m);
if(i!=-1) {keyboard[i].setVisible(true);return true;}
else return false;
}
private boolean rightLocation(int Location,int m)
{
if((Location>=0)&&(Location<m)) return true;
else return false;
}
private boolean moveChess(String st,int m)//移动控件
{
int location=whichChess(st,m);
if(location==-1) return false;
int i,newlocation;
for(i=0;i<4;i++)
{
newlocation=location+fx[i];
if(rightMove(location,newlocation,m))
{
if(keyboard[newlocation].getText().equals(" "))//判断哪个是空位
{
keyboard[newlocation].setText(keyboard[location].getText());
keyboard[location].setText(" ");
keyboard[location].setVisible(false);
keyboard[newlocation].setVisible(true);
return true;
}
}
}
return false;
}
private void test()
{
int i;
for(i=0;i<100;i++)
{
delay(10000000);
keyboard[1].setVisible(true);
delay(10000000);
keyboard[1].setVisible(false);
}
}
private boolean rightMove(int location,int newlocation,int m)//判断所点击的控件四周是否有空位
{
int k=(int)Math.sqrt(m);
if(rightLocation(newlocation,m)&&((location/k==newlocation/k)||(location%k==newlocation%k)))
return true;
else return false;
}
public void actionPerformed(ActionEvent e)
{
JButton temp=(JButton)e.getSource();
String str=temp.getText();
if(already)
{
moveChess(str,m);
if(rightChess(m))
{
UseTime.stopShow();
JOptionPane.showMessageDialog(null,"You are win");
already=false;
}
}
}
void startChess(int m)
{
int k=(int)Math.sqrt(m);
int i,location,newlocation,way,time,oldway;
int n=1;
for(i=0;i<m-1;i++,n++)
{
keyboard[i].setText(String.valueOf(n));
keyboard[i].setVisible(true);
}
keyboard[m-1].setText(" ");
keyboard[m-1].setVisible(false);
location=m-1;
oldway=-1;
delay(200000);
time=60+(int)(Math.random()*70);
for(i=0;i<time;i++)
{
do
{
way=(int)(k*Math.random());
newlocation=location+fx[way];
}
while(!rightMove(location,newlocation,m)||way==oldway);
oldway=opfx[way];
keyboard[location].setText(keyboard[newlocation].getText());
keyboard[location].setVisible(true);
keyboard[newlocation].setText(" ");
keyboard[newlocation].setVisible(false);
location=newlocation;
delay(10000);
}
}
}
}
public class Puzzle
{
public static void main(String args[])
{
new Pintu();
}
}
class Logo extends Dialog
{
Icon logo1;
JLabel jl;
public Logo(JFrame f)
{
super(f,"拼图V1.0",false);
logo1=new ImageIcon("image/001.gif");
jl=new JLabel(logo1);
add(jl);
setSize(300,200);
setResizable(false);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent aa){setVisible(false);}});
}
public void str()
{
setVisible(true);
}
public void exit()
{
System.exit(0);
}
}
class Help extends Dialog
{
public Help(JFrame f)
{
super(f,"拼图帮助",false);
TextArea ta=new TextArea(10,40);
add(ta);
Font font1=new Font("Dialog",Font.PLAIN,16);
ta.setFont(font1);
ta.setText("这是一个功能很强的拼图游戏\n\n帮助说明如下:\n\n 1.点击开始,同时时间计时开始\n\n 2.用左键点击移动图片,使图片移至相应位置\n\n 3.游戏设置了低,中,高级游戏 \n\n 4.拼图是一个智力和运气并存的游戏当你觉得\n\n 无法进行下去时,可以按重来,再玩\n\n 5.完成一次游戏后,记下所用时间按退出可以\n\n 结束游戏 " );
ta.setEditable(false);
setSize(400,300);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){setVisible(false);}});
}
}
class LayTime extends JLabel implements Runnable
{
private volatile Thread t;
LayTime()
{
super();
setText("时间:0");
}
public void start()
{
t=new Thread(this,"showtime");
t.start();
}
public void run()
{
int usedtime=0;
Thread thisThread = Thread.currentThread();
while(t==thisThread)
{
try
{
usedtime++;
setText("时间:"+Integer.toString(usedtime));
t.sleep(1000);
}
catch(InterruptedException e)
{
setText("无法准确显示时间");
}
}
}
public void stopShow()
{
t=null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -