In this walk-through you will create a simple GUI that will use 2 shoInputDialogs and a showMessageDialog to find the sum of two numbers input from the user.
Whats New In This Walk-Through:
- showInputDialog's- A graphical box that users can input data from
- Integer.parseInt - A method used to convert a string number to a int var
- Using showMessageDialog's-A graphical box that can display a message
Step 1. Import javax.swing.JOptionPane:
Step 2. Declare a class and a main method:
Step 4. Create two int variable and set them equal to the Strings we collected from the user via the showInputDialog (you will need to parse the strings before you can set them equal to a int variable):
Step 5.Create a third int var and set it equal to the sum of int var1 and int var2:
TNB Java Tut 50 Source Code:
import javax.swing.JOptionPane;
class apples {
public static void main(String[] args){
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");
int num1 = Integer.parseInt(fn);
int num2 = Integer.parseInt(sn);
int sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The answer is " + sum, "the title", JOptionPane.PLAIN_MESSAGE);
}
}
End of Tutorial