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)
评论0