Java:使用 ActionListener,我需要从文本字段获取用户输入并使用它,我遇到了问题

标签 java string swing actionlistener jtextfield

所以我需要做的是使用 JTextField 以字符串形式获取用户输入,虽然我可以完成这部分,但我无法从方法中获取字符串以在程序的其他地方分析它,这是我到目前为止所拥有的:

 import javax.swing.* ;
 import java.awt.event.* ;
 import java.util.* ;
 class Games extends JFrame implements ActionListener
{
  JPanel pnl = new JPanel() ;
  JTextField input = new JTextField(38) ;
  JTextArea output = new JTextArea(6, 37) ;
  JScrollPane pane = new JScrollPane(output) ;
  String inputString ;

public Games()
{
    super("Text Based RPG") ;
    setSize(600,200) ;
    setDefaultCloseOperation(EXIT_ON_CLOSE) ;
    output.setLineWrap(true) ;
    output.setWrapStyleWord(true) ;
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;
    output.setEditable(false) ;
    pane.getVerticalScrollBar().setValue(100); //scrollbar always at bottom
    input.requestFocus() ;

    /*Edit: Should I call for the input string here, e.g. output.append(inputString)
     i get the compilation error message:
    Games.java:29: error: cannot find symbol
    output.append(inputString) ;
                  ^
    symbol:   variable inputString
    location: class Games
    */

    add(pnl) ;
    pnl.add(pane) ;
    pnl.add(input) ;

    input.addActionListener(this) ;

    setVisible(true) ;
}

public void actionPerformed(ActionEvent event) 
{
    inputString = input.getText() ;
    input.setText("") ;
}         
/*The string i need to analyse is inputString, but I cannot, get it out of this method
  any help you guys can give me would be greatly appreciated. */

public static void main(String[] args) 
{
    Games gui = new Games() ;
}
}

**********************************
    try
    {
        a = reader.readLine() ;
    }
    catch (IOException e ) 
    {
        System.out.println("An Input Error Has Occured") ;
    }
    if (a.indexOf("hide") != -1 ) switchvariable = 'b' ;
    if  ((a.indexOf("exit") != -1 ) || (a.indexOf("leave") != -1) || (a.indexOf("out") != -1)) switchvariable = 'a' ;
    if ((a.indexOf("exit") == -1) && (a.indexOf("hide") == -1) && (a.indexOf("leave") == -1) && (a.indexOf("out") == -1)) switchvariable= 'c' ;
    String outcome = "" ;
    switch (switchvariable) 
    {
    case 'a' : outcome = "You exit the tavern to be discovered by\na group of bandits!\n" ; i = -1 ; break ;
    case 'b' : outcome = "You hide behind the bar for 10 minutes until\nyou can no longer hear the group." ; i = -1 ; break ;
    case 'c' : outcome = "You must choose between the two given options, exit the tavern, or hide." ; break ; // the variable i will still be greater
    //than 1 so the loop will continue until you reach an agreeable option, which in this case is either hiding, or addressing the group of people
    }
    System.out.println(outcome) ;           

我理想中想做的是使用文本字段进行用户输入,所以我需要做的是将文本放入输出文本区域中,然后这应该根据用户输入进行更改,这是我想要的代码示例喜欢使用的显示在星号下方

最佳答案

如果我理解正确的话,当用户在文本字段中按 Enter 时,您希望将在文本字段中输入的文本附加到输出文本区域。

当按下 Enter 时,文本字段会触发一个操作事件。您在类里面收听此事件。所以,此时,您只需将值附加到文本区域即可。

Swing 是基于事件的:

public void actionPerformed(ActionEvent event) {
    String inputString = input.getText() ;
    output.append(inputString);
    input.setText("");
}

构造函数中的代码仅在执行时执行。因此,获取文本字段的值当然会返回一个空字符串,因为在构造时,文本字段甚至还不可见。

编辑:要做你想做的事,只需执行以下操作:

public void actionPerformed(ActionEvent event) {
    String inputString = input.getText() ;
    printOutcome(inputString);
    input.setText("");
}

private void printOutcome(String input) {
    char switchvariable = ' ';
    if (input.indexOf("hide") != -1) {
        switchvariable = 'b' ;
    }
    if ((input.indexOf("exit") != -1 ) || (input.indexOf("leave") != -1) || (input.indexOf("out") != -1)) {
        switchvariable = 'a';
    }
    if ((input.indexOf("exit") == -1) && (input.indexOf("hide") == -1) && (input.indexOf("leave") == -1) && (input.indexOf("out") == -1)) {
        switchvariable= 'c';
    }
    String outcome = "" ;
    switch (switchvariable) {
        case 'a' : 
            outcome = "You exit the tavern to be discovered by\na group of bandits!\n"; 
            break;
        case 'b' : 
            outcome = "You hide behind the bar for 10 minutes until\nyou can no longer hear the group." ; 
            break ;
        case 'c' : 
            outcome = "You must choose between the two given options, exit the tavern, or hide."; 
            break;
    }
    System.out.println(outcome);
}

不过这个方法太复杂了。使用 if 子句设置 char 变量,然后切换该 char 变量是没有意义的。你的 if 子句也应该被清理。

关于Java:使用 ActionListener,我需要从文本字段获取用户输入并使用它,我遇到了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13939702/

相关文章:

java - 如何渲染具有 3 种颜色渐变的圆弧?

java - 在 BoxLayout 中将按钮居中对齐

java - HTTPEntity 给出空值

java - 如何使用scanner函数在java中逐个字符获取并将其存储在二维数组中

php - PHP 中的裸字符串是否有任何合法用途?

javascript - 删除特定字符jquery后引号中的字符

c++ - C/C++ 中的快速字符串标记化

java - Google App Engine 和 Python 连接项目

java - 线程安全队列类出现问题。具体来说,除了异常(exception)情况

Java - Swing GUI 未加载