Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: I get this error...
|
Posted: Nov 22, 2002 11:53 AM
|
|
Here is a slightly modified version of your program to illustrate what I meant. When you write "private int x;" inside your class definition, you've declared a member variable x. If you write "x = 5;" in your init() method then you modify that member variable. However, if you write "int x = 5;" in your init() method, you've just created another variable which resides inside the scope of the method and set it to 5, but the member variable you declared before is unaffected.
You'll see below what I meant about initializing the variables where you declare them, also.
By the way, I added a main, which puts the applet into a JFrame, so that the thing can run stand-alone, without a web page or appletviewer (just "java EApplet" will run it).
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class EApplet extends Applet implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private TextField QuestionField;
private Label ScoreLabel;
private int Score;
private int i;
private String Questions[];
String[] Questions = { "A Tomato is a vegetable.",
"There are gray elephants in Denmark.",
"Certain politicians really are morons.",
"The sun is warm.",
"Ontogeny recapitulates phylogeny."};
int[] Answers = {0, // Nope, it's a fruit.
1, // Obviously.
1, // Of course.
0, // No, it is hot!
1}; // Natually.
private int Answers[];
private boolean foundKey;
public void init()
{
foundKey = false;
int i = 1;
/*int[] Answers = new int[5];
String[] Questions = new String[5];
Questions[1] = "A Tomato is a vegetable.";
Answers[1] = 0;
Questions[2] = "You love this game.";
Answers[2] = 1;
Questions[3] = "Troy is an Idiot!";
Answers[3] = 1;
Questions[4] = "The sun is warm.";
Answers[4] = 1;
*/
lcdField = new TextField(20);
QuestionField = new TextField(20);
ScoreLabel = new Label("0");
Score = 0;
keyPad = new Panel();
keysArray = new Button[3];
setLayout(new BorderLayout());
lcdField.setEditable(false);
QuestionField.setEditable(false);
keysArray[1] = new Button("YES");
keysArray[2] = new Button("NO");
keyPad.setLayout(new GridLayout(1,2)); keyPad.add(keysArray[1]);
keyPad.add(keysArray[2]);add(lcdField, BorderLayout.NORTH);
add(QuestionField, BorderLayout.SOUTH);
add(ScoreLabel, BorderLayout.EAST);
add(keyPad, BorderLayout.CENTER);
QuestionField.setText(" " + (Questions[i]));
keysArray[1].addActionListener(this);
keysArray[2].addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
foundKey = false;
System.out.println( "Question: " + Questions[i] );
QuestionField.setText( Questions[i] );
if(e.getSource() == keysArray[1])
{
foundKey = true;
if(Answers[i] == 1)
{
lcdField.setText("You are Correct!!");
Score += 1;
ScoreLabel.setText(" " + (Score));
}
if(Answers[i] == 0)
{
Score -= 1;
ScoreLabel.setText(" " + (Score));
lcdField.setText("You are Incorrect!!");
}
}
if(e.getSource() == keysArray[2])
{
lcdField.setText(""+(Answers[i]));
foundKey = true;
if(Answers[i] == 0)
{
lcdField.setText("You are Correct!!");
Score += 1;
ScoreLabel.setText(" " + (Score));
}
if(Answers[i] == 1)
{
Score -= 1;
ScoreLabel.setText(" " + (Score));
lcdField.setText("You are Incorrect!!");
}
}
if(Score == 4)
{
lcdField.setText("You WIN!!!");
}
else
{
lcdField.setText("You lose...");
}
}
public static void main(String[] args)
{
Applet applet = new EApplet();
javax.swing.JFrame frame = new javax.swing.JFrame("EApplet");
// To close the application:
frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add(applet);
frame.setSize(400,300);
applet.init();
applet.start();
frame.setVisible(true);
}
}
|
|