Presentation is loading. Please wait.

Presentation is loading. Please wait.

CREATING A SIMPLE GUI Mr. Crone. First GUI Today we will use Swing to create the following application:

Similar presentations


Presentation on theme: "CREATING A SIMPLE GUI Mr. Crone. First GUI Today we will use Swing to create the following application:"— Presentation transcript:

1 CREATING A SIMPLE GUI Mr. Crone

2 First GUI Today we will use Swing to create the following application:

3 Creating the Source Code Create a new class called SwingDemo Import the entire Swing Package import javax.swing.*; public class SwingDemo{ }

4 Creating the Source Code Create a Constructor with no parameters Create a main method import javax.swing.*; public class SwingDemo{ public SwingDemo(){ } public static void main(String args[]){ }

5 Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value }

6 Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Stops the program from running after closing the window Jlabel jlab = new Jlabel(“Swing powers the modern Java GUI”); }

7 Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Stops the program from running after closing the window JLabel jlab = new JLabel(“Swing powers the modern Java GUI”); jfrm.getContentPane().add(jlab); //add label to the content pane jfrm.setVisible(true); //Show the container }

8 Creating the Source Code Inside the main method: public static void main(String[] args){ public void run(){ new SwingDemo(); }

9 Creating the Source Code Inside the main method: public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingDemo(); } }); }

10 Complete Source Code import javax.swing.*; public class SwingDemo { public SwingDemo(){ JFrame jfrm = new JFrame("A Simple Swing Program"); // top - level container jfrm.setSize(275, 100); // Set the size jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // stop program when closed JLabel jlab = new JLabel("Swing powers the modern Java GUI"); // Create a new Label jfrm.getContentPane().add(jlab); // Add the Label to the Content Pane which is contained within the JFrame jfrm.setVisible(true); // Set the visibility to True } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingDemo(); } }); }

11 The main Method Explained The source code within the main method causes a SwingDemo object to be created on the event-dispatching thread rather than on the main thread To prevent problems with multiple threads, we launch the program from the event-dispatching thread The GUI can be launched from the main method using the main thread, however this will not work with every GUI To remain safe and compatible with all GUI’s, we will continue to use the event-dispatching thread

12 References: Swing: A Beginner’s Guide (Osborne Mcgraw Hill) - Herbert Schildt


Download ppt "CREATING A SIMPLE GUI Mr. Crone. First GUI Today we will use Swing to create the following application:"

Similar presentations


Ads by Google