我对这个程序有几个问题,我要做的第一件事是让它可以比较并查看textfield是否等于colorValues [x]位置。第二个问题是if语句说如果inText == to colorValues.length - 1打开一个框,说明这些内容都不起作用。第三个问题,即使它确实收到了对不起的消息和/或者congradulations消息,你如何制作它以便不显示文本字段?
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
答案 0 :(得分:0)
首先,我建议您学习正确格式化代码,因为这样可以更容易地了解最新情况。
其次,您发布的代码在第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();
}
}