Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

GUI with Try Catch

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();
  • 2
1 Solution
You could just use JOptionPane.showInputDialog - that's a GUI ;-)
Jim CakalicSenior Developer/ArchitectCommented:
You need to call pack() and show() on rectObject after it is created in main.
Mayank SAssociate Director - Product EngineeringCommented:
:-) I like CEHJ's idea.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now