Saturday, July 30, 2011

How to create a dialog-based program in Java?

by Vilchor G. Perdido (eturo)


First in our code is to import the package in the Java API where the JOptionPane is. And it is found at the javax.swing package, so will create a code to import this package. It look like this...

import javax.swing.JOptionPane;
or you may write the code above like this;

import javax.swing.*; //note that the asterisk (*) indicate that all the classes and methods inside this package will be automatically used or include by the program


Second, create a code to define the class name (Note: The class name you entered a while ago is always the same the class name you will write in the code.) Below the class declaration, put a paired of open and close curly bracket where we will put the variables and methods later to be used by our program. The whole class is denoted by the light blue color in the code pad in the BlueJ text editor.

import javax.swing.JOptionPane;
public class MyFirstGUIProgram
     your code here
}

 
Third, after defining your class, the next step is to create the main method to be able to execute by the program. Whatever statements you declared inside the main method will be executed. The main method of our program will be indicated by the light yellow color the code pad window. To create the main method of our program, do this inside the class:


import javax.swing.JOptionPane; //package declaration


public class MyFirstGUIProgram //class definition

{

     public static void main(String[] args) //the main method
     {
         your code here
     }
}


Now, we are ready to create the statement that will display a dialog box with a message. To do this, we have the following syntax in creating a message dialog box.

JOptionPane.showMessageDialog(null,"your message here","text in the title bar", type of dialog box ,);

Explanation:

 


So, in our code:


Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...