GUI with Try Catch

Posted on 2006-05-03
Last Modified: 2010-03-31
I have to write a program, it must be GUI, employ at least one try-catch block, have two distinct interactions witha at least 2 questions each. There must be an OK and ESit button.
Program ask user "Do you like sports?" User answer Y or N (if anything else is entered program needs to catch it and reply with error message and allow interaction to continue.) Question 2 is based on answer from one. If yes then"Is it hockey?", if no Then
Program then asks user "Do you like music?"
if user says Y
Then program asks "Is it jazz?"
I can't even get the GUI right to try to write the rest. Please help.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class week5_3 extends JFrame
      private JTextField inputTF;
      private JButton okayB, exitB;

      private OkayButtonHandler obHandler;
      private ExitButtonHandler ebHandler;
      private static final int WIDTH = 400;
      private static final int HEIGHT = 300;
      public week5_3()
                              //Create text field
            inputTF = new JTextField(10);
                              //Create Okay Button
            okayB = new JButton("Okay");
            obHandler = new OkayButtonHandler();
                              //Create Exit Button
            exitB = new JButton("Exit");
            ebHandler = new ExitButtonHandler();
                              //Set the title of the window
            setTitle("Week 5 Assignment 3");
                              //Get container
            Container pane = getContentPane();
                              //Set the layout
            pane.setLayout (new GridLayout(2, 2));
                              //Place the components in the pane
            private class OkayButtonHandler implements ActionListener
                  public void actionPerformed(ActionEvent e)

            private class ExitButtonHandler implements ActionListener
                  public void actionPerformed(ActionEvent e)
            public static void main(String[] args)
                        week5_3 rectObject = new week5_3();
Question by:sendhelp
    LVL 86

    Accepted Solution

    You could just use JOptionPane.showInputDialog - that's a GUI ;-)
    LVL 19

    Expert Comment

    by:Jim Cakalic
    You need to call pack() and show() on rectObject after it is created in main.
    LVL 30

    Expert Comment

    :-) I like CEHJ's idea.
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now