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.

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:


Example 2- Input Value is Integer.

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:


Example 3- Input Value is Double.

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:


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");
}
}
Comments (No)