Results 1 to 2 of 2
  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default Help with this code

    Hi, I am writing a Java application and i need some help with the following: I have a JTextField called 'answer' and a JButton called 'submit'. What is supposed to happen is when i click the JButton, it checks to see if the letter 'A' is written in the JTextField. If it is, then a wav sound is played. If not, a message is outputed on the screen. Please note that I have tested the wav sound by clicking the JButton and it works. It's just when i use the if else structure that it doesn't work. Also, how do I separate JLabels? I wish to have them in the centre of the screen and aligned. I'm using the BorderLayout.Center. For example, one sentence is on one line,then there should be a gap of 2 or 3 lines, and then the next sentence will appear. The code is posted below.

    Help is greatly appreciated.


    //------------------------------------------------------------------------

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class QuestionsAnswers extends JFrame
    {


    public QuestionsAnswers()
    {



    JPanel content = (JPanel)getContentPane();

    /* Set JFrame Properties */
    setSize(1000, 700);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.red); // Sets frame color to grey
    setVisible(true);
    setTitle("Java123 - Questions and Answers");

    JPanel panel1 = new JPanel( );
    JPanel panel2 = new JPanel( );
    JPanel panel3 = new JPanel( );





    /* Question Label */
    JLabel question = new JLabel( );
    question.setText("Java is ..... ");
    question.setSize(700,400);
    panel1.add(question);

    /* Option Labels */
    JLabel optionA = new JLabel( );
    optionA.setText("A. an object-oriented programming language");
    optionA.setSize(300,25);
    panel2.add(optionA);


    JLabel optionB = new JLabel( );
    optionB.setText("B. a structured programming language");
    optionB.setSize(300,25);
    panel2.add(optionB);


    JLabel optionC = new JLabel( );
    optionC.setText("C. a procedural programming language");
    optionC.setSize(300,25);
    panel2.add(optionC);



    /* Answer TextField */
    final JTextField answer = new JTextField( );
    answer.setColumns(5);
    panel3.add(answer);


    final String checkAnswer = "A";


    /* Submit Answer Button */
    JButton submit = new JButton("Submit Answer");
    submit.setSize(80,30);
    panel3.add(submit);



    /* Add to content pane */
    content.add(panel1, BorderLayout.NORTH);
    content.add(panel2, BorderLayout.CENTER);
    content.add(panel3, BorderLayout.SOUTH);

    /* Menu Events caused by user clicks */
    // --------------------------------------------------------------------------------

    submit.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {


    if (checkAnswer != answer.getText())
    {

    System.out.println("Incorrect!");

    }

    else
    {

    IntroWave theThread = new IntroWave("Audio\\JMF.wav");
    theThread.start();

    }




    }

    });
    // --------------------------------------------------------------------------------


    }


    public static void main(String[] args)
    {
    QuestionsAnswers start = new QuestionsAnswers();
    start.setVisible(true);
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Need help with my code.
    By stormviper in forum New To Java
    Replies: 8
    Last Post: 11-18-2013, 03:58 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  3. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 02:02 AM
  4. help with oop code
    By nhlfan in forum New To Java
    Replies: 1
    Last Post: 11-27-2007, 08:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •