OOP
OOP
import javax.swing.*;
Window(){
setTitle("My First Window");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
2.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
Window(){
setTitle("Bodar Layout Window");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(btn1, BorderLayout.PAGE_START);
add(btn2, BorderLayout.PAGE_END);
add(btn3, BorderLayout.LINE_END);
add(btn4, BorderLayout.LINE_START);
add(btn5, BorderLayout.CENTER);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
3.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JLabel lbl1;
private JLabel lbl2;
Window(){
setTitle("Bodar Layout Window");
setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
add(lbl1, BorderLayout.PAGE_START);
add(lbl2,BorderLayout.PAGE_END);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
4.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JLabel lbl1;
private JLabel lbl2;
Window(){
setTitle("Bodar Layout Window");
setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
public void initComponents(){
lbl1 = new JLabel("This is the North button");
lbl2 = new JLabel("This is the South button");
lbl1.setHorizontalAlignment(JLabel.CENTER);
lbl2.setHorizontalAlignment(JLabel.CENTER);
lbl1.setFont(new Font("Serif",1,20));
lbl2.setFont(new Font("Serif",1,20));
add(lbl1, BorderLayout.PAGE_START);
add(lbl2,BorderLayout.PAGE_END);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
5.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
Window(){
setTitle("flow Layout Window");
setSize(350,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
initComponents();
}
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
6.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
Window(){
setTitle("flow Layout Window");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.LEADING));
initComponents();
}
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
7.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
Window(){
setTitle("flow Layout Window");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout(FlowLayout.TRAILING));
initComponents();
}
add(btn1);
add(btn2);
add(btn3);
add(btn4);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
8.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
Window(){
setTitle("Grid Layout Window");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(2, 2));
initComponents();
}
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
9.
import javax.swing.*;
import java.awt.*;
Calculator() {
setSize(300, 300);
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(btnPanel, BorderLayout.CENTER);
}
}
class Example {
public static void main(String args[]){
Calculator c1 = new Calculator();
c1.setVisible(true);
}
}
10.
This code will not show any thing because it extends the JPanel instead of JFrame, Also the
JPanel need to be add to the JFrame to be displayed since there no JFrame and therefore this
code will not show any thing.
11.
import javax.swing.*;
import java.awt.*;
class Window extends JFrame {
private JPanel panel;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
Window(){
setTitle("JPanel is a Container");
setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
public void initComponents() {
Panel panel = new Panel();
panel.setLayout(new GridLayout(2,2));
add(btn1, BorderLayout.PAGE_START);
add(btn2, BorderLayout.PAGE_END);
add(btn3, BorderLayout.LINE_END);
add(btn4, BorderLayout.LINE_START);
panel.add(btn5);
panel.add(btn6);
panel.add(btn7);
panel.add(btn8);
add(panel, BorderLayout.CENTER);
}
}
class Example {
public static void main(String args[]){
Window c1 = new Window();
c1.setVisible(true);
}
}
12.
The buttonPanel uses the FlowLayout layout manager, which arranges the buttons
horizontally, and the buttonPanel is inserted to the center of the frame using a BorderLayout,
which results in the mixing of layouts. When these two elements are combined, a layout
results in which the buttonPanel keeps its horizontal alignment inside the frame's center.
13.
import javax.swing.*;
import java.awt.*;
Calculator() {
setSize(300, 300);
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
displayPanel.add(txt1);
add(displayPanel, BorderLayout.PAGE_START);
add(btnPanel, BorderLayout.CENTER);
}
}
class Example {
public static void main(String args[]){
Calculator c1 = new Calculator();
c1.setVisible(true);
}
}
14.
import javax.swing.*;
import java.awt.*;
Window() {
setSize(400, 300);
setTitle("Demonstrate TextField");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
class Example {
public static void main(String args[]){
Window w1 = new Window();
w1.setVisible(true);
}
}
15.
The layout of this code will be flowlayout.Because of the flowlayout manager and the order
in which the components are added,the GUI will have a simple horizontal arrangement of the
components from left to right.The first text field will be on the right side and the second text
field will be on the left side followed by the buttons.
16.
The out put will be a simple window with the border name "Demonstrate JLabel" and inside
of the window top left conor there is a label as "This is a JLabel".
17.
18.
import java.awt.*;
import javax.swing.*;
Window(){
setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Student Detail Form");
initComponents();
}
p1.add(lbl1);
p1.add(txt1);
p1.add(lbl2);
p1.add(txt2);
p1.add(lbl3);
p1.add(txt3);
p1.add(lbl4);
p1.add(txt4);
p2.add(lbl5);
p3.add(btn1);
p3.add(btn2);
add(p1);
add(p2,BorderLayout.PAGE_START);
add(p3,BorderLayout.PAGE_END);
}
}
class Example{
public static void main(String args[]){
new Window().setVisible(true);
}
}
19.
The output will be a simple frame with a JComboBox in the north part of the frame with four
options "RED,GREEN,BLACK,BLUE".
20.
import java.awt.*;
import javax.swing.*;
Window(){
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Student Detail Form");
initComponents();
}
p1.add(lbl1);
p1.add(txt1);
p1.add(lbl2);
p1.add(txt2);
p1.add(lbl3);
p1.add(txt3);
p1.add(lbl4);
p1.add(txt4);
add(p1);
}
}
class Example{
public static void main(String args[]){
new Window().setVisible(true);
}
}
21.
import java.awt.*;
import javax.swing.*;
Window(){
setSize(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Student Detail Form");
initComponents();
}
p1.add(lbl1);
p1.add(txt1);
p1.add(lbl2);
p1.add(txt2);
p1.add(lbl3);
p1.add(txt3);
p1.add(lbl4);
p1.add(txt4);
p2.add(lbl5);
p3.add(btn1);
p3.add(btn2);
add(p1);
add(p2,BorderLayout.PAGE_START);
add(p3,BorderLayout.PAGE_END);
}
}
class Example{
public static void main(String args[]){
new Window().setVisible(true);
}
}
22.
An toggle button is a graphical user interface component that represents a two-state switch,
with each click switching the status of the button between "selected" and "unselected." It is
often applied to indicate binary decisions, such as turning on or off a feature, turning
something on or off, or choosing between two options.
23.
import java.awt.*;
import javax.swing.*;
Window(){
setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
j1.setText("Male");
j2.setText("Female");
genderPanel.add(j1);
genderPanel.add(j2);
p1.add(lbl1);
p1.add(txt1);
p1.add(lbl2);
p1.add(txt2);
p1.add(lbl3);
p1.add(genderPanel);
p2.add(lbl4);
p3.add(btn1);
p3.add(btn2);
add(p1);
add(p2,BorderLayout.PAGE_START);
add(p3,BorderLayout.PAGE_END);
}
}
class Example{
public static void main(String args[]){
new Window().setVisible(true);
}
}
24.
import java.awt.*;
import javax.swing.*;
Window(){
setSize(300,100);
setTitle("RadioButton Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
p1.add(c1);
p1.add(c2);
p1.add(c3);
p1.add(c4);
add(p1);
}
}
class Example{
public static void main(String args[]){
new Window().setVisible(true);
}
}
25.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
Window() {
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
p1.add(lbl1);
p2.add(lbl2);
p2.add(c1); // Add the combo box to the panel
add(p1, BorderLayout.PAGE_START);
add(p2, BorderLayout.LINE_START);
}
class Example {
public static void main(String args[]) {
new Window().setVisible(true);
}
}
26.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Window() {
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
initComponents();
}
p1.add(lbl);
p2 = new JPanel();
p2.setLayout(new GridLayout(1, 10));
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
}
}
class Example {
public static void main(String args[]) {
new Window().setVisible(true);
}
}
27.
28.
29.
30.
31.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Window() {
setSize(300, 100);
setTitle("Slider Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
initComponents();
}
add(slider);
}
}
class Example {
public static void main(String args[]){
new Window().setVisible(true);
}
}
32.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Window() {
setSize(350, 300);
setTitle("Slider Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
initComponents();
}
p1.add(lbl1);
p1.add(slider1);
p2.add(lbl2);
p2.add(slider2);
p3.add(lbl3);
p3.add(slider3);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
}
}
class Example {
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
new Window().setVisible(true);
});
}
}