-
Notifications
You must be signed in to change notification settings - Fork 0
GUI
GUI stands for graphic user interface. A GUI is used every day to allow the user to interact with a program. Everything you do on the web is most likely done through a GUI. A GUI makes any program easier to look at and makes it more approachable from a user perspective. Just think about how unappealing Facebook would look if there we no graphical elements like search boxes, or if you had to do everything through the command line.
Unlike accepting user input through the command line you do not need to use a Scanner class to accept input. Instead, you will use the JOptionPane class. Through the JOptionPane class, there are two ways to accept user input. The InputDialog and the ConfirmDialog. The InputDialog is used to accept input such as strings or numbers. The ConfirmDialog asks the user a question and usually accepts Yes, No, or Cancel responses. Just like with the Scanner class you must import the JOptionPane class.
import javax.swing.JOptionPane;
When you use an InputDialog the dialog box is displayed asking the user for input and then providing the user with a dialog box for inputting the data. After the user inputs the data they can click a button that returns the input as a String. To properly do this you will need to use the showInputDialog() method. Below is code that asks the user for their name.
import javax.swing.*;
public class GUI {
public static void main(String args[]){
String userName = "";
userName = JOptionPane.showInputDialog(null, "What is your name?");
JOptionPane.showMessageDialog(null, "Hello " + userName + "!");
}
}
Output will place screenshot here
As you can see the code for accepting the input is simple. There is also a more advanced showInputDialog method. This method accepts four arguments, instead of the normal two.
- The parent component This value sets where the box will be displayed on the screen. If this is null then the box appears in the center of the screen.
- The message the user will see. This is what the user will see displayed in the dialog box
- The title This will become the title of the dialog box
- The type field This describes what type of dialog box it is. These are, ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE. Each of these messages also gives the dialog box a different icon.
userName = JOptionPane.showInputDialog(null, "What is your name?", "Who are you"
, JOptionPane.QUESTION_MESSAGE);
Output place screenshot here
Input you may receive may not just be strings. Sometimes you will need to accept numbers in order to do arithmetic with them. In order to do this, you must first parse the Strings that the input dialog returns. Parsing means you are converting the string to its numeric equivalent. In order to parse a string, you must use the parse method follow by the data type you wish to parse it too. Let's say you have a program to calculate hours a student spent studying in a week.
//declaring strings to store student working hours and days they worked
string studentHours, studyDays;
//int to store days
int days = 0;
//double to store hours
double hours = 0.0;
//double variable to store total hours studied
double totalStudy = 0.0;
//asks student how many hours they studied
studentHours = JOptionPane.showInputDialog(null, "How many hours did you study per day?", "Hours"
, JOptionPane.QUESTION_MESSAGE);
//parses it into a double
hours = Double.parseDouble(studentHours);
//asks student how many hours they studied
studyDays= JOptionPane.showInputDialog(null, "How many days did you study?", "Study Hours"
, JOptionPane.QUESTION_MESSAGE);
//parse it into an integer
days = Integer.parseInt(studyDays);
//calculates the totalStudy hours
totalStudy = hours * days
//outputs the information to the user
JOptionPane.showMessageDialog(null, "You studied " + days + " and " +
hours + " each day " + " which is " + totalStudy + " hours in total" );
Output
Place screenshots here
Parsing strings allows you to be able to use the input dialogs them in a lot more ways. Now instead of just using them for String handling, you can use them to handle numbers as well.
Sometimes it is not always necessary to accept full strings or numbers from the user. Sometimes all you need is a simple yes or no from the user. To accomplish this you can use a confirm dialog box. The confirm dialog works similar to the input dialog, however, instead of returning a string value it returns an integer value based on the selection. Let's say you want to ask a user if they were over 18.
//stores the user choice
int choice = 0;
//boolean to set to the users choice
boolean isYes;
//sets the choice equal to the users choice
choice = JOptionPane.showConfirmDialog(null, "Are you over 18?");
//sets boolean to true if the user answers yes
isYes = (choice == JOptionPane.YES_OPTION);
//outputs the result
JOptionPane.showMessageDialog(null, "You responed " + isYes);
Output(If yes is chosen)
Output(If no is chosen)
Just like the input dialog, the confirm dialog has an advanced form as well.
- The parent component This value sets where the box will be displayed on the screen. If this is null then the box appears in the center of the screen.
- The message the user will see. This is what the user will see displayed in the dialog box
- The title This will become the title of the dialog box
- An integer value the tells what type of dialog it is This can be either YES_NO_CANCEL_OPTION or YES_NO_OPTION
- The type field This describes what type of dialog box it is. These are, ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE. Each of these messages also gives the dialog box a different icon.
JOptionPane.showInputDialog(null, "Are you over 18", "Prompt 1", JOptionPane.YES_NO_OPTION
, JOptionPane.QUESTION_MESSAGE);