我对这个程序有几个问题,我想要做的第一件事是让它可以比较并查看文本字段是否等于colorValues [x]位置。第二个问题是if语句说如果inText ==为colorValues.length - 1打开一个框,表示congradulations不起作用。第三个问题,即使它得到抱歉消息和或congradulations消息你如何使它的文本字段不显示?简单的FlowLayOut帮助
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
我来编辑它使每个人都可以看到所有的代码并且可以给我一些我应该做的步骤的建议。 – daddycardona 2009-11-30 00:15:24
有人可以帮我用我的循环? – daddycardona 2009-11-30 01:56:30