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.
To add a JFrame container: In the Projects window, right-click the ContactEditor node and choose New > JFrame Form. Alternatively, you can find a JFrame form by choosing New > Other > Swing GUI Forms > JFrame Form. Enter ContactEditorUI as the Class Name.
The Java Swing JFrame class is the foundation for creating graphical Java applications. Without the frame, you can't perform any interactions. When you create a new instance of the JFrame, you can pass a title to the constructor or simply create an empty frame.
Method Summary
Basically, a JFrame represents a framed window and a JPanel represents some area in which controls (e.g., buttons, checkboxes, and textfields) and visuals (e.g., figures, pictures, and even text) can appear.
JPanel vs JFrame both are commonly used classes available in java; JPanel can be considered as a general container, which is used in case of complex or bigger functions which require grouping of different components together; on the other hand, JFrame is generally used to host simple elements used in a window like a ...
Thoughts: Avoid extending JFrame as it ties your GUI to being, well a JFrame. If instead you concentrate on creating JPanels instead, then you have the freedom to use these JPanels anywhere needed -- in a JFrame, or JDialog, or JApplet, or inside of another JPanel, or swapped with other JPanels via a CardLayout./span>
Create a Simple Window Using JFrame
A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame. By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class./span>
More JFrame size information
In a Java JFrame, we can add an instance of a JButton class, which creates a button on the frame as follows in the code below:
Java JTextField Example
getLabel
Java JButton Example
Button is a control component that has a label and generates an event when pressed. When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. ... The application can make use of the button's action command as a messaging protocol.
JToggleButton(String text): Creates an unselected toggle button with the specified text. JToggleButton(String text, boolean selected): Creates a toggle button with the specified text and selection state....Commonly Used Methods:
The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers./span>
There are 7 layout managers built into Java. Most UIs are built using some combination of them, typically by nesting layout managers. The most commonly used layouts are FlowLayout, BorderLayout and BoxLayout.
The setLayout(...) method allows you to set the layout of the container, often a JPanel, to say FlowLayout, BorderLayout, GridLayout, null layout, or whatever layout desired. The layout manager helps lay out the components held by this container./span>
A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. ... Object) method can be used to associate a string identifier with a given card for fast random access.
A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size./span>
Constructs a border layout with the specified gaps between components. The horizontal gap is specified by hgap and the vertical gap is specified by vgap .
BorderLayout is the default layout for the window objects such as JFrame, JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the components in the five regions. Four sides are referred to as north, south, east, and west. The middle part is called the center./span>
The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program.
JSlider is an implementation of slider. The Component allows the user to select a value by sliding the knob withing the bounded value . The slider can show Major Tick marks and also the minor tick marks between two major tick marks, The knob can be positioned at only those points./span>
Several AWT and Swing classes provide layout managers for general use:
There are four basic layout types: process, product, hybrid, and fixed position.
A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.
Because they use different layout managers to control the size and position of the buttons. Layout management is the process of determining the size and position of components. By default, each container has a layout manager -- an object that performs layout management for the components within the container.