I have a few questions for this program, one the first thing I am trying to do is make it so it could compare and see if the textfield is equal to the colorValues[x] position. The second issue is the if statement says if inText == to colorValues.length - 1 to open a box that says congradulations that does not work either. 3rd issue even if it did get the Sorry message and or congradulations message how do you make it so that the textfield is not shown?
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