Question
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 newbefore 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 addand 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 Stringto display. We can set the text of a field (the setText method), or get the current text in a field (the getTextmethod). 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.
This program implements a flash card to teach multiplication.
Write a program that uses a WidgetView object (the WidgetView class is available elsewhere in this Lesson).
your program should use a Random object to generate two random numbers between 0 and 9 (inclusive).
To explain the operation of this program, we'll assume that our random number generator generated 6 and 3.
display a JLabel with the text "What is 6 times 3?"
create an empty JTextField to hold the user's answer
create a JButton that has the text "click after answering"
The user should put his or her guess in the JTextField and click the JButton.
When the button is clicked, the program should get the text from the JTextField, convert it from String to int, and create a JLabel that says either
That's right. Good Job, or
Sorry, the correct answer is 18
depending on whether the user input the correct number (18 in this case).
Write a program that uses a WidgetView object to do the following:
Generate two random integers between 1 and 9 (inclusive). Name one of them x, the other y. Display them to the user using JLabel objects.
Create a JLabel object displaying the text "Enter an operation number."
Create a JTextField for the user's input.
Create a JButton displaying the text "Press here when you've entered your operation." Use addAndWait to add it to the WidgetView object.
When the user clicks the JButton, evaluate operation in the following order to determine the one and only mathematical operation to perform on x and y. Use a JLabel to display the result.
If operation is between 1 and 10 inclusive, add x and y.
If operation is evenly divisible by 4, subtract y from x.
If operation is evenly divisible by 5, use integer division to divide y into x.
If operation is an even number, use floating point division to divide y into x.
If none of the other tests on operation apply, multiply x and y.
Note: Operation can be negative or zero.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started