Simple Java GUI (Graphical User Interface)

Simple Java GUI:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

In this blog, we will learn about how to create a simple input dialog box in Java. For this we need to import the library javax.swing.JOptionPane. [Solve JOptionPane error in Eclipse]

Example 1- Input Value is String.

Dialog Box Input Value is String Java
package com.java_tutorial;

import javax.swing.JOptionPane;

public class Gui_example {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	     String web_name = JOptionPane.showInputDialog("Enter Your Website Name");
	     JOptionPane.showMessageDialog(null, "Hi.... " + web_name);
	
  }

}

Output:

string dialog box output java
string dialog box output in java

Example 2- Input Value is Integer.

Dialog Box Input Value is Integer Java
package com.java_tutorial;

import javax.swing.JOptionPane;

public class Gui_example {

	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		
	     int web_traffic = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Website Traffic"));
	     JOptionPane.showMessageDialog(null, "Your Website Traffic Is.... " + web_traffic);
	
  }

}

Output:

add integer value output java
integer value output java

Example 3- Input Value is Double.

Input Value is Double Java
package com.java_tutorial;

import javax.swing.JOptionPane;

public class Gui_example {

	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		
	     double web_speed = Double.parseDouble(JOptionPane.showInputDialog("Enter Your Website Speed"));
	     JOptionPane.showMessageDialog(null, "Your Website Speed Is.... " + web_speed+ " ms");
	
  }

}

Output:

Input Value Double Output Java
Input Value Double Output in Java

Simple Java GUI Source Code: When you run the code it asks you for input one by one.

package com.java_tutorial;

import javax.swing.JOptionPane;

public class Gui_example {

	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		
	     
		 String web_name = JOptionPane.showInputDialog("Enter Your Website Name");
	     JOptionPane.showMessageDialog(null, "Hi.... " + web_name); 
		
		 int web_traffic = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Website Traffic"));
	     JOptionPane.showMessageDialog(null, "Your Website Traffic Is.... " + web_traffic);
		
		 double web_speed = Double.parseDouble(JOptionPane.showInputDialog("Enter Your Website Speed"));
	     JOptionPane.showMessageDialog(null, "Your Website Speed Is.... " + web_speed+ " ms");
	
  }

}

Python Bike Rental System
Python GUI Calculator Project
Create Youtube Video Downloader Using Python
Create MySQL Database and Connection in Python
Rock Paper Scissors Game in Python (Using Random Module)
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply