Basic Tutorial - How To Make Simple GUI Using Java Swing

By @elfida2/9/2018utopian-io

What Will I Learn?

  • You will learn about GUI
  • You will learn about Swing
  • You will learn about Java programming

Requirements

Difficulty

  • Basic

Tutorial Contents

In this post I will explain about making GUI look using java swing. The way to make it too simple, you do not need to design the look of the GUI as do people in general.

Before that you should know what the GUI understanding is. GUI is an interface display, so it can facilitate the user. So, the GUI is a means for the user to be able to communicate with the computer.

To create a GUI, you can use Java Swing. Java Swing is a library in java that serves to create the interface (GUI).

How to make it is like the following steps.

Input command to call Java Swing library, otherwise you will not be able to create GUI.

import javax.swing.JOptionPane;

The next step is to enter the name of the class we want, for example I will give the name javaswing.
The name of this class also serves as the name of the java file that we will save.

public class javaswing 
{

Then, we will enter the command for the main method. This is an obligation for java programming.

public static void main (String[] args) 
{

Next we will declare the name and age variables. Variables serve to accommodate data input. And the data type we use is string, because the data we input is a word or sentence.

String name= " ";
String age= " ";

After making the declaration. We will create the Input Dialog view based on each variable. In this session too, you will be able to start inputting data.

name=JOptionPane.showInputDialog("Submit Your Name = ");
age=JOptionPane.showInputDialog("Submit Your Age = ");

Next, we will declare another variable. The variable we declare contains a sentence for us to display in the output later. Because it contains a sentence, then we use the string data type.

String message="Your Name is " +name +" , And Your Age is " +age;

The last step is to display Message Dialog, here also called the message variable. So it will appear as output.

JOptionPane.showMessageDialog(null,message);
}
}

The result is like the following picture.

  • Inputting name
gambar.png
gambar.png
  • Inputting age
gambar.png
gambar.png
  • Message Dialog View
gambar.png

Here is the complete syntax.

import javax.swing.JOptionPane;
public class javaswing 
{
	public static void main (String[] args) 
	{
		String name= " ";
		String age= " ";
		
		name=JOptionPane.showInputDialog("Submit Your Name = ");
		age=JOptionPane.showInputDialog("Submit Your Age = ");
		
		String message="Your Name is " +name +" , And Your Age is " +age;
		
		JOptionPane.showMessageDialog(null,message);
	}
}



Posted on Utopian.io - Rewarding Open Source Contributors

13

comments