📄 numtest.java
字号:
/**
*数字拼图游戏
*
*@author 董利伟
*@version 1.0 2006/07/15
*/
import java.awt.*;
import java.awt.event.*;
public class NumTest extends Frame
{
/**
*按钮数组
*/
Button[] bt = new Button[9];
/**
*按钮比较
*用于记录最近两次按下的按钮
*/
int[] BNum = new int[2];
/**
*计数器
*/
int n = 0;
/**
*初始化和转换工具
*/
NumUtil nu;
public NumTest()
{
nu = new NumUtil();
nu.createNum();
/**
*创建按钮
*/
for (int i = 0; i < 9; i++)
{
String str = "";
if(i < 8)
{
str = String.valueOf(nu.num[i]);
}
bt[i] = new Button(str);
bt[i].setFont(new Font("宋体",Font.BOLD,38));
bt[i].setForeground(Color.blue);
bt[i].setBackground(Color.white);
}
this.setLayout(new GridLayout(3,3,3,3));
for (int i = 0; i < 9; i++)
{
this.add(bt[i]);
}
this.setSize(300,300);
/**
*为每个按钮添加事件
*/
for (int i = 0; i < 9; i++)
{
bt[i].addActionListener(new Test());
}
Toolkit tk = this.getToolkit();
Dimension dms = tk.getScreenSize();
int fx = (int)(dms.getWidth()) / 2 - this.getWidth() / 2;
int fy = (int)(dms.getHeight()) / 2 - this.getHeight() /2;
this.setLocation(fx,fy);
this.setTitle("数字拼图游戏");
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setVisible(true);
}
class Test implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < 9; i++)
{
Button bb = (Button)e.getSource();
if(bb == bt[i])
{
n++;
int t = n % 2;
BNum[t] = i + 1;
//System.out.println(i);
boolean tt = nu.safe(BNum[0],BNum[1]);
if(tt)
{
int a = BNum[0];
int b = BNum[1];
String str = bt[a-1].getLabel();
bt[a-1].setLabel(bt[b-1].getLabel());
bt[b-1].setLabel(str);
}
}
}
}
}
public static void main(String [] args)
{
new NumTest();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -