0% found this document useful (0 votes)
2 views10 pages

Java Program To Create A Window 1

The document contains multiple Java programs demonstrating the creation and customization of JFrame windows using Swing. It includes examples of adding components like labels, buttons, and panels, as well as using different layout managers such as GridLayout and FlowLayout. Additionally, it showcases programs for calculating the area and perimeter of rectangles with user input.

Uploaded by

heaven
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Java Program To Create A Window 1

The document contains multiple Java programs demonstrating the creation and customization of JFrame windows using Swing. It includes examples of adding components like labels, buttons, and panels, as well as using different layout managers such as GridLayout and FlowLayout. Additionally, it showcases programs for calculating the area and perimeter of rectangles with user input.

Uploaded by

heaven
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//Java program to create a window

import [Link].*;
public class JFrame1
{
public static void main(String[] args)
{
JFrame aFrame = new JFrame("First frame");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](250, 100);
[Link](true);
}
}

//Java program to create a window with a Constructor


import [Link].*;
public class RectangleProgramOne extends JFrame
{
public RectangleProgramOne()
{
setTitle("Area and Perimeter of a Rectangle");
setSize(200,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[]args)


{
RectangleProgramOne rectProg=new RectangleProgramOne();
}
}

//Creating two JFrame objects application


public class CreateTwoJMyFrameObjects
{
public static void main(String[] args)
{
JMyFrame myFrame = new JMyFrame();
JMyFrame mySecondFrame = new JMyFrame();
}
}
//Customizing a JFrame’s Appearance

import [Link].*;
public class JFrame2
{
public static void main(String[] args)
{
[Link](true);
JFrame aFrame = new JFrame("Second frame");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 400);
[Link](true);
}
}

import [Link].*;
import [Link].*;

public class JFrameWithColor extends JFrame


