gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:18:31 25 4
gpt4 key购买 nike

所以我需要做的是使用 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 时,文本字段会触发一个 Action 事件。你在类里面听这个事件。因此,此时,您只需要将值附加到文本区域。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com