JFrame is class of swing package in java and swing package is a part of j2se to develop standalone application.
Here below I am Creating a frame with single argument constructor of the JFrame where I have declared the title of the frame. Next, I am creating a JPanel, JPanel is another type of container. in which panel we are adding other components and the panel itself will be added to the frame.
package com.findnerd;
import java.awt.FlowLayout;
import javax.swing.*;
public class TestFrameExample {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("This is a label! Bhagwan Singh");
JButton button = new JButton();
button.setText("Press me");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
0 Comment(s)