SEARCH HERE

Tuesday, December 27, 2022

Java JFrame

 

Java JFrame

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.

Nested Class

Modifier and TypeClassDescription
protected classJFrame.AccessibleJFrameThis class implements accessibility support for the JFrame class.

Fields

Modifier and TypeFieldDescription
protected AccessibleContextaccessibleContextThe accessible context property.
static intEXIT_ON_CLOSEThe exit application default window close operation.
protected JRootPanerootPaneThe JRootPane instance that manages the contentPane and optional menuBar for this frame, as well as the glassPane.
protected booleanrootPaneCheckingEnabledIf true then calls to add and setLayout will be forwarded to the contentPane.

Constructors

ConstructorDescription
JFrame()It constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc)It creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title)It creates a new, initially invisible Frame with the specified title.
JFrame(String title, GraphicsConfiguration gc)It creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.

Useful Methods

Modifier and TypeMethodDescription
protected voidaddImpl(Component comp, Object constraints, int index)Adds the specified child Component.
protected JRootPanecreateRootPane()Called by the constructor methods to create the default rootPane.
protected voidframeInit()Called by the constructors to init the JFrame properly.
voidsetContentPane(Containe contentPane)It sets the contentPane property
static voidsetDefaultLookAndFeelDecorated(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.
voidsetIconImage(Image image)It sets the image to be displayed as the icon for this window.
voidsetJMenuBar(JMenuBar menubar)It sets the menubar for this frame.
voidsetLayeredPane(JLayeredPane layeredPane)It sets the layeredPane property.
JRootPanegetRootPane()It returns the rootPane object for this frame.
TransferHandlergetTransferHandler()It gets the transferHandler property.

JFrame Example

  1. import java.awt.FlowLayout;  
  2. import javax.swing.JButton;  
  3. import javax.swing.JFrame;  
  4. import javax.swing.JLabel;  
  5. import javax.swing.JPanel;  
  6. public class JFrameExample {  
  7.     public static void main(String s[]) {  
  8.         JFrame frame = new JFrame("JFrame Example");  
  9.         JPanel panel = new JPanel();  
  10.         panel.setLayout(new FlowLayout());  
  11.         JLabel label = new JLabel("JFrame By Example");  
  12.         JButton button = new JButton();  
  13.         button.setText("Button");  
  14.         panel.add(label);  
  15.         panel.add(button);  
  16.         frame.add(panel);  
  17.         frame.setSize(200300);  
  18.         frame.setLocationRelativeTo(null);  
  19.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.         frame.setVisible(true);  
  21.     }  
  22. }  

Output

Java Jframe

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top