熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java寫的打字游戲

2022-06-13   來源: Java核心技術 

  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();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26686.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.