Here the project i have to make

First i have to create a GUI that contains the normal buttons with minimize, maximize, and X button.

Well, i'm suppose to create 3 questions, and answer them in textbox. Following that, when i hit enter it is suppose to say if you are correct or not. The problem is that i can't seem to get my strings right to compare. It's either that or there is something wrong with my if- else statement. Basically i want it to show if they are correct or incorrect. Please help:

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String string1 = "Wilson";

private String string2 = " ";

private String string3= "Ace";

private String string4 = " " ;

private String string5 = "Yes" ;

private String string6 = " ";

public AnAQuiz() {

choice = new JComboBox();

label = new JLabel();

label.setBackground(Color.blue);

text = new JTextField(42);

CheckAnswer = new Button("Check Answer");

TryAgain = new Button("Would you like to try again?");

if (string1.equals(string2)) {

label.setText("correct");

} else if (string2 != (string1)) {

label.setText("incorrect");

}

if (string3.equals(string4)) {

label.setText("correct");

} else if (string4 != (string3)) {

label.setText ("incorrect");

}

if (string5.equals(string6)) {

label.setText("correct");

} else if (string6 != (string5)) {

label.setText("correct");

}

choice.addItem("Tennis Question #1");

choice.addItem("Tennis Question #2");

choice.addItem("Tennis Question #3");

Listener listen = new Listener();

choice.addActionListener(listen);

add(choice);

add(label);

add(text);

add(CheckAnswer);

add(TryAgain);

}

private class Listener implements ActionListener {

public void actionPerformed(ActionEvent event) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText("Whats the famous tennis brand that begins with 'W'?");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

}

}

}

//--

public static void main(String[] args) {

JFrame frame = new JFrame("Quiz");

frame.setForeground(Color.RED);

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.show();

frame.setVisible(true);

}

}

Message was edited by:

Vaust

[2929 byte] By [Vausta] at [2007-11-26 19:13:39]
# 1

Couple of things:

* your tests are at the wrong place - you'd like to call them after the user presses the 'check my answer' button

* you never seem to use string2/4/6 (and you don't need 'em - you can use the text in the text field)

* you're correct in using equals and not == to compare Strings, but you shouldn't use != either - use ! a.equals (b) instead

Oh, and when posting, please use code tags - it makes your code so much easier to read (which helps getting answers faster).

Edit: one last thing I forgot - you could also use equalsIgnoreCase to make the quiz a bit more forgiving.

levi_ha at 2007-7-9 21:13:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
Could you guys edit wat i wrote plz! thanks! It will help me better understand.
Vausta at 2007-7-9 21:13:39 > top of Java-index,Desktop,Core GUI APIs...
# 3

[nobr]Hi

I Have posted my small project of mine I think it should suffice you.

I don't use database to store the question or the Answer Instead i have stored everything in a string and retriving it.

Go through it and get some idea

import java.awt. *;

import java.awt. event.*;

import javax.swing.*;

class QuestionSeries{

static String info ="Java Online Test Week \n \nINSTRUCTIONS:\nThere are 25 questions in this test and 25 minutes to complete them all.\nThe finish button is highlighted in blue when you reach the end of the test. \nClicking the finish button will display the results with the correct answers marked in light red. \n \nThe timecounter begins when you click on the 'start' button \n \nBest of luck!\n";

static String []question ={"Question 1:\nWhat is the result of attempting to compile and run the following program?\n\npublic class JavaAppclass{\npublic static void main(String args){\nString myString = \"javaprograms\";\nSystem.out.println(myString);\n}\n}\n",

"Question 2:\nWhat is the result of attempting to compile and run the following program with \n\n>javac JavaApp.java\n>java JavaApp\n\nclass TryFirst{\nString myString = \"javatest\";\n}\n\nclass JavaApp{\n\npublic static void main(String[] arg){\n System.out.println(new TryFirst().myString);\n}\n}\n",

"Question 3: \nWhat is the result of attempting to compile and run the following program with \n>javac JavaApp.java\n>java JavaApp\n\nclass MyJavaApp{ \nString myString = \"elephants\"; \npublic static void main(String[] arg){ \nSystem.out.println(myString); \n} \n}\n",

"Question 4:\nWhat is the result?\n\nclass JavaApp{ \n public static void main(String[] a){ \nString textstr = \"hello\";\nfor (int i=4; i<5; i++)\nSystem.out.println(str); \n } \n}\n",

"Question 5:\nWhat is the result here?\n\nclass MyJavaApp{ \n public static void main(String[] a){ \nint j=0;\nfor (int i=0; i<3; i++) j=j+i;\nSystem.out.println(j); \n } \n}\n",

"Question 6:\nWhat is the result?\n\nclass MyJavaApp{ \n public static void main(String[] a){ \nint num1;\nint num2 = 7;\nnum2= num1 * 2 +3;\nSystem.out.println(num2); \n } \n}\n",

"Question 7:\nWhat is the result?\n\nclass MyJavaApp{ \n int num1;\n public static void main(String[] a){ \nnumber1=2;\nint number2 = 7;\nnumber2= number1 * 2 +3;\nSystem.out.println(number2); \n } \n}\n",

"Question 8:\nWhat is the result?\n\nclass JavaApp{ \n static int number1 = 4;\n public static void main(String[] a){ \nnumber1=2;\nint number2 = 7;\nnumber2= number1 * 2 +3;\nSystem.out.println(number2); \n } \n}\n",

"Question 9:\nWhat is the result?\n\nclass JavaApp{ \n static int number1 = 3;\n public static void main(String[] arg)\nint number2 = 7;\nnumber2= number2 + number1 * 2 +3;\nSystem.out.println(number2); \n } \n}\n",

"Question 10:\nWhat is the result of compiling and running the following code?\n\nclass JavaApp{ \n public static void main(String[] a){\nint x = (int) (Math.random());\nSystem.out.println(x); \n } \n}\n",

"Question 11:\nWhat is the result?\n\nclass Tryclass{ \n static String text = \"rabbit\";\n public static void main(String[] a){\nint num = text.length;\nSystem.out.println(num); \n } \n}\n",

"Question 12:\nWhat would be the result here?\n\nclass Myclass{ \n public static void main(String[] a){\nchar []rabbit={'a','b','c','d'};\nint num = rabbit.length;\nSystem.out.println(num); \n } \n}\n",

"Question 13:\nWhat is the result here?\n\nclass JavaApp{ \n public static void main(String[] a){\nint number = 10;\nString mystr = Integer.toBinaryString(number);\nSystem.out.println(mystr); \n } \n}\n",

"Question 14:\nWhat would be the result if we were to run this program with\n>java MyJavaApp hello java world ?\n\nclass MyJavaApp{ \npublic static void main(String[] args){\nSystem.out.println(args[1]); \n } \n}\n",

"Question 15:\nWhat is the result of this program?\n\nclass MyJavaApp{ \n public static void main(String[] a){\ndouble d =1.75;\nint i = d;\nSystem.out.println(i++); \n } \n}\n",

"Question 16:\nWhat is the result of this program?\n\nclass MyJavaApp{ \n public static void main(String[] a){\nint 1stNum = 5;\nint 2ndNum = 3;\ndouble d =1.25 +1stNum/2ndNum;\nSystem.out.println(d); \n } \n}\n",

"Question 17:\nWhat is the result of this program?\n\nclass MyJavaApp{ \n public static void main(String[] arg){\nint Num1 = 5;\nint Num2 = 4;\ndouble d =1.25 +Num1/Num2;\nSystem.out.println(d); \n } \n}\n",

"Question 18:\nWhat is the result of this program?\n\nclass TryJavaApp{ \n static float f;\n public static void main(String[] ar){\nint Number = 5;\nf = 1.25;\nSystem.out.println(f*4); \n } \n}\n",

"Question 19:\nWhat is the result of this program?\n\nclass JavaApp{ \n static float f;\n public static void main(String[] ar){\nint Num = 2;\nf = (float)1.25 * 2;\nSystem.out.println(f * Num); \n } \n}\n",

"Question 20:\nWhat is the result of this program?\n\nclass HelloJavaApp{ \n public static void main(String[] ar){\nbyte num = 64;\nnum += num;\nSystem.out.println(num); \n } \n}\n",

"Question 21:\nWhat is the result of this program?\n\nclass JavaApp{ \n public static void main(String[] a){\n double d = 1.56789;\nlong num = Math.round(d);\nSystem.out.println(num); \n } \n}\n",

"Question 22:\nWhat is the result of this program?\n\nclass JavaApp{ \n public static void main(String[] a){\ndouble d = 1.56789;\nint num = (int)Math.round(d * d);\nSystem.out.println(num); \n } \n}\n",

"Question 23:\nWhat is the result of this program?\n\nclass MyJavaApp{\npublic static void main(String[] arg){\ndouble doub1 = 1.51;\nint number1 = (int) (doub1 +0.5);\nSystem.out.println(number1); \ndouble doub2 = -1.51;\nint number2 = (int) (doub2 +0.5);\nSystem.out.println(number2); \n } \n}\n",

"Question 24:\nWhat is the result of this program?\n\nclass TryApp{ \npublic static void main(String[] arg){\nint num = 4;\nnum = (num + 4/2) % num+1;\nSystem.out.println(num); \n } \n}\n",

"Question 25:\nWhat is the result?\n\nclass MyApp{ \n public static void main(String[] ar){\nfor(int i=0; i<3; i++)\nfor(int j=1; j<3; j++)\nSystem.out.print(j+i+\",\"); \n } \n}\n"

};

static String [][]answers ={{"It will not compile as the main method String is not an array\n","It will compile but there will be a runtime error if we try to run it with:- \n>java JavaApp\n","The program is fine, but we will need another program to run it and get the output.\n","It will compile and run and output \"java\" to the screen\n"},{"It will not compile as you cannot have 2 java source files on the same page\n"," \nIt will output \"javatest","It will not compile as the String myString is not-static and it is being accessed from a static method\n"," \nIt compiles but there is no output at runtime\n"},{" \nFine - it will output elephants\n","Runtime error MyJavaApp is not public\n","Compiler error myString is a non-static variable being accessed in a static method\n","Compile error in the main() method\n"},{" \nIt will output hello\n","The out put will be \nhello\nhello\n"," \nNo output the for loop needs a ' ; ' at the end\n"," \nCompilation error for loops have to start with a zero\n"},{" \n3\n"," \n6\n"," \n4\n"," \n2\n"},{" \n17\n"," \n5\n"," \n3\n","It won't compile\n"},{" \n14\n"," \n7\n"," \n17\n"," \nError.\n"},{" \n16\n"," \n14\n"," \n7\n"," \nError\n"},{" \n15\n"," \n23\n"," \n50\n"," \nError\n"},{" \n1\n"," \nYou cannot predict the random result\n"," \n0\n"," \nError\n"},{" \n6\n"," \n5\n"," \n0\n"," \nError\n"},{" \n4\n"," \n3\n"," \n0\n"," \nError\n"},{" \n00001010\n"," \n10\n"," \n1010\n"," \nError\n"},{"hello java world\n"," \nhello\n"," \njava\n"," \nworld\n"},{" \n1.75\n"," \n2\n"," \n3\n"," \nError\n"},{" \n3.25\n"," \n2.25\n"," \n2.2... and a very long number\n"," \nThe code will not compile\n"},{" \n2.5\n"," \n2.25\n"," \n2.2 ... -something and a very long number\n"," \nThe code will not compile\n"},{" \nError float f is not initialised\n"," \noutput: 5\n"," \noutput: 5.0\n"," \nError, this is not a valid way to initialise a float type variable\n"},{" \n4.0\n"," \n5\n"," \n5.0\n"," \nError, it will not compile\n"},{" \n128\n"," \n-128\n"," \n1\n"," \nError, number is out of range\n"},{" \n1\n"," \n2\n"," \n1.0\n"," \nError\n"},{"3. - and to 15 decimal places\n"," \n2\n"," \n3\n"," \nError\n"},{" 2 \n-2\n"," 1\n-2\n"," 2\n-1\n"," 1\n-1\n"},{" \n1\n"," \n3\n"," \n4\n"," \n6\n"},{"1,2,2,3,3,4\n","1,2,3,3,4,6\n","1,2,3,4\n"," \n0,1,1,2,2,3,3,4\n"} };

static int []n = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

static String []choice= {"23","2","3","1","1","4","4","3","4","3","4","1","3","3","4","4","2","4","3","2","2","3","3","2","1"};

static int tally = choice.length;

static String testtitle="Java Programming Online Test";

static int timeLimit =10;

static int passMark = 15;

}

/* OnlineTest class */

public class OnlineTest extends JFrame{

static String studentname ="";

static int TOTAL=0;

static {

try{

TOTAL = QuestionSeries.tally;

/* The input window */

studentname = JOptionPane.showInputDialog("Enter your name: ");

if(studentname.length() < 1) studentname = "Anonymous";

else studentname = studentname.trim() + " ";

}catch(NullPointerException e){ System.exit(0); }

}

int seconds, minutes;

int quesnum, itemCheck, mark;

final String TESTTITLE = QuestionSeries.testtitle;

final int TIMELIMIT = QuestionSeries.timeLimit;

final int PASS = QuestionSeries.passMark;

String []answers = new String[TOTAL];

JButton []choice_button = new JButton[6];

JTextArea answerboxes[] = new JTextArea[4];

JCheckBox []boxes = new JCheckBox[4];

JTextPane pane = new JTextPane();

JLabel student, choose, message, timecounter, testresult;

boolean start_test, check_answer, allowRestart, finishtest;

Northwindow panelNorth = new Northwindow();

Southwindow panelSouth = new Southwindow();

Centerwindow panelCenter = new Centerwindow();

/* OnlineTest Constructor */

protected OnlineTest(){

for (int i=0; i<TOTAL; i++) answers[i] ="";

getContentPane().setLayout(new BorderLayout() );

getContentPane().add("North", panelNorth);

getContentPane().add("South", panelSouth);

getContentPane().add("Center", panelCenter);

int width = 0, height=0;

if(java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth()><799){width=640; height=460; }

else {width=720; height=540; }

setSize(width,height);

Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

setLocation((dim.width-width)/2, (dim.height-height)/2);

}

/**

** Northwindow class

**/

class Northwindow extends JPanel{

/**

** Northwindow constructor

**/

public Northwindow(){

setLayout(new GridLayout(2,2));

setBackground(new Color(230, 230, 255));

student = new JLabel("\t Welcome : "+studentname+" to the Online Java Test");

student.setFont(new Font("",Font.BOLD,16) );

message = new JLabel();

message.setForeground(Color.blue);

add(student);

add(message);

add(new JLabel("") );

add(new JLabel("") );

setBorder(BorderFactory.createEtchedBorder() );

}

}

/**

** Southwindow class

**/

class Southwindow extends JPanel{

public Southwindow(){

String []key = {"","start:","next:","finish:","check next:","check previous:"};

for(int i=0; i<choice_button.length; i++)

{

choice_button[i] = new JButton(key[i]);

choice_button[i].addActionListener(new ActionHandler() );

if(i !=0)add(choice_button[i]);

}

setBorder(BorderFactory.createEtchedBorder() );

}

}

/**

** Centerwindow class

**/

class Centerwindow extends JPanel{

public Centerwindow(){

setLayout(new GridLayout(1,2) );

JScrollPane west = new JScrollPane(pane);

pane.setForeground(Color.red);

pane.setFont(new Font ("monospaced",0,12) );

pane.setText(QuestionSeries.info);

pane.setEditable(false);

JPanel east = new JPanel();

east.setLayout(new BorderLayout() );

JPanel northEast = new JPanel();

northEast.setBackground(new Color(230, 230, 255) );

east.add("North", northEast);

JPanel westEast = new JPanel();

westEast.setLayout(new GridLayout(6,1) );

east.add("West", westEast);

JPanel centerEast = new JPanel();

centerEast.setLayout(new GridLayout(6,1) );

centerEast.setBackground(new Color(255,255,200));

east.add("Center", centerEast);

timecounter = new JLabel("There are "+TOTAL+" questions in total");

timecounter.setFont(new Font ("Arial",Font.BOLD,16) );

timecounter.setForeground(new Color(0,90,20) );

northEast.add(timecounter);

westEast.add(new JLabel(" "));

String []boxs = {" A ", " B ", " C ", " D "};

for(int i=0; i><boxes.length; i++) {

boxes[i] = new JCheckBox(boxs[i]);

boxes[i].addItemListener(new ItemHandler() );

westEast.add(boxes[i]);

}

westEast.add(new JLabel() );

choose = new JLabel("CHOOSE CORRECT ANSWERS");

choose.setBorder(BorderFactory.createEtchedBorder() );

centerEast.add(choose);

JScrollPane panes[] = new JScrollPane[4];

for(int i=0; i><answerboxes.length; i++){

answerboxes[i] = new JTextArea();

answerboxes[i].setBorder(BorderFactory.createEtchedBorder() );

answerboxes[i].setEditable(false);

answerboxes[i].setBackground(Color.white);

answerboxes[i].setFont(new Font("",0,12) );

answerboxes[i].setLineWrap(true);

answerboxes[i].setWrapStyleWord(true);

panes[i] = new JScrollPane(answerboxes[i]);

centerEast.add(panes[i]);

}

if(TIMELIMIT >0)testresult = new JLabel(studentname+",You have only : "+TIMELIMIT+" minutes to complete.");

else testresult = new JLabel("There is no time limit for this test");

testresult.setBorder(BorderFactory.createEtchedBorder() );

centerEast.add(testresult);

add(west);

add(east);

}

}

/**

** ActionHandler class to handle all the action events from the buttons.

**/

class ActionHandler implements ActionListener{

/* actionPerformed method */

public void actionPerformed(ActionEvent evt){

String source = evt.getActionCommand();

if(source.equals("start:")){

choice_button[1].setVisible(false);

start_test=true;

allowRestart=true;

if(TIMELIMIT >0)new Timer(); // inner Timer class

panelSouth.remove(choice_button[1]); //start

displayquestion();

}

if(start_test){

if(source.equals("previous:")) {

recordanswer();

quesnum--;

if(quesnum == -1) quesnum=TOTAL-1;

checkteststatus();

displayquestion();

}

if(source.equals("next:")) {

recordanswer();

quesnum++;

if(quesnum == TOTAL-1) finishtest=true;

if(quesnum == TOTAL) quesnum=0;

checkteststatus();

displayquestion();

}

if(source.equals("finish:")) {

if (finishtest){

recordanswer();

quesnum = 0;

choice_button[4].setBackground(Color.lightGray);

timecounter.setForeground(Color.blue);

timecounter.setFont(new Font ("Arial",0,14) );

start_test=false;

check_answer=true;

panelSouth.add(choice_button[0]);

mark_ques();

displayquestion();

checkteststatus();

calculateResult();

}

else JOptionPane.showMessageDialog(null,"Cycle through all questions before pressing finish",

"User Message",JOptionPane.INFORMATION_MESSAGE);

}

}

if (check_answer){

if(source.equals("check next:")) {

quesnum++;

if(quesnum == TOTAL) quesnum=0;

mark_ques();

displayquestion();

checkteststatus();

}

if(source.equals("check previous:")) {

quesnum--;

if(quesnum == -1) quesnum=TOTAL-1;

mark_ques();

displayquestion();

checkteststatus();

}

}

validate();

}

/* Timer class */

class Timer extends Thread implements Runnable{

public Timer(){

new Thread(this).start();

}

public void run() {

while(start_test){

try {

Thread.sleep(1000);

seconds++;

if(seconds % 60 == 0 && seconds != 0){

seconds -= 60;

minutes++;

}

timecounter.setText("Time Counter: "+minutes+" mins : "+seconds+" secs ");

if(minutes==TIMELIMIT){

start_test=false;

endTest();

}

}

catch(InterruptedException ex) { System.out.print(ex); }

}

}

}

/* checkteststatus method */

public void checkteststatus(){

if((quesnum == TOTAL-1)&&(start_test))choice_button[3].setBackground(Color.green);

else choice_button[4].setBackground(Color.lightGray);

if(answers[quesnum].length() >0){

for(int i=0; i<answers[quesnum].length(); i++)

boxes[Integer.parseInt(answers[quesnum].substring(i,i+1) )-1].setSelected(true);

}

else for(int i=0; i><boxes.length; i++)boxes[i].setSelected(false);

}

/* displayquestion method */

public void displayquestion(){

int j = quesnum+1;

pane.setText(QuestionSeries.question[quesnum]);

if(start_test)message.setText("Question "+j+" out of "+TOTAL);

for (int i=0; i><4; i++)answerboxes[i].setText(QuestionSeries.answers[quesnum][i]);

if(start_test){

String temp="";

if(QuestionSeries.n[quesnum]==1) temp="<html> Choose only <b>ONE</b> Option</html>";

else if(QuestionSeries.n[quesnum]==2) temp="<html> Choose <b>TWO</b> Options</html>";

else if(QuestionSeries.n[quesnum]==3) temp="<html> Choose <b>THREE</b> Options</html>";

else temp="<html> <b>ALL are true</b> true</html>";

choose.setText(temp);

}

else {

timecounter.setText("Your choices are shown in the boxes");

choose.setText("Correct answers are marked in light red.");

}

}

/* record answer method */

public void recordanswer(){

String tmp = "";

for(int i=0; i<boxes.length; i++) if(boxes[i].isSelected() ) tmp +=i+1;

answers[quesnum] = tmp;

}

/* endTest method */

public void endTest(){

message.setText("TIME OVER: please press 'finish'");

choice_button[2].setEnabled(false);

choice_button[3].setEnabled(false);

choice_button[4].setEnabled(true);

}

/* mark_ques() method to highlight correct answers */

public void mark_ques(){

for(int i=0; i><answerboxes.length; i++) answerboxes[i].setBackground(Color.white);

for(int i=0; i><QuestionSeries.choice[quesnum].length(); i++)

answerboxes[Integer.parseInt(QuestionSeries.choice[quesnum].substring(i,i+1))-1].setBackground(Color.red);

if(QuestionSeries.choice[quesnum].equals(answers[quesnum])) message.setText("Answer correct, well done!");

else message.setText("Sorry, you got this one wrong.");

}

public void calculateResult(){

mark=0;

double temp=0.0;

java.text.DecimalFormat df = new java.text.DecimalFormat("#0.#");

for(int i=0; i><TOTAL; i++)if(QuestionSeries.choice[i].equals(answers[i]))mark++;

temp=(double)mark;

if(temp/TOTAL*100 >=PASS) testresult.setText(" Well done "+studentname.substring(0,studentname.indexOf(' ') )+", you passed");

else testresult.setText(" Better luck next time "+studentname.substring(0,studentname.indexOf(' ') ) );

student.setText(" Final score for "+studentname+": "+mark+" out of "+TOTAL+": "+df.format(temp/TOTAL*100)+"%");

new Resultwindow().show();

}

}

/* Resultwindow class */

class Resultwindow extends JFrame{

Resultwindow() {

super( studentname+" results: " +(mark*100/TOTAL >=PASS?"PASS":"FAIL") );

Container cont = getContentPane();

cont.setLayout(new GridLayout(TOTAL/2+3,5,2,5) );

cont.setBackground(new Color(255,220,255) );

cont.add(new JLabel(" "+"Marks:"+mark+"/"+TOTAL+": "+"Percentage: "+(mark*100/TOTAL)+"%") );

for(int i=0; i<3; i++)cont.add(new JLabel() );

String temp[] = new String[TOTAL];

for(int i=0; i<TOTAL; i++){

if(QuestionSeries.choice[i].equals(answers[i])) temp[i]="correct";

else temp[i]="wrong";

}

for(int i=0; i><TOTAL; i++) cont.add(new JLabel(" Question "+(i+1)+": "+temp[i]) );

pack();

setLocation(200,200);

}

}

/* ItemHandler class */

class ItemHandler implements ItemListener{

public void itemStateChanged(ItemEvent evt){

if(start_test){

for(int i=0; i><boxes.length; i++) if(boxes[i].isSelected() ) itemCheck++;

if(itemCheck > QuestionSeries.n[quesnum]){

java.awt.Toolkit.getDefaultToolkit().beep();

if(QuestionSeries.n[quesnum]==1)JOptionPane.showMessageDialog(null,"<html><font size='4' color='00308a'><center>"+

"There is only "+QuestionSeries.n[quesnum]+" possible<br> answer to question "+(quesnum+1)+

"<html>","User Information Message",JOptionPane.INFORMATION_MESSAGE);

else JOptionPane.showMessageDialog(null,"<html><font size='4' color='00308a'><center>"+

"There are only "+QuestionSeries.n[quesnum]+" possible<br> answers to question "+(quesnum+1)+

"<html>","User Information Message",JOptionPane.INFORMATION_MESSAGE);

}

itemCheck=0;

}

}

}

/* main method */

public static void main(String [] args){

OnlineTest frame = new OnlineTest();

frame.setTitle(""+QuestionSeries.testtitle);

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.setVisible(true);

}

}

[/nobr]

jofin123a at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thank you very much, good program. I will refrence your program you have here. If i have any questions, i will post. =)
Vausta at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 5

you do have some errors:

"AnAQuiz.java": illegal start of expression at line 42, column 5

"AnAQuiz.java": ';' expected at line 42, column 13

"AnAQuiz.java": illegal start of expression at line 66, column 5

"AnAQuiz.java": ';' expected at line 79, column 6

"AnAQuiz.java": '}' expected at line 81, column 2

"QuestionSeries.java": generics are not supported in -source 1.2 (try -source 1.5 to enable generics) at line 86, column 84

"QuestionSeries.java": illegal start of type at line 86, column 85

"QuestionSeries.java": > expected at line 88, column 38

"QuestionSeries.java": > expected at line 163, column 53

"QuestionSeries.java": > expected at line 173, column 59

"QuestionSeries.java": > expected at line 302, column 50

"QuestionSeries.java": illegal start of type at line 311, column 26

"QuestionSeries.java": > expected at line 311, column 27

"QuestionSeries.java": > expected at line 346, column 51

"QuestionSeries.java": ']' expected at line 347, column 55

"QuestionSeries.java": > expected at line 347, column 72

"QuestionSeries.java": > expected at line 358, column 38

"QuestionSeries.java": > expected at line 382, column 46

