This presentation is a tutorial of how to design a JFrame
window which contains a JButton.
When the user clicks on the JButton, a message dialog appears.
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
A Simple Java GUI Application.pptx
1. A Simple Java GUI Application:
In this tutorial we will design a JFrame
window which contains a JButton.
When the user clicks on the JButton
a message dialog appears.
2. A Simple Java GUI Application:
Steps:
1. Start the Eclipse IDE.
2. Create a Java Project with any name, for example
GUIJavaProject.
3. Inside the GUIJavaProject, create a class with any name,
for example GUIJavaClass, with a main method inside
the class.
3. A Simple Java GUI Application:
4. Make the GUIJavaClass class extends the JFrame class.
5. Make the GUIJavaClass class implements the
ActionListener interface.
Additional import statements are required to define:
• JFrame class
• ActionListener interface.
5. A Simple Java GUI Application:
6. Inside the GUIJavaClass class add the followings:
• Definition for the JButton object (B1).
• Definition for an empty constructor method.
• Definition for the method actionPerformed method.
Additional import statements are required to define:
• JButton class
• ActionEvent class
6. public class GUIJavaClass extends JFrame implements ActionListener
{
private JButton B1 ;
public MyClass( )
{
}
public void actionPerformed ( ActionEvent e )
{
}
public static void main ( String [ ] args )
{
}
}
7. A Simple Java GUI Application:
7. Inside the empty constructor method add the following
code.
Additional import statement is required to define the
FlowLayout class.
8. public GUIJavaClass( )
{
//setLayout: sets the size and position of the JButton within the JFrame.
setLayout(new FlowLayout());
//Create the JButton (B1) with the statement "Click" written inside JButton.
B1 = new JButton ("Click");
/*
Add action listener to (B1) to listen to event from it.
event is triggered here if user click on (B1).
*/
B1.addActionListener(this);
//Add (B1) to the JFrame.
add (B1);
}
9. A Simple Java GUI Application:
8. Inside the actionPerformed method add the following
code.
Additional import statement is required to define the
JOptionPane class.
10. public void actionPerformed( ActionEvent e )
{
/*
If user clicks on (B1) the message dialog with the message
"Button was clicked" appears.
*/
if ( e.getSource() == B1 )
JOptionPane.showMessageDialog( this, "Button was clicked");
}
11. A Simple Java GUI Application:
9. Inside the main method add the following code.
12. public static void main( String [ ] args )
{
/* Define and create the GUIJavaClass object (frame),
this will call the empty constructor. */
GUIJavaClass frame = new GUIJavaClass();
//set the title for the JFrame window to "GUI Example".
frame.setTitle("GUI Example");
/* set the width for the JFrame window width to 400,
and height to 500. */
frame.setSize(400, 500);
/* set the location for the JFrame window to 400 on x-axis,
and to 100 to y-axis. */
frame.setLocation(400,100);
/* make the JFrame window close when the user click
on the X button inside the JFrame's title bar. */
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the JFrame window appear to user.
frame.setVisible(true);
}
13. A Simple Java GUI Application:
10. Execute the code.
Inside the JFrame window, when the button is clicked, a
message dialog will appear with the statement “Button
was clicked” inside.