What Will I Learn?
- You will learn about Java programming
- You will learn about GUI
- You will learn about Swing
Requirements
Difficulty
- Intermediate
Curriculum
Tutorial Contents
This time I will show you a tutorial to create a simple book return form (in GUI). To make it, we still use the Java Swing feature. So we do not need to spend time to design the interface of the form.
Before that, I'll explain again about the GUI. GUI is a display interface in an application to facilitate the user in accessing the computer. GUI can also be interpreted as a bridge to connect users with computers, with the aim that users can communicate with the computer.
Next, you should also know about Swing. Swing is a feature in java programming in order to display GUI look. Without Swing, we can not build a GUI.
So we'll create a book return form with some entries like as
- The borrower name
- The borrowed book title
- The borrowed book code
- The date of the book lending
- The return date of the book
- The amount of the fine
The book return limit is a maximum of 3 days, more than that will be charged a fine of Rp 10000 per day.
For the process, note the following steps:
First, enter the command to call Java Swing Library. This library is very useful for us to build a GUI in java programming.
import javax.swing.JOptionPane;
Next is to enter the class name. The name of this class works as our file name. For now I will name it with returnform.
public class returnform
Next, we will enter the command for the main method. Without the main method, java programming will not work. So this is mandatory in java programming.
public static void main(String[] args)
{
Then, we will do a declaration of variables. Variables serve as a shelter of data we will input. There are several variables that we will declare like as:
- name --> to declare the Input Dialog and save the inputted data in the form of a word or sentence.
- title --> to declare the Input Dialog and save the inputted data in the form of a word or sentence.
- code --> to declare the Input Dialog and save the inputted data in the form of a word or sentence.
- borrow_date --> to declare the Input Dialog.
- value1 --> to save the inputted data in form of integer.
- return_date --> to declare the Input Dialog.
- value2 --> to save the inputted data in form of integer.
In the variable there is also data type, this time we will use String (to store data in the form of word or sentence), and int (to store data in the form of integer) data type.
String name;
String title;
String code;
String borrow_date;
int value1 ;
String return_date;
int value2 ;
int fine;
We go to stage to display Input Dialog with JOptionPane.showInputDialog (""); command. Dialog Input is based on the variables we have declared. With this command too, we will be able to input data in the form of string.
Then, there is also a command like Integer.parseInt (); ,with this command we can input data in the form of integer .
name=JOptionPane.showInputDialog("Name of Borrower: ");
title=JOptionPane.showInputDialog("Title of Book: ");
code=JOptionPane.showInputDialog("Code of Book: ");
borrow_date=JOptionPane.showInputDialog("Date of Borrow (On February): ");
value1=Integer.parseInt(borrow_date);
return_date=JOptionPane.showInputDialog("Date of Return (On February): ");
value2=Integer.parseInt(return_date);
Then we will create a formula to calculate the value of the fine.
fine=(value2-value1-3)*10000;
Then we will make the selection. Where if the book is returned on the third day or less, it will not be penalized. In this session we will also display Message Dialog to show a message that we are not subject to fines.
if(value2<=value1+3)
{
String message=name+" is free from fines";
JOptionPane.showMessageDialog(null,message);
}
This time we will do otherwise, if the book is returned more than 3 days then we will be charged a fine of Rp 10000 per day. Message Dialog will also be displayed in this session. Message Dialog serves to display the amount of penalties earned.
else
{
String message=name+" get a fine of Rp "+fine;
JOptionPane.showMessageDialog(null,message);
}
}
}
Running Test
- For example, I borrowed a book on February 12th. Then I returned the book on February 15th. Therefore, I will not be fined because the return limit is not more than 3 days.
Inputting borrower name.
Inputting title of book.
Inputting code of book.
Inputting date of borrow
Inputting date of return
Message Dialog View
- For other example, I borrowed a book on February 12th. Then I returned the book on February 16th. Then I will be fined, because I borrow for 4 days. So, I will be fined Rp 10000 for more than 1 day from the maximum limit of lending (3 days).
Inputting borrower name
Inputting title of book
Inputting code of book
Inputting date of borrow
Inputting date of return
Message Dialog view
Here is the complete syntax :
import javax.swing.JOptionPane;
public class returnform
{
public static void main(String[] args)
{
String name;
String title;
String code;
String borrow_date;
int value1 ;
String return_date;
int value2 ;
int fine;
name=JOptionPane.showInputDialog("Name of Borrower: ");
title=JOptionPane.showInputDialog("Title of Book: ");
code=JOptionPane.showInputDialog("Code of Book: ");
borrow_date=JOptionPane.showInputDialog("Date of Borrow (On February): ");
value1=Integer.parseInt(borrow_date);
return_date=JOptionPane.showInputDialog("Date of Return (On February): ");
value2=Integer.parseInt(return_date);
fine=(value2-value1-3)*10000;
if(value2<=value1+3)
{
String message=name+" is free from fines";
JOptionPane.showMessageDialog(null,message);
}
else
{
String message=name+" get a fine of Rp "+fine;
JOptionPane.showMessageDialog(null,message);
}
}
}
Posted on Utopian.io - Rewarding Open Source Contributors
Ma girl, you know something?
Really? Thank you brother :v
You love me?
No :v
:(
Alwasy like your post, i swear this poat will be approved😊
Big thanks bro
Your contribution cannot be approved because it does not follow the Utopian Rules.
Violated Rules:
Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.
Same contributions will never be accepted in Utopian twice by the same or different user.
Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.
My Opinion:
You can contact us on Discord.
[utopian-moderator]
I don't understand with this statement,
"Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project."
So, do you mean that my tutorial is trivial?
@yokunjon
Check "my opinion" section. Your older tutorial also covers the same thing.
And, yes, it's trivial. You could show several GUI options in a tutorial, instead of this. And doing it that way is our prefered way. Your posts should cover different technical aspects of that subject. But, you only used "showMessageDialog" and "showInputDialog", and this does not differ from your older tutorial.
Also, Discord is our prefered communication platform. So next time, I would be happy if you use it. Have a nice day :)