JFrame And JWindow Component In Java | Free Source Code

JFrame and JWindow Component in Java

jwindow and Jframe

This is an instructional exercise where we will going to produce a program that will have a JWindow and JFrame Component in Java. The JFrame can be a container of other components, has a title and border, and has buttons for minimize, maximize, and close. A JWindow can hold other components, however not at all like JFrame it has no title and border, and no minimize, maximize, and close button.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name  jWindowComponent.java.

2. Import the following packages:

import javax.swing.*; // used to access the JFrame, JWindow, and JPanel class
import java.awt.*; // used to access the FlowLayout class

3. Initialize your variable in your Main, 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. We will add the JPanel component into the window, and it will have a default BorderLayout of Center position.

window.getContentPane().add(panel, "Center");

Now, we will create a FlowLayout for the panel and a Label inside the panel.

panel.setLayout(new FlowLayout());
panel.add(new Label("This is a Window"));

 

5. Lastly, we will set the size and location of the Frame and the Window.

frame.setSize(300, 300);
frame.setLocation(100, 100);


window.setSize(300, 300);
window.setLocation(500, 100);

Then, we will set the visibility to True for the two components to be seen by the user.

frame.setVisible(true);
window.setVisible(true);


Frame:

jframe


Window:

jwindow


Output:

jframe and jwindow in java

Here’s the full code of this tutorial:

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);
  }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.