再次寻求帮助来解决家庭作业的问题。我已经完成了大部分内容,但我很难理解为什么我不能让 for 循环等待用户文本输入。基本上,我需要程序提示用户输入内存中的颜色。颜色作为字符串包含在数组中。在 for 循环的每次迭代中,我希望操作监听器检查并查看是否输入了正确的文本。如果是这样,该框应将其 JLabel 文本从“输入颜色编号 x”更改为“输入颜色编号 x+1”
这是我注意到的: 我的 for 循环在考虑让用户在字段中键入文本之前贯穿整个迭代周期。因此,它不会提示用户输入颜色编号 1,而是直接输入颜色编号 5。
这是我得到的代码:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class ColorGame extends JFrame
{
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
//Text field that will confirm a user submission
private JTextField colorEntry = new JTextField(10);
private int colorIndex = 0; //current index of colorSequence
//String Array of colors to be searched for and validated in a text field
private String[] colorSequence = {"red", "white", "yellow", "green", "blue"};
public ColorGame()
{
setTitle("Memory Game");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}
public void createContents()
{
//dialog box to inform user of program's purpose
JOptionPane.showMessageDialog(null, "How good is your memory?\nTry to memorize "
+ "this color sequence:\n\nred, white, yellow, green, blue");
//JLabel to prompt user for color
JLabel colorPrompt = new JLabel();
add(colorPrompt);
add(colorEntry);
for(int i = 0; i < colorSequence.length; i++)
{
colorIndex = i + 1;
colorPrompt.setText("Enter color number " + colorIndex + ":");
colorEntry.addActionListener(new Listener());
}
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String colorText = "";
colorText = colorEntry.getText();
if(colorText.equals(colorSequence))
System.out.println("Hello");
else
System.out.println("No");
}
}
public static void main(String[] args)
{
new ColorGame();
}
}
最佳答案
您的 for 循环是逐行运行命令行程序的代码,但这不是事件驱动 GUI 的工作方式。
相反,您希望 ActionListener 中的代码推进计数器并根据该计数器更改行为。
private class Listener implements ActionListener
{
int counter = 0;
public void actionPerformed(ActionEvent e)
{
switch (count) {
case 0:
// do something for 0
// including changing JLabel text if need be
break;
case 1:
// do something for 1
break;
case 2:
// do something for 2
break;
default;
// do something for default
}
count++;
}
}
for 循环将立即循环,而不关心用户的输入或按钮何时被按下。关键是您想要根据 JButton 被按下的次数来改变程序的状态。而且由于状态变化是由按下按钮触发的,因此无需担心某些循环循环速度太快。
<小时/>这是一个相关示例,它不是精确的解决方案,但应该为您提供一些解决方案的想法:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ChangeState {
public static final String[] STRINGS = {
"Enter the 1st String:",
"Enter the 2nd String:",
"Enter the 3rd String:"
};
private static void createAndShowGui() {
final JTextField textField = new JTextField(10);
final String[] replies = new String[STRINGS.length];
final JLabel label = new JLabel(STRINGS[0]);
JPanel mainPanel = new JPanel();
mainPanel.add(label);
mainPanel.add(textField);
textField.addActionListener(new ActionListener() {
int count = 0;
@Override
public void actionPerformed(ActionEvent arg0) {
if (count < STRINGS.length) {
replies[count] = textField.getText();
count++;
if (count < STRINGS.length) {
label.setText(STRINGS[count]);
} else {
label.setText("Done!");
System.out.println("Replies:");
for (String reply : replies) {
System.out.println(reply);
}
}
textField.setText("");
}
}
});
JFrame frame = new JFrame("ChangeState");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
关于java - For 循环不等待 JTextField 中的用户响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462839/