Java:使用ActionListener,我需要从文本字段获取用户输入并使用它,I';我有问题
因此,我需要做的是使用JTextField以字符串的形式获取用户输入,虽然我可以做这部分,但我在从程序中其他地方分析字符串的方法中获取字符串时遇到了困难,以下是我到目前为止得到的结果:Java:使用ActionListener,我需要从文本字段获取用户输入并使用它,I';我有问题,java,string,swing,actionlistener,jtextfield,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()
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子句也应该清理。很抱歉,我在编写output.append(inputString)时可能误导了您,虽然您给出的解决方案是正确的,但这不是我想要的。我需要能够将inputString从方法中拉出来,并将其与indexOf运算符一起使用,如果您知道这样做的方法,我将不胜感激:)想法是将来自用户的数据存储在变量中,但在actionPerformed方法中不可能这样做,因为每次用户按enter键,方法中附加的所有内容都被放入,这显然不是期望的结果。从
actionPerformed()
方法中调用indexOf()
方法,或者将inputString作为参数传递给另一个调用indexOf()
的方法。将输入字符串存储在变量中是个坏主意。如果您不明白,请编辑您的问题,并详细说明您真正想做的事情。好的,我将编辑问题,完整地说明我想做什么,感谢您的贡献。您提到的方法太复杂,您有什么建议可以简化我正在尝试做的事情吗?同样在最后一行,这是早期版本,如果我试图将结果打印到输出文本区域,我将如何更改该行?