0

我对这个程序有几个问题,我要做的第一件事就是让它可以比较并查看文本字段是否等于 colorValues[x] 位置。第二个问题是 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
4

1 回答 1

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();

    }
}
于 2009-12-02T12:53:10.820 回答