This section contains the detail about Swing's Jframe in java.
Working with JFrame
JFrame is a Window with border, title, buttons, menu bar and user-specified components. The components added to the frame are referred to as its contents; these are managed by the contentPane. JFrame objects store several objects including a Container object known as the content pane. To add a component to a JFrame, add it to the content pane.
By default, a JFrame is displayed in the upper-left corner of the screen. To display a frame at a specified location, you can use the setLocation(x, y) method in the JFrame class.
JFrame Constructors
Constuctors | Description |
JFrame() | Constructs a new frame that is initially invisible. |
JFrame(GraphicsConfiguration gc) | Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title. |
JFrame(String title) | Creates a new, initially invisible Frame with the specified title. |
JFrame(String title, GraphicsConfiguration gc) | Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device. |
Example :
package corejava; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JFrameTest { public static void main(String s[]) { JFrame frame = new JFrame("JFrame Source Demo"); // Add a window listner for close button frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // This is an empty content area in the frame JLabel jlbempty = new JLabel("This is JLabel"); jlbempty.setPreferredSize(new Dimension(275, 100)); frame.getContentPane().add(jlbempty, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }
Output :
[ 0 ] Comments