简体   繁体   English

在Java.awt中使用KeyEvents

[英]Using KeyEvents with Java.awt

I am trying to create a text-game and have decided the best way to do this is to use a UI instead of the cin.nextLine() method and the compiler output. 我正在尝试创建一个文本游戏,并且已确定执行此操作的最佳方法是使用UI而不是cin.nextLine()方法和编译器输出。 What I've been trying to do is to add a KeyEvent to my TextField for user input where when the user presses the enter key their text goes into the TextArea I've set above just under any other text and the input goes away. 我一直试图做的是将一个KeyEvent添加到我的TextField中供用户输入,当用户按下Enter键时,他们的文本进入我在其他任何文本下方都已设置的TextArea中,并且输入消失了。 I've also been trying to add the String that they typed into the input just before pressing the enter key into a String variable that will be processed later in if/else statements. 我也一直在尝试将输入的字符串添加到输入中,然后将Enter键按下到String变量中,稍后将在if / else语句中对其进行处理。

I've tried adding the text main.input.setText(""); 我尝试添加文本main.input.setText(“”); but that did not work either. 但这也不起作用。 As well, I've attempted to add the word public before the deceleration and initialisation of the TextField but that came up with an error of its own.I also don't exactly know how I should properly access the text inside of the TextField to add it to the TextArea above and to process it as a String variable. 同样,我试图在减慢和初始化TextField之前添加public一词,但这带来了自己的错误。我也不完全知道如何正确访问TextField中的文本以将其添加到上面的TextArea并将其作为String变量进行处理。

import java.awt.*;
import java.awt.event.*;

public class UI extends Frame implements KeyListener
{
    public void test()
    {
        Frame main=new Frame();
        main.setSize(1366,768);
        main.setLayout(null);
        main.setVisible(true);
        TextArea mainText=new TextArea("Come on and do something!");
        mainText.setBounds(10,30,1366,728);
        main.setBackground(Color.white);
        mainText.setEditable(false);
        TextField input=new TextField("");
        input.setBounds(10,738,1366,20);
        main.add(input);
        main.add(mainText);
        input.addKeyListener(this);
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        Toolkit.getDefaultToolkit().beep();
        input.setText("");
    }
}

    public static void main(String[] args)
    {
        UI set = new UI();
        set.test();
    }
}

I expected this code to set the TextField to blank and I'd hoped to go further to find out how to add the code to a variable and to the Text Area. 我希望这段代码将TextField设置为空白,并且希望进一步找出如何将代码添加到变量和Text Area中。 Instead, I get this error message: 相反,我收到此错误消息:

Error: cannot find symbol
  symbol:   variable input
  location: class UI

EDIT: Alright so I didn't know awt was out of date, so I did change it to a Swing version. 编辑:好的,所以我不知道awt已过时,所以我确实将其更改为Swing版本。 I did also remove the extending as well as changing the KeyListener to an ActionListener. 我也确实删除了扩展并将KeyListener更改为ActionListener。 However, what occurs now is that the ActionListener is not being properly added to the JTextField. 但是,现在发生的是没有将ActionListener正确地添加到JTextField中。 Here's my updated code below: 这是我下面更新的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UI
{
    private JFrame main;
    private JTextArea mainText;
    public UI()
    {
        main=new JFrame("Text Game");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(1366,768);
        mainText=new JTextArea("Come on and do something!");
        mainText.setBounds(10,100,1366,728);
        mainText.setEditable(false);
        JTextField input=new JTextField("");
        input.setBounds(10,700,1366,20);
        input.addActionListener(this);
        main.add(input);
        main.add(mainText);
        main.setVisible(true);
    }

        public static void main(String[] args)
        {
            new UI();
        }
}

Any ideas for what I've done wrong now? 关于我现在做错了什么的任何想法?

The basic problem is one of context. 基本问题是环境之一。 input is declared as a local variable within the context of the constructor, so it can not be accessed from any other context input在构造函数的上下文中声明为局部变量,因此无法从任何其他上下文访问它

 public void test()
    {
        //...
        TextField input=new TextField("");
        //...
    }

To be honest, this is pretty basic Java 101, you should start by having a look at Understanding Class Members . 坦白地说,这是非常基本的Java 101,您应该首先了解了解类成员 This is a concept you should already understand before embarking on GUI development (IMHO) 在开始进行GUI开发(IMHO)之前,您应该已经了解了这一概念。

Observations 意见

  • Avoid KeyListener (generally), especially with text components 避免使用KeyListener (通常),尤其是对于文本组件
  • Use an ActionListener instead 改用ActionListener
  • AWT is out-of-date, consider Swing or, better yet, JavaFX AWT已过时,请考虑使用Swing或更好的JavaFX
  • Avoid null layouts, they will waste your time 避免使用null布局,这会浪费您的时间
  • As a general rule, you should not be extending directly from top level containers like Frame . 通常,您不应直接从诸如Frame类的顶级容器扩展。 They lock you into a single use case, reduce re-usability and you're not really adding any new functionality to the class anyway 他们将您锁定在一个用例中,减少了可重用性,并且您实际上并没有真正在类中添加任何新功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM