📄 j_game.java
字号:
//package edu.qhj;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class J_Game extends JFrame{
private static final long serialVersionUID = 5243750595170293660L;
private myListener listener=new myListener();
public int x=20,y=20,p1=20,p2=20;
private String time="10:00";
boolean flag=true;
private JButton [][]button;
private JPanel pane;
private JPanel panel;
private JLabel timelabel;
private JButton btnstart;
private JButton btnpause;
private JButton btnrestart;
private JLabel label;
public J_Game(){
creatComponent();
this.setResizable(false);
this.setBounds(200, 200, 520, 400);
this.setForeground(Color.blue);
// this.setSize(400,400);
this.setTitle("Sorting Grame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void creatComponent(){
button=new JButton[4][4];
panel=new JPanel();
pane=new JPanel();
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
JSplitPane spane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
spane.setDividerSize(0);
spane.setDividerLocation(400);
timelabel=new JLabel("10:00");
// pane.setLocation(200, 200);
btnstart=new JButton(" 开始 ");
btnpause=new JButton(" 暂停 ");
btnrestart=new JButton("重新开始");
label=new JLabel(" 时间:");
// btnstart.setSize(80, 20);
// btnpause.setLocation(460, 300);
pane.setLayout(new GridLayout(3,1));
pane2.setLayout(new GridLayout(1,1));
pane2.add(label);
pane2.add(timelabel);
pane1.add(btnstart);
pane1.add(btnpause);
pane1.add(btnrestart);
btnstart.addActionListener(listener);
btnstart.addActionListener(listener);
pane.add(pane2);
pane.add(pane1);
this.setLayout(new BorderLayout(5,5));
panel.setLayout(new GridLayout(4,4));
// panel.setBounds(0, 0, 400, 400);
spane.setLeftComponent(panel);
spane.setRightComponent(pane);
ArrayList clist=Rand();
int k=0;
for(int i=0;i<4;i++){
for (int j = 0; j<4; j++) {
if((Integer)clist.get(k)==0){
k++;
button[j][i]=new JButton(" ");
}
else{
button[j][i]=new JButton(String.valueOf(clist.get(k++)));
}
button[j][i].setForeground(Color.blue);
button[j][i].addActionListener(listener);
panel.add(button[j][i]);
}
}
this.add(spane);
}
public ArrayList Rand(){
ArrayList<Integer> ranlist=new ArrayList<Integer>();
int r=0,i;
for(i=0;i<1000;i++){
r=(int)(Math.random()*16);
if(!ranlist.contains(r)){
ranlist.add(r);
}
}
for(i=0;i<ranlist.size();i++)
System.out.println (ranlist.get(i));
return ranlist;
}
class myListener implements ActionListener{
JButton tempbutton=null;
int i=0,j=0;
JButton ev=null;
String text=null;
public myListener(){
}
public void actionPerformed(ActionEvent e) {
int s=1;
Contral contral=new Contral();
ev=(JButton)e.getSource();
i=(ev.getLocation().x)/(ev.getBounds().height);
j=(ev.getLocation().y+27)/(ev.getBounds().width);
if(button[i][j].getText()==" ")
{
x=i;
y=j;
}
else {
p1=i;
p2=j;
}
if((x-1)==p1||(x+1==p1)||(y-1==p2)||(y+1==p2)||(p1-1==x)||(p1+1==x)
||(p2-1==y)||(p2+1==y))
if(((p1-1==x)&&(p2-1==y))||((x-1==p1)&&(y-1==p2))||
((p1+1==x)&&(p2-1==y))||((x+1==p1)&&(y-1==p2))
||((p1+1==x)&&(p2+2==y))||((x-1==p1)&&(y-2==p2))
||((p1+1==x)&&(p2-2==y))||((x-1==p1)&&(y+2==p2))){}
else
{
button[x][y].setText(button[p1][p2].getText());
button[p1][p2].setText(" ");
System.out.println("x="+x);
System.out.println("y="+y);
x=y=p1=p2=20;
}
for( int t=0;t<4;t++){
for(int k=0;k<4;k++){
if(button[k][t].getText().equals(new Integer(s))||button[k][t].getText().equals(new Integer((s)-1))){
s++;
}
System.out.println(button[k][t].getText());
}
}
if(s>=16)JOptionPane.showMessageDialog(null,"恭喜你,过关了");
System.out.println("button"+button[i][j].getText());
System.out.println("i="+i);
System.out.println("j="+j);
System.out.println("ev.getLocation().x="+ev.getLocation().x);
System.out.println("ev.getLocation().y ="+ ev.getLocation().y);
System.out.println("ev.getBounds().height="+ev.getBounds().height);
System.out.println("ev.getBounds()width="+ev.getBounds().width);
if(ev.equals(btnstart))contral.start();
if(ev.equals(btnrestart)){
contral.interrupt();
}
}
}
class Contral extends Thread{
SimpleDateFormat format=new SimpleDateFormat("mm:ss");
String str=": ";
Calendar start=Calendar.getInstance();
int sss=start.get(Calendar.SECOND)+600;
int mmm=start.get(Calendar.MINUTE);
int ss,mm;
int s,m;
public void run() {
flag=true;
while(flag){
try {
ss=Calendar.getInstance().get(Calendar.SECOND);
mm=Calendar.getInstance().get(Calendar.MINUTE);
s=((sss+60*mmm)-(ss+60*mm))%60;
m=((sss+60*mmm)-(ss+60*mm))/60;
if(s<10)str=": 0";
else str=":";
timelabel.setText(m+str+s);
sleep(1000);
} catch (Exception e) {
flag=false;
}
}
}
public void destroy() {
time="10:00";
timelabel.setText(time);
}
}
public static void main(String args[]) {
new J_Game();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -