Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting Text to show on GUI

843805Feb 24 2007 — edited Feb 25 2007
I'm about desperate because i spent 3 days on this and i'm am in dire need for help!

1. First i want to create a GUI that it a quiz.
2. Create Questions by using Combo Box
3. Next it creates a text field and allow me to input my answer
4. following that, after the input is inputted, i click a button that says check answer and it will return if it is correct or false.

The problem is that code is so messed up that i don't know where to go.
When i run it, it show me a combo box with 3 questions but before i even answer it, it shows that i'm incorrect.
Thats why I can't really brief wat i need help in. So, could you write a quick code that can summarize the whole thing. You can use my code and fix it up. Thanks very so much!
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 = new JLabel("");
public AnAQuiz() {
        choice = new JComboBox();
        label = new JLabel();
        label.setBackground(Color.blue);
        choice.addItem("Tennis Question #1");
        choice.addItem("Tennis Question #2");
        choice.addItem("Tennis Question #3");
        text = new JTextField(20); //step 4
        Listener listen = new Listener();
        choice.addActionListener(listen);
        setLayout(new BoxLayout(this, 0));
        add(choice);
        add(label);
        add(text);
        
        add(testresult);
}
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 (Answer1.equals(input))testresult.setText("Correct");
                
              else { testresult.setText("Incorrect");
             
              }
                 break;
             case 1:
                label.setText("What do you call when someone misses a serve?");
                if (Answer2.equals(input))testresult.setText("Correct"); 
                    
                
                    
               else { testresult.setText("Incorrect");
               }
                break;
     
             case 2:
                label.setText("Should you shake hands after a match?");
                if (input.equals(Answer3))testresult.setText("Correct");
                 else{

                     testresult.setText("Incorrect");
                 }
                break;
       
            }
      testresult.setVisible(false);
        }
    }
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();
        frame.setVisible(true);
    }


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2007
Added on Feb 24 2007
22 comments
186 views