Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA code:: To gain some experience using objects, let's play with a really simple Graphical User Interface. WidgetView is based on a Java technology called

JAVA code:: To gain some experience using objects, let's play with a really simple Graphical User Interface. WidgetView is based on a Java technology called Swing. WidgetView.java is available for your inspection, but I advise against it: sort of like legislation and sausage, some things shouldn't be examined too closely. It also uses parts of the Java language we won't cover in this course. And, depending on the specifics of our course and when you take it, we might use a different technology if we cover GUIs in depth later on.

In section 2.3 we learned how to read console input using a Scanner. Using a Scanner requires setting things up using a rather odd looking statement

Scanner input = new Scanner(System.in);

When the new in this statement is executed, Java creates something that didn't exist before. In this case, it creates a Scanner object. We'll discuss this new operator in much, much, much more detail later in the course, but for now, we can use this new operator to create things other than Scanners. In particular, we'll need it to build a WidgetView Graphical User Interface to show to the user.

Extract the Java source files from the attached archive and import them into your IDE.

These examples gradually build a fairly robust program that displays GUI elements (commonly called widgets) to a user, allows the user to enter data, and allows our program to read the user's input, process it, and display our results to the user. The widgets we'll use with WidgetView are labels (JLabel), text fields (JTextField) and buttons (JButton). You'll notice that the new operator is used a lot. When we want to add a widget to our GUI, we have to create it with new first. In fact, we even have to create our GUI using new before we can use it--notice the

WidgetView wv = new WidgetView():

statement comes first.

Walk through the Student examples (starting with Student1), running and studying each one before moving to the next. They will show how WidgetView objects can be created using new, and how the add and addAndWait methods work. JTextFields (javax.swing.JTextField) and JButton (javax.swing.JButton) and JLabel (javax.swing.JLabel) are Swing classes provided by Oracle. Notice the import statements in the Student examples. You can look up the complete documentation for these classes by googling "oracle java api JTextField," for example. But the most important methods to use with these objects are shown in the Student examples. We can use new to create a JTextField with a set number of columns, or an initial String to display. We can set the text of a field (the setText method), or get the current text in a field (the getText method). Pay attention to the order that we create an object, set its data, and add it to our GUI.

WidgetView's addAndWait method allows our program to receive user input. It adds a widget to the GUI (much like the add method), but does something more. It waits for the user to do something. Similar to Scanner's next, nextInt and nextDouble methods, the user program stops until some external condition is satisfied. In Scanner's case, the program waits until there is a word or int or double value available from the keyboard. In WidgetView's case, it waits on the user to click a button or put the cursor in a specific text field and press the enter key.

The StudentOrderingNumbers program shows how a program can ask for input (three int values) and identify the largest, middle and smallest numbers of the input. It then displays these results to the user. StudentOrderingNumbersAndMore also shows how relational operators evaluate to a boolean value, and how to display boolean values.

Here are some rules of thumb for a WidgetView program:

Create exactly one WidgetView object. That means have only one statement like WidgetView wv = new WidgetView();

You'll create as many JLabel and JTextField objects as you need, and use each one for only one purpose. For example, if you want to give the user one instruction and two items to input, you might have something like

JLabel askForNumbers = new JLabel("Enter two numbers");

JTextField oneNumber = new JTextField(5);

JTextField otherNumber = new JTextField(5);

You'll probably need only one JButton, and use addAndWait to add it to the GUI.

After the addAndWait returns, you'll read your user input from the JTextFields and do your processing. You'll then create one or more new JLabel objects and use the add method to display the results to the user.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions