Working with GUI (Graphical User Interface) in Java !

In this article, I have given a very simple program that calculates the simple interest using principal amount, rate of interest and time. All the three parameters are taken as input during run-time. Instead of taking it through the command line, I have made use of a simple GUI. ( GUIs are always attractive, isn’t it ?) !

import javax.swing.JOptionPane;

public class SimpleInterest
{
        public static void main( String [] args )
        {
                int principalAmount, time;
                double rateOfInterest;
                double si;
                String paInput = JOptionPane.showInputDialog( null, "Enter the Principal Amount" );
                principalAmount = Integer.parseInt( paInput );
                String roi = JOptionPane.showInputDialog( null, "Enter Rate of Interest" );
                rateOfInterest = Double.parseDouble(roi);
                String t = JOptionPane.showInputDialog( null, "Enter the time" );
                time = Integer.parseInt(t);
                si = ( principalAmount * rateOfInterest * time ) / 100;
                String Output = "The simple interest is INR." + si ;
                JOptionPane.showMessageDialog( null, Output );
        }
}

Compile the code:

$javac SimpleInterest.java

Execute the code:

$java SimpleInterest

Output:

Java-GUI-1-Subhash Programming-Classes-Best-Java-Training-Institute

Java-GUI-2-Subhash Programming-Classes-Best-Java-Training-Institute

Java-GUI-3-Subhash Programming-Classes-Best-Java-Training-Institute

Java-GUI-4-Subhash Programming-Classes-Best-Java-Training-Institute

Java-GUI-5-Subhash Programming-Classes-Best-Java-Training-Institute

‘JOptionPane’ is one of the many pre-defined classes in the Java Library. We can use the ‘showMessageDialog( )’ method to display any text in a message dialog box. The ‘showInputDialog( )’ method can be used to display an input dialog box. This method returns a string which can later be converted to integer or double or any other data type using ‘Integer.ParseInt( )’ or ‘Double.ParseInt( )’ as shown in the Java Program above. The above program was executed on Mac OS X-10.10. You might have a different dialog box design if you try on any other machine.

About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java and Linux Programming. 

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.