The javax.swing.JFrame class is a type of container
which inherits the java.awt.Frame class. JFrame works like the main window
where components like labels, buttons, textfields are added to create a GUI.
Unlike Frame, JFrame has the option to hide or close
the window with the help of setDefaultCloseOperation(int) method.
Class
Declaration
Following is the declaration for javax.swing.JFrame class
−
public class JFrame
extends
Frame
implements WindowConstants, Accessible, RootPaneContainer
Class Constructors
Constructor
& Description
1. JFrame()
Constructs a new frame that is initially invisible.
2. JFrame(GraphicsConfiguration gc)
Creates a Frame in the specified GraphicsConfiguration
of a screen device and a blank title.
3. JFrame(String title)
Creates a new, initially invisible Frame with the
specified title.
4. JFrame(String title, GraphicsConfiguration gc)
Creates a JFrame with the specified title and the
specified GraphicsConfiguration of a screen device.
Useful Methods:
Modifier
and Type |
Method |
Description |
protected void |
addImpl(Component comp, Object constraints, int
index) |
Adds the specified child Component. |
protected JRootPane |
createRootPane() |
Called by the constructor methods to create the
default rootPane. |
protected void |
frameInit() |
Called by the constructors to init the JFrame
properly. |
void |
setContentPane(Containe contentPane) |
It sets the contentPane property |
static void |
setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) |
Provides a hint as to whether or not newly created
JFrames should have their Window decorations (such as borders, widgets to
close the window, title...) provided by the current look and feel. |
void |
setIconImage(Image image) |
It sets the image to be displayed as the icon for
this window. |
void |
setJMenuBar(JMenuBar menubar) |
It sets the menubar for this frame. |
void |
setLayeredPane(JLayeredPane layeredPane) |
It sets the layeredPane property. |
JRootPane |
getRootPane() |
It returns the rootPane object for this frame. |
TransferHandler |
getTransferHandler() |
It gets the transferHandler property. |
Ex:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrameExample {
public
static void main(String s[]) {
JFrame
frame = new JFrame("JFrame Example");
JPanel
panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel
label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
No comments:
Post a Comment