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