Difference between revisions of "Simple JFrame"

From MyWiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
How to make frames tutorial : https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html<br>
 
How to make frames tutorial : https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html<br>
A Frame is a top-level window with a title and a border. The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width - (insets.left + insets.right) by height - (insets.top + insets.bottom).<br>
+
<br>
 +
<source lang="java">
 +
package components;
 +
 +
import java.awt.*;
 +
import java.awt.event.*;
 +
import javax.swing.*;
 +
 +
/* FrameDemo.java requires no other files. */
 +
public class FrameDemo {
 +
    /**
 +
    * Create the GUI and show it.  For thread safety,
 +
    * this method should be invoked from the
 +
    * event-dispatching thread.
 +
    */
 +
    private static void createAndShowGUI() {
 +
        //Create and set up the window.
 +
        JFrame frame = new JFrame("FrameDemo");
 +
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +
 +
        JLabel emptyLabel = new JLabel("");
 +
        emptyLabel.setPreferredSize(new Dimension(175, 100));
 +
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
 +
 +
        //Display the window.
 +
        frame.pack();
 +
        frame.setVisible(true);
 +
    }
 +
 +
    public static void main(String[] args) {
 +
        //Schedule a job for the event-dispatching thread:
 +
        //creating and showing this application's GUI.
 +
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
 +
            public void run() {
 +
                createAndShowGUI();
 +
            }
 +
        });
 +
    }
 +
}
 +
 
 +
</source>

Latest revision as of 08:51, 18 November 2014

How to make frames tutorial : https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

package components;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
/* FrameDemo.java requires no other files. */
public class FrameDemo {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JLabel emptyLabel = new JLabel("");
        emptyLabel.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}