Searching...
Sunday, June 1, 2014

Swing JFrame -2

June 01, 2014
So far you have created a simple JFrame. What you have done so far is legitimate, well created code but there are a few more lines to add into the previous program to make it perfect.           The reason they were omitted from the JFrame -1 example is that all of these lines are semi-optional. There's a good reason for putting each in, but if you have a better reason to leave them out, do so. FrameExample_Extended.java
package java9r.blogspot.com;

import javax.swing.*;

public class FrameExample_Extended{

    private static void createAndShowGUI() {
   
        //This is to turn on the Default 'Look and Feel'.
        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("-------- Hello World --------");
       
        // To make sure the default operation on pressing the close button on the window-bar
        // is to exit the program.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // To size the window to fit all the widgets in, without working out the size manually.
        frame.pack();
        //Display the window.
        frame.setSize(400, 400);
      
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
 Output

0 comments:

Post a Comment

ads2