import javaxswing*;
import javaawt*;
import javaawtevent*;
//public class Test {
// public static void main(String[] args) {
// Dazi dazi = new Dazi();
// dazisetFrame();
// }
//}
public class Dazi implements KeyListenerRunnable{
JFrame jf;// 主框架;
JMenuBar mainBar;
Thread t = null;
JPanel jp;// 三位数字
JPanel jp;// 级数线宽面板
JPanel jp; // 得分的线宽面板
JLabel jtf;// 显示得分信息
JMenu fileMenu;
JMenu helpMenu;
JLabel jl = new JLabel();
JLabel jl = new JLabel();
JLabel jl = new JLabel();
JLabel jl = new JLabel();
int x[]={};
int y[]={};
String[][] str = new String[][];
String tempStr= ;
int flagCount = ;
int score = ;
int jibie = ;
boolean startFlag = false;
public String getstring() {
int x = (int) ( * Mathrandom());
int y = (int) ( * Mathrandom());
int z = (int) ( * Mathrandom());
String str = + x + y + z;
return str;
}
public void setFrame() {
jf = new JFrame(dz);
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
jfsetBounds( );
jfsetLayout(null);
setStr();
jfaddKeyListener(this);
jfsetVisible(true);
// jp = new JPanel();
// jl = new JLabel();
// jl = new JLabel();
// jl = new JLabel();
showMenu(); // 设置菜单
showTool(); // 设置工具条
showScore();
jfsetResizable(false);
//xunhuan();
// jpsetBounds( );
}
private void setStr() {
String str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
}
public void xunHuan(){
while(y[]<=){
try {
Threadsleep();
} catch (InterruptedException e) {
eprintStackTrace();
}
for(int i=;i<;i++){
y+=jibie;
}
if(!isLive()) {
if(getDisapperStr()equals()) {
String str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
y[] = ;
jlsetBounds(new Rectangle(x[] y[] ));
}else if(getDisapperStr()equals()) {
String str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
y[] = ;
jlsetBounds(new Rectangle(x[] y[] ));
}else if(getDisapperStr()equals()) {
String str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
y[] = ;
jlsetBounds(new Rectangle(x[] y[] ));
}else{
String str = getstring();
str[][] = str;
str[][] = + ;
jlsetText(str);
y[] = ;
jlsetBounds(new Rectangle(x[] y[] ));
}
//加分
score += ;
String scoreStr = 得分 + score;
jtfsetText(scoreStr);
}else {
if(isOver()) {
for(int i=; i<ylength; i++) {
y = ;
}
setStr();
JOptionPaneshowMessageDialog(jf 游戏结束! 提示
JOptionPaneINFORMATION_MESSAGE);
return;
}
jlsetBounds(new Rectangle(x[] y[] ));
jlsetBounds(new Rectangle(x[] y[] ));
jlsetBounds(new Rectangle(x[] y[] ));
jlsetBounds(new Rectangle(x[] y[] ));
}
jfadd(jl);
jfadd(jl);
jfadd(jl);
jfadd(jl);
}
}
/**
* 判断四个串是否存在
* @return 若存在 返回true 否则false
*/
private boolean isLive() {
boolean flag = true;
for(int i=; i<strlength; i++) {
if(!str[]equals()) {
flag = false;
}
}
return flag;
}
private String getDisapperStr() {
String str = ;
for(int i=; i<strlength; i++) {
if(!str[]equals()) {
str = str[];
break;
}
}
return str;
}
private boolean isOver() {
boolean overFalg = false;
for(int i=; i<ylength; i++) {
if(y >= ) {
overFalg = true;
}
}
return overFalg;
}
public void showMenu() {
mainBar = new JMenuBar();
fileMenu = new JMenu(游戏); // 设置菜单栏选项
helpMenu = new JMenu(帮助);// 设置菜单栏选项
// jfsetJMenuBar(mainBar);//将菜单栏添加到主框架里面
JMenuItem startItem = new JMenuItem(开始游戏(O) O); // 设置游戏里面的开始选项
JMenuItem exitItem = new JMenuItem(结束游戏(X) X);// 设置游戏里面的结束选项
startItemsetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_O
InputEventCTRL_MASK));// 设置开始选项的快捷键
startItemaddActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
startFlag = true;
//Systemoutprintln(begin);
}
}
);
exitItemsetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_X
InputEventCTRL_MASK));// 设置退出选项的快捷键
exitItemaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPaneshowMessageDialog(jf 你应经结束游戏了 提示
JOptionPaneINFORMATION_MESSAGE);
Systemexit();
}
});
JMenuItem aboutItem = new JMenuItem(关于(A) A);// 设置帮助里的关于选项
aboutItemsetAccelerator(KeyStrokegetKeyStroke(KeyEventVK_A
InputEventCTRL_MASK));// 设置关于选项的快捷键
aboutItemaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPaneshowMessageDialog(jf 这是一个简单的打字游戏 提示
JOptionPaneINFORMATION_MESSAGE);
}
});
mainBaradd(fileMenu);// 将游戏菜单添加到主菜单中
mainBaradd(helpMenu);// 将帮助菜单添加到主菜单中
fileMenuadd(startItem);// 将开始菜单添加到游戏菜单中
fileMenuadd(exitItem);// 将退出菜单添加到游戏菜单中
helpMenuadd(aboutItem);// 将关于菜单添加到帮助菜单中
jfsetJMenuBar(mainBar);
}
public void showTool() {
Container contentPane = jfgetContentPane();
JToolBar jtb = new JToolBar(); // 设置工具条
jtbsetSize( );
contentPaneadd(jtb BorderLayoutWEST);
mainBar = new JMenuBar();// 定义主菜单
JRadioButton[] buttons = new JRadioButton[];
jp = new JPanel();
jpsetSize( );
jpsetBorder(BorderFactorycreateTitledBorder(选择级数));
jpsetLayout(new GridLayout( ));
ButtonGroup bg = new ButtonGroup();
JRadioButton b = new JRadioButton( true);
baddItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(egetStateChange() == eSELECTED) {
setJibie();
}
}
}
);
bgadd(b);
jpadd(b);
JRadioButton b = new JRadioButton();
baddItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(egetStateChange() == eSELECTED) {
setJibie();
}
}
}
);
bgadd(b);
jpadd(b);
JRadioButton b = new JRadioButton();
baddItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(egetStateChange() == eSELECTED) {
setJibie();
}
}
}
);
bgadd(b);
jpadd(b);
JRadioButton b = new JRadioButton();
baddItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(egetStateChange() == eSELECTED) {
setJibie();
}
}
}
);
bgadd(b);
jpadd(b);
JRadioButton b = new JRadioButton();
baddItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(egetStateChange() == eSELECTED) {
setJibie();
}
}
}
);
bgadd(b);
jpadd(b);
// for (int i = ; i < buttonslength; i++) {
// buttons = new JRadioButton( + i);
// bgadd(buttons);
// jpadd(buttons);
// buttonsaddItemListener(new ItemListener() {
//
// public void itemStateChanged(ItemEvent e) {
// if(egetStateChange() == eSELECTED) {
// }
// }
// });
// }
jtbadd(jp);
}
public void showScore() {
jp = new JPanel();
jpsetBounds( );
jtf = new JLabel(得分:);
jpadd(jtf);
jfadd(jp);
jfsetVisible(true);
}
public void keyPressed(KeyEvent e) {
tempStr +=egetKeyChar();
flagCount ++;
if(flagCount == ) {
for(int i=; i<strlength; i++) {
if(tempStrequals(str[])) {
str[] = + (i+);
break;
}
}
flagCount = ;
tempStr = ;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
Dazi dazi = new Dazi();
dazisetFrame();
new Thread(dazi)start();
}
public void setJibie(int jibie) {
thisjibie = jibie * ;
}
public void run() {
while (true) {
try {
if(startFlag) {
xunHuan();
startFlag = false;
}
Threadsleep();
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}