"QuestionSeries.java": > expected at line 393, column 45

"QuestionSeries.java": Package C:\Documents and Settings\redwall\jbproject\AnAQuiz\src\anaquiz\QuestionSeries.java stated in source unnamed package does not match directory C:\Documents and Settings\redwall\jbproject\AnAQuiz\src\anaquiz\QuestionSeries.java.

Vausta at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hey how did you compile your program?i guess you i have tried with javac QuestionSeries.java am i correct?Please try with javac OnlineTest.java
jofin123a at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 7
the program which i use to compile your program is J-Bulder 2005 Foundation.
Vausta at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 8
Hey i have not worked with J-Builder, But my program is working fine in the Command mode.If you want try it out. Otherwise go through the code and get some idea.Thank you
jofin123a at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...
# 9

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 = "Wilson";

private String Answer2 = "Ace";

private String Answer3 = "Yes";

private String input = " ";

boolean Answer = false;

JLabel testresult;

public AnAQuiz() {

choice = new JComboBox(); //Step 1

label = new JLabel(); //Step2

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1"); //step 3

choice.addItem("Tennis Question #2"); // Step 3

choice.addItem("Tennis Question #3"); //step 3

text = new JTextField(42); //step 4

Listener listen = new Listener();

choice.addActionListener(listen);

add(choice);

add(label);

add(text);

}

private class Listener implements ActionListener {

public void actionPerformed(ActionEvent event ) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText("Whats the famous tennis brand that begins with 'W'?");

if (input.equals(Answer1))testresult = new JLabel("Correct!");

else testresult = new JLabel("Incorrect!");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

}

}

}

//--

public static void main(String[] args)

{

JFrame frame = new JFrame("Quiz");

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.show();

}

}

here i coded it!

Help needed still!

Message was edited by:

Vaust

Message was edited by:

Vaust

Vausta at 2007-7-9 21:13:40 > top of Java-index,Desktop,Core GUI APIs...