JFrame and JWindow
JFrame and JWindow Component in Java
Outline: JFrame and JWindow
JFrames and JWindows are two of the most commonly used components in Java applications. A frame is a window that contains other components within it. A window is a frame that is always present on the screen. A button or menu item in a JFrame can be toggled on and off by the user. JWindows offer a similar functionality but are used for menus and toolbars instead of individual buttons. To create an application using these components, you must understand their functionality and how to implement it in your Java code.
Guide: JFrame and JWindow
In this post, we will illustrate how to build a program that consists of a JWindow and JFrame Component in Java. To achieve this, follow the process below;
1 – Create a java program and name it jWindowComponent.java in JCreator or NetBeans.
2 – Import the below packages.
import javax.swing.*; // used to access the JFrame, JWindow, and JPanel class
import java.awt.*; // used to access the FlowLayout class
3 – In the Main, initiate your variable;
– Variable frame for JFrame
-Variable window for JWindow
– and Variable panel for JPanel.
JFrame frame = new JFrame("This is a Frame");
JWindow window = new JWindow();
JPanel panel = new JPanel();
4 – Then, add JPanel component in the window, and the default BorderLayout at Center position.
window.getContentPane().add(panel, "Center");
Proceed to create a FlowLayout for the panel and also a Label inside the panel.
panel.setLayout(new FlowLayout());
panel.add(new Label("This is a Window"));
5 – Finally, we can set the size and location of the Window and Frame.
frame.setSize(300, 300);
frame.setLocation(100, 100);
window.setSize(300, 300);
window.setLocation(500, 100);
Now set the visibility to True for both of the components to be seen by the user.
frame.setVisible(true);
window.setVisible(true);
Frame
Window
Output: JFrame and JWindow
Complete Source Code: JFrame and JWindow
import javax.swing.*; // used to access the JFrame, JWindow, and JPanel class
import java.awt.*; // used to access the FlowLayout class
public class jWindowComponent {
public static void main(String[] args) {
JFrame frame = new JFrame("This is a Frame");
JWindow window = new JWindow();
JPanel panel = new JPanel();
window.getContentPane().add(panel, "Center");
panel.setLayout(new FlowLayout());
panel.add(new Label("This is a Window"));
frame.setSize(300, 300);
frame.setLocation(100, 100);
window.setSize(300, 300);
window.setLocation(500, 100);
frame.setVisible(true);
window.setVisible(true);
}
}