{
private final int SIZE = 180;
private Container con = getContentPane();

public JFrameWithColor()
{
super("Frame");
setSize(SIZE, SIZE);
[Link]([Link]);
}

public static void main(String[] args)


{
JFrameWithColor frame = new JFrameWithColor();
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
//Java program to create a Jlabel

import [Link].*;
public class JFrame3
{
public static void main(String[] args)
{
final int FRAME_WIDTH = 250;
final int FRAME_HEIGHT = 100;
JFrame aFrame = new JFrame("Third frame");
[Link](FRAME_WIDTH, FRAME_HEIGHT);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);

// JLabel object was created.


JLabel greeting = new JLabel("Good day!");
[Link](greeting);
}
}

import [Link].*;
import [Link].*;

public class JFrame4


{
public static void main(String[] args)
{
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 100;
Font headlineFont = new Font("Arial", [Link], 20);
JFrame aFrame = new JFrame("Fourth frame");
[Link](FRAME_WIDTH, FRAME_HEIGHT);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
JLabel greeting = new JLabel("Good day");
[Link](headlineFont);
[Link](greeting);
}
}
// Using GridLayout Manager
import [Link].*;
import [Link].*;

public class RectangleProgramTwo extends JFrame


{
private JLabel lengthL,widthL,areaL,perimeterL;
private static final int WIDTH=400;
private static final int HEIGHT=300;

public RectangleProgramTwo()
{
//Create the four labels
lengthL=new JLabel("Enter the length : ",[Link]);
widthL=new JLabel("Enter the width : ",[Link]);
areaL=new JLabel("Area : ",[Link]);
perimeterL=new JLabel("Perimeter : ",[Link]);

//Set the title of the window


setTitle("Area and Perimeter of a Rectangle");

//Get the container


Container pane=getContentPane();

//Set the layout


[Link](new GridLayout(4,1));

//Place the components in the pane


[Link](lengthL);
[Link](widthL);
[Link](areaL);
[Link](perimeterL);

//Set the size of the window and display it


setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[]args)


{
RectangleProgramTwo rectObject=new RectangleProgramTwo();
}
}
// Using FlowLayout Manager
import [Link].*;
import [Link].*;
public class JFrame6
{
public static void main(String[] args)
{
final int FRAME_WIDTH = 250;
final int FRAME_HEIGHT = 100;
JFrame aFrame = new JFrame("Sixth frame");
[Link](FRAME_WIDTH, FRAME_HEIGHT);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
JLabel greeting = new JLabel("Hello");
JLabel greeting2 = new JLabel("Who are you?");
[Link](new FlowLayout());
[Link](greeting);
[Link](greeting2);
}
}

import [Link].*;
import [Link].*;
public class JTwoButtons extends JFrame
{
JButton button1 = new JButton("Enabled");
JButton button2 = new JButton("Disabled");
public JTwoButtons()
{
setLayout(new FlowLayout());
add(button1);
add(button2);
[Link](false);
}
public static void main(String[] args)
{
JTwoButtons frame = new JTwoButtons();
[Link](150, 150);
[Link](true);
}
}
// Java Program with Panels
import [Link].*;
import [Link].*;
import [Link];

public class JFrameWithPanels extends JFrame


{
private final int WIDTH = 250;
private final int HEIGHT = 120;
private JButton button1 = new JButton("One");
private JButton button2 = new JButton("Two");
public JFrameWithPanels()
{
super("JFrame with Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
Container con = getContentPane();
[Link](new FlowLayout());
[Link](panel1);
[Link](panel2);
[Link](button1);
[Link]([Link]);
[Link](button2);
[Link]([Link]);
setSize(WIDTH, HEIGHT);
}
public static void main(String[] args)
{
JFrameWithPanels frame = new JFrameWithPanels();
[Link](true);

}
}
// Using Borderlayout Manager
import [Link].*;
import [Link].*;

public class JDemoBorderLayout extends JFrame


{
private JButton nb = new JButton("North Button");
private JButton sb = new JButton("South Button");
private JButton eb = new JButton("East Button");
private JButton wb = new JButton("West Button");
private JButton cb = new JButton("Center Button");
private Container con = getContentPane();

public JDemoBorderLayout()
{
[Link](new BorderLayout());
[Link](nb, [Link]);
[Link](sb, [Link]);
[Link](eb, [Link]);
[Link](wb, [Link]);
[Link](cb, [Link]);
setSize(400, 150);
}
public static void main(String[] args)
{
JDemoBorderLayout frame = new JDemoBorderLayout();
[Link](true);
}
}
// JFRAME with many components
import [Link].*;
import [Link].*;
public class JFrameWithManyComponents extends JFrame
{
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 150;
public JFrameWithManyComponents()
{
super("Demonstrating many components");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel heading = new JLabel("This frame has many components");


[Link](new Font("Arial", [Link], 16));
JLabel namePrompt = new JLabel("Enter your name:");

JTextField nameField = new JTextField(12);

JButton button = new JButton("Click to continue");

setLayout(new FlowLayout());
add(heading);
add(namePrompt);
add(nameField);
add(button);
}

public static void main(String[] args)


{
JFrameWithManyComponents frame = new JFrameWithManyComponents();
[Link](true);

}
import [Link].*;
import [Link].*;
import [Link].*;

public class RectangleProgramThree extends JFrame


{
private JLabel lengthL,widthL,areaL,perimeterL;
private JTextField lengthTF,widthTF,areaTF,perimeterTF;
private JButton calculateB,exitB;
private static final int WIDTH=400;
private static final int HEIGHT=300;

public RectangleProgramThree()
{
//Create the four labels
lengthL=new JLabel("Enter the length : ",[Link]);
widthL=new JLabel("Enter the width : ",[Link]);
areaL=new JLabel("Area : ",[Link]);
perimeterL=new JLabel("Perimeter : ",[Link]);

//Create the four text fields


lengthTF=new JTextField(10);
widthTF=new JTextField(10);
areaTF=new JTextField(10);
perimeterTF=new JTextField(10);

//Create Calculate Button


calculateB=new JButton("Calculate");

//Create Exit Button


exitB=new JButton("Exit");

//Set the title of the window


setTitle("Area and Perimeter of a Rectangle");

//Get the container


Container pane=getContentPane();

//Set the layout


[Link](new GridLayout(5,2));

//Place the components in the pane


[Link](lengthL);
[Link](lengthTF);
[Link](widthL);
[Link](widthTF);
[Link](areaL);
[Link](areaTF);
[Link](perimeterL);
[Link](perimeterTF);
[Link](calculateB);
[Link](exitB);

//Set the size of the window and display it


setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[]args)


{
RectangleProgramThree rectprog = new RectangleProgramThree();
}
}

You might also like