机器人在JTextfield中输入密钥而不是触发代码

时间:2016-12-12 09:47:49

标签: java jtextfield enter robot

我制作了一个热土豆游戏,并且通过输入某些字母键并按下JTextfield中的输入,另一个玩家获得了一个转弯并且GUI更新了。当游戏在计时器用完之前运行时,我想让空格键作为“输入”按钮,因此我在按下空格时使用机器人来触发输入键。看来它可以作为,但由于某种原因,当我使用空格输入字母时,它不会正确运行附加到ActionListener的其余代码。

我是否需要添加更多代码来触发与按下Enter按钮相关的某种操作?除了r.keyPress(keyCode)之外还有更多模拟enter键操作的东西吗?下面我相信这部分程序都是守旧代码。

public char l = 'a'; // char l acts as random char target
public String s = ""; // String s acts as target
boolean p1Starts = false;

int totalTime = 1000*(HotPotato.randNum()); // randomizing the length of the game
long startTimeMili = 100;  // setting up the current timer
long startTimeSec = 1;  // setting up the current timer
boolean timerState = true; // creating timer boolean
Robot r; 

JPanel content = new JPanel(new BorderLayout());
JPanel graphics = new JPanel(new GridLayout(0, 3));

public HotPotatoGUI() {

    myHotPotato = new HotPotato();

    setTitle("Hot Potato");

    // graphics = new JPanel();

    myPicture = new HotPotatoDrawer(myHotPotato.state());
    setContentPane(content);

    team1.addActionListener(this);
    main.addActionListener(this);
    team2.addActionListener(this);

    entryText.addKeyListener(new KeyListener(){ 

        public void keyPressed(KeyEvent a) {
             if(a.getKeyCode()==KeyEvent.VK_SPACE) {
                try {
                    r = new Robot();
                } catch (AWTException e) {
                    e.printStackTrace();
                }
                 int keyCode = KeyEvent.VK_ENTER; // the enter key
                 r.keyPress(keyCode);
                 r.keyRelease(keyCode);

             }
        }
        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }
        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
        }
    });

    entryText.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)  {

            String entry = entryText.getText(); // sets entry equal to text put into textfield

            System.out.print("\nQuestion " + s); // remove
            System.out.print("\nEntry " + entry); // remove

            if (entry.equals(s))
            {
                myHotPotato.flipSwitchOne();
                p1Starts = !p1Starts;
                l = HotPotato.randLetterBoth(p1Starts); // sets new char target 
                s = Character.toString(l); // converts new target into string
                long present = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());

                // should activate when the timer reaches 0
                if (present - startTimeSec >= totalTime/1000) {
                    myHotPotato.endGame();
                    System.out.print("Switched");
                    entryText.removeActionListener(this);
                }

                // lines to see the variables involved in the timer
                System.out.print("\n + total time = " + totalTime/1000);
                //System.out.print("\n + currenttime = " + System.currentTimeMillis());
                System.out.print("\n Timer " + (present - startTimeSec));
                System.out.print("\n + TimerState = " + myHotPotato.stateTimer());

                setLabels(s, p1Starts);
                myPicture.makeHotPotatoOn(myHotPotato.state());
                myPicture.repaint();

            }

            entryText.setText(""); // clears text after entry

            timerState = (System.currentTimeMillis() - startTimeSec >= totalTime);

          }
        });

输出文字。注意Entry如何显示它与Question字母相同,但在第一种情况下使用“enter”使用了ActionListener中的所有代码,而空格键似乎只是在那里执行字符串。

Question c
Entry c
 + total time = 12
 Timer 2
 + TimerState = true
Question n
Entry n 

0 个答案:

没有答案