我对这个程序有几个问题,我要做的第一件事是让它可以比较,看看文本字段是否等于colorValuesx的位置。第二个问题是if语句if inText == to colorValues.length -1,以打开一个表示恭喜的框,但这也不起作用。第三个问题,即使它确实收到了抱歉消息和/或祝贺消息,您如何使文本字段不显示?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlbertCardonaProg7 extends JFrame
{
private static final int WIDTH = 350;
private static final int HEIGHT = 250;
private static final String[] colorValues = {"red","white",
"yellow","green","blue"};// I dentifies the colors
private JTextField nameBox;
private JLabel greeting;
private String[] message = {"Input color number 1",
"Input color number 2: ","Input color number 3: "
,"Input color number 4:","Input color number 5:"};
private JLabel namePrompt = new JLabel(this.message[0]);
public AlbertCardonaProg7()
{
setTitle("MEMORY GAME");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}// end constructor
//******************************************
private void createContents()
{
nameBox = new JTextField(15);
greeting = new JLabel();
add(namePrompt);
add(nameBox);
add(greeting);
nameBox.addActionListener(new Listener());
}//end createContents
//************************************************
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int inText;
for(inText =0; inText < 5; inText++)
{
if(nameBox.getText().equals(colorValues[inText] ))
{
namePrompt.setText( message[inText]); // its not working trying
//to see if it is equal to the proper spot
//in the colorValues[array]
add(nameBox);
nameBox.setText("");
nameBox.requestFocus();
inText++;
}
if(!nameBox.getText().equals(colorValues[inText]))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Sorry, drink more Ginseng ");
add(namePrompt);
break;
}
if( inText == (colorValues.length -1))
{
AlbertCardonaProg7 darn = new AlbertCardonaProg7();
darn.namePrompt.setText("Congradulations,
Your mind is Awesome!!!");
add(namePrompt);
break;
}
}// loop
}//end action performed
}// end class Listener
//**************************************
public static void main(String[] args)
{
String colors = "";
for(int i = 0; i < colorValues.length; i++)
colors += colorValues[i] + " ";
JOptionPane.showMessageDialog(null,"How good is your memory.\n
See if you can memorize this sequence.\n\n" + colors,
"Message", JOptionPane.INFORMATION_MESSAGE );
AlbertCardonaProg7 outBox = new AlbertCardonaProg7();
}// end main class
}//end Class AlberCardonaProg7发布于 2009-12-02 20:53:11
首先,我建议您学习正确地格式化您的代码,因为这样可以更容易地看到正在发生的事情。
其次,您发布的代码在第64和80行有语法错误,这会导致编译失败。问题是Java不允许您在源代码中使用多行字符串文字,因此您必须将两个字符串连接在一起。例如:
darn.namePrompt.setText("Congradulations,
Your mind is Awesome!!!");应该是:
darn.namePrompt.setText("Congradulations," +
" Your mind is Awesome!!!");现在,不幸的是,您的问题并没有特别清楚程序的预期行为应该是什么。我对此的解释是,您希望为用户提供一个文本框,要求他们输入第一种颜色,然后显示一个对话框,根据他们的答案是否正确,显示祝贺或抱歉的对话框。如果他们得到了正确的答案,那么你想要显示第二种颜色的输入框,检查答案等。
我的第一个建议是在实例化JFrame时创建所有控件,但只需隐藏其他控件,直到用户输入正确的值。接下来,我建议您在开始编写代码之前计划好操作侦听器要做的事情。
在这种情况下,程序需要存储用户当前正在处理的输入字段的数组索引。然后,侦听器需要检查这个变量,并验证inputFields数组中的相应字段。监听器需要向用户显示一个对话框,告诉用户他们的答案是否正确,如果用户正确,则启用下一个输入字段。
把所有这些放在一起,你会得到这个:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlbertCardonaProg7 extends JFrame {
private static final int WIDTH = 350;
private static final int HEIGHT = 250;
private static final String[] colorValues = { "red", "white", "yellow",
"green", "blue" };
private final JLabel[] inputLabels = new JLabel[colorValues.length];
private final JTextField[] inputFields = new JTextField[colorValues.length];
private int index = 0;
public AlbertCardonaProg7() {
//Create the UI controls
for (int i = 0; i < colorValues.length; i++) {
inputLabels[i] = new JLabel("Input color number " + i + ":");
inputLabels[i].setVisible(false);
inputFields[i] = new JTextField(15);
inputFields[i].setVisible(false);
inputFields[i].addActionListener(new Listener());
add(inputLabels[i]);
add(inputFields[i]);
}
//Make the first set visible
inputLabels[0].setVisible(true);
inputFields[0].setVisible(true);
setTitle("MEMORY GAME");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (inputFields[index].getText().equals(colorValues[index])) {
JOptionPane.showMessageDialog(null, "Congratulations, you got the answer correct");
//See if there are more controls to make visible
if (++index < colorValues.length) {
inputLabels[index].setVisible(true);
inputFields[index].setVisible(true);
}
} else {
JOptionPane.showMessageDialog(null,
"Sorry, your answer is wrong", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
String colors = "";
for (int i = 0; i < colorValues.length; i++) {
colors += colorValues[i] + " ";
}
JOptionPane.showMessageDialog(null, "How good is your memory.\n"
+ "See if you can memorize this sequence.\n\n" + colors,
"Message", JOptionPane.INFORMATION_MESSAGE);
AlbertCardonaProg7 outBox = new AlbertCardonaProg7();
}
}编辑:基于你下面的评论,我已经修改了程序以满足预期的行为。主要的变化是构造函数不再隐藏其他控件,侦听器现在必须遍历每个输入字段以检查它们都是正确的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AlbertCardonaProg7 extends JFrame {
private static final int WIDTH = 350;
private static final int HEIGHT = 250;
private static final String[] colorValues = { "red", "white", "yellow",
"green", "blue" };
private final JLabel[] inputLabels = new JLabel[colorValues.length];
private final JTextField[] inputFields = new JTextField[colorValues.length];
public AlbertCardonaProg7() {
//Create the UI controls
for (int i = 0; i < colorValues.length; i++) {
inputLabels[i] = new JLabel("Input color number " + i + ":");
inputFields[i] = new JTextField(15);
inputFields[i].addActionListener(new Listener());
add(inputLabels[i]);
add(inputFields[i]);
}
setTitle("MEMORY GAME");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// See if there are any wrong answers
boolean correct = true;
for(int i = 0; i < colorValues.length; i++) {
if (!inputFields[i].getText().equals(colorValues[i])) {
correct = false;
}
}
if(correct) {
JOptionPane.showMessageDialog(null,
"Congratulations, you got the answer correct");
} else {
JOptionPane.showMessageDialog(null,
"Sorry, your answer is wrong", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
String colors = "";
for (int i = 0; i < colorValues.length; i++) {
colors += colorValues[i] + " ";
}
JOptionPane.showMessageDialog(null, "How good is your memory.\n"
+ "See if you can memorize this sequence.\n\n" + colors,
"Message", JOptionPane.INFORMATION_MESSAGE);
AlbertCardonaProg7 outBox = new AlbertCardonaProg7();
}
}https://stackoverflow.com/questions/1817139
复制相似问题