CS3340:  Intro OOP and Design

Creating a GUI Application

 

 

Method - using EventQueue and Runnable to launch the main GUI -JFrame

import java.awt.EventQueue;
import javax.swing.JFrame;

public class SimpleEx extends JFrame {

    public SimpleEx() {

        initUI();
    }

 

//method to setup UI
    private void initUI() {
       //THIS is where you would add new components to this JFrame

        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

 

   //main method of Application, creates instance of this class inside run method of anonymous Runnable
   // instance and Launched via EventQueue

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {      //here we are defining anonymous inner class
       
            @Override
            public void run() {
                SimpleEx ex = new SimpleEx();
                ex.setVisible(true);
            }
        });
    }

  }

NOW you can put components in your JFrame--below adding a button

private void initUI() {

        JButton quitButton = new JButton("Quit");

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        createLayout(quitButton);

        setTitle("Quit button");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    //method to setup the Layout
   private void createLayout(JComponent... arg) {
        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );
    }



 

see more elaborate example

 

 

Method2 - Same as above (using EventQueue and Runnable to launch the main GUI -JFrame) --BUT make the JFrame a different class

 

class SimpleEx {****} //same as above but, NO main method

class MainApplicationClass {

     public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
       
            @Override
            public void run() {
                SimpleEx ex = new SimpleEx();
                ex.setVisible(true);
            }
        });

     }

}

 

Method 3 - have MainApplication class contain an Instance of JFrame (or class you created that descends from JFrame) inside the run method of an anonymous inner instance of Runnable that is again passed to the EventQueue to invoke

NOTE: this is how the Eclipse WindowBuilder gui tool does --this is its autogenerated code

 

import java.awt.EventQueue;

import javax.swing.JFrame;

 

public class MainApp {

                private JFrame frame;

                /**
                 * Launch the application.
                 */
                public static void main(String[] args) {
                                EventQueue.invokeLater(new Runnable() {
                                                public void run() {
                                                                try {
                                                                                MainApp window = new MainApp();
                                                                                window.frame.setVisible(true);
                                                                } catch (Exception e) {
                                                                                e.printStackTrace();
                                                                }
                                                }
                                });
                }

                /**
                 * Create the application.
                 */
                public MainApp() {
                                initialize();
                }

                /**
                 * Initialize the contents of the frame.
                 */
                private void initialize() {
                                frame = new JFrame();
                                frame.setBounds(100, 100, 450, 300);
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                                //add Code here to customize your JFrame --create components and add them to this JFrame
                }

}

 

 

© Lynne Grewe