Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts
Friday, October 28, 2011 3 comments

WALKTHROUGH:TNB Java Tut 50 -"Graphical User Interface GUI" [VIDEO] [SOURCE CODE]

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:

    Photobucket

    Step 2. Declare a class and a main method:


    Step 3.Create 2 String variable and set them equal to a showInputDialog:



    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:



    Step 6. Create a showMessageDialog that will display the sum of the two numbers in sentence form:

    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
     
    ;