2012-12-25 27 views
-2

可能重复:
Java hangman game with gui, problems with incrementing/decrementing numbers的Java Swing刽子手比赛赢/记不清不起作用,KeyListener的是越野车

所以问题是,我吸在摇摆,不知道如何使用keylistener,它开始多次听按键(第二游戏后记录2个键,第三游戏后记录3个键等等)。我不知道如何正确使用Event Dispatch Thread,或者我应该使用它,并且won/lost count错误地增加。此外,当我希望框架听取键盘输入时,我要求将其焦点放在窗口中,但当我不希望框架读取键盘输入时,我不知道如何解除它。欢迎任何帮助。这是我第二次提出这个问题,希望这次我已经让程序和问题更容易理解了。编辑:游戏现在可以工作,还可以添加缓冲写入器来写入日志文件。对不起,懒得修改一些代码的命名部分为英文,因为他们是立陶宛人。

import java.io.*; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.*; 

public class Hangman { 

    static int won = 0; 
    static int lost = 0; 
    static String key = ""; 
    static String word = null; 
    static int no = 0; 
    static StringBuffer toguess; 
    static KeyboardFocusManager manager; 
    static Runnable hangman; 
    static ArrayList<String> tried; 
    static int gamecount = 0; 

    @SuppressWarnings("resource") 
    public static void main(String[] args) throws IOException { 
     FileWriter filewriter = new FileWriter("rezultatai.txt", true); 
     final BufferedWriter writer = new BufferedWriter(filewriter); 
     final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     final Date date = new Date(); 
     hangman = new Runnable() { 

      public void run() { 
       final JFrame frame = new JFrame(); 
       frame.setLayout(new GridLayout(4, 1)); 
       final JLabel label = new JLabel(); 
       label.setFont(new Font("Serif", Font.BOLD, 48)); 
       label.setForeground(Color.GREEN); 
       label.setText("Kaledines kartuves"); 
       final JPanel panel1 = new JPanel(); 
       final JPanel panel2 = new JPanel(); 
       final JPanel panel3 = new JPanel(); 
       final JPanel panel4 = new JPanel(); 
       final JLabel label3 = new JLabel(); 
       panel1.add(label); 
       panel4.add(label3); 
       final JLabel label2 = new JLabel(); 
       frame.setVisible(true); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       final JButton button = new JButton("Pradeti"); 
       panel2.add(button); 
       frame.add(panel1); 
       frame.add(panel2); 
       frame.add(panel3); 
       frame.setSize(800, 600); 
       button.addActionListener(new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent e) { 
         gamecount = gamecount + 1; 
         no = 0; 
         label3.setText("6 tries left"); 
         frame.add(panel4); 
         frame.setFocusable(true); 
         frame.requestFocusInWindow(); 
         label2.setText("won " + won + ", lost " + lost); 
         panel3.add(label2); 
         button.setText("Sekantis"); 
         BufferedReader reader = null; 
         try { 
          reader = new BufferedReader(new FileReader("hangeng.txt")); 
         } catch (FileNotFoundException e1) { 
          e1.printStackTrace(); 
         } 
         int lineno = (int) (Math.random() * 167); 
         for (int i = 0; i < lineno; i++) { 
          try { 
           reader.readLine(); 
          } catch (IOException e1) { 
           e1.printStackTrace(); 
          } 
         } 
         word = null; 
         try { 
          word = reader.readLine().replace(" ", ""); 
         } catch (IOException e1) { 
          e1.printStackTrace(); 
         } 
         String missing = ""; 
         for (int u = 0; u < (word.length() - 2); u++) { 
          missing = missing + "*"; 
         } 
         final String guess = word.charAt(0) + missing 
           + word.charAt((word.length() - 1)); 
         toguess = new StringBuffer(guess); 
         label.setText(toguess.toString()); 
         tried = new ArrayList<String>(); 
        } 
       }); 
       frame.addKeyListener(new KeyListener() { 

        @Override 
        public void keyPressed(KeyEvent e) { 
        } 

        @Override 
        public void keyReleased(KeyEvent e) { 
        } 

        @Override 
        public void keyTyped(KeyEvent e) { 
         if ((no == 6) && (!(toguess.toString().equals(word)))) { 
          frame.setFocusable(false); 
          button.requestFocusInWindow(); 
          label.setText("Deja, bet zodis buvo " + word); 
          lost = lost + 1; 
         } 
         key = String.valueOf(e.getKeyChar()); 
         String guessing = key; 
         Boolean k = false; 
         if ((no < 6)) { 
          k = false; 
          if (!(tried.contains(guessing))) { 
           tried.add(guessing); 
           for (int length = 1; length < (word.length() 
             - 1); length++) { 
            if (guessing.equals(word.substring(length, 
              (length + 1)))) { 
             toguess.replace(length, (length + 1), 
               String.valueOf(word.charAt(length))); 
             k = true; 
            } 
           } 
           if (k == false) { 
            no = no + 1; 
           } 
          } 
          label.setText(toguess.toString()); 
          if (toguess.toString().equals(word)) { 
           label.setText("Teisingai! Zodis buvo " + word); 
           frame.setFocusable(false); 
           button.requestFocusInWindow(); 
           no = 6; 
           won = won + 1; 
          } 
         } 
         label3.setText((6 - no) + " tries left"); 
        } 
       }); 
       frame.addWindowListener(new java.awt.event.WindowAdapter() { 

        @Override 
        public void windowClosing(java.awt.event.WindowEvent windowEvent) { 
         try { 
          writer.write(System.getProperty("line.separator") 
            + dateFormat.format(date) 
            + System.getProperty("line.separator") 
            + (gamecount - 1) + " games played, " 
            + lost + " lost " + won + " won"); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         try { 
          writer.flush(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
         try { 
          writer.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 
      } 
     }; 
     hangman.run(); 
    } 
} 
+0

我敢打赌,您每次开始游戏时都会注册新的关键人物,同时保留年长的听众。也就是说,如果你想回答,你应该尝试发布一个[SSCCE](http://sscce.org) – SJuan76

+0

这是你在那里为你的进口报表花哨的模式。 – Aaron

+0

是的,那是问题 –

回答

3

一些提示,让你在正确的轨道上:

  1. 你在actionPerformed()方法添加键侦听器的框架。这意味着每次单击该按钮时,都会将新的关键侦听器添加到该帧。关键侦听器应该被添加一次,并且只有一次,当帧被初始化时。

  2. 如果你不想听按键,那么你可以设置一个keyTypedIgnored标志为true。在关键监听器中,如果此标志为真,则忽略该事件。如果它被设置为false,那么你做一些事情。

  3. 关于美国东部时间,是的,所有的秋千互动应该在美国东部时间。应使用SwingUtilities.invokeLater(runnable)调用Runnable,如Swing tutorial中所述。

最后,也许最重要的建议,不要在一个巨大的方法中做所有事情。分离执行特定任务的对象和方法中的代码。添加一些空白行以使代码可读。正确缩进代码。

+0

先生,你刚刚救了我的比赛!只需将KeyListener放置在ActionListener按钮的外面即可解决问题,现在我可以自豪地享受这个蹩脚的单字hang子手。如果其他人对解决方案感兴趣,我也会更新代码。 –