0% found this document useful (0 votes)
195 views12 pages

Swings Lab Assignment

This document provides code examples for creating internal frames in Java using the JInternalFrame class. It includes constructors for JInternalFrame that allow setting the title, resizability, closability, and maximizability. It also includes a code example that creates multiple internal frames and adds them to a JDesktopPane. Other examples show how to change the background color of a frame using action events and how to create an edit menu for a notepad application.

Uploaded by

Piyush Khalate
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)
195 views12 pages

Swings Lab Assignment

This document provides code examples for creating internal frames in Java using the JInternalFrame class. It includes constructors for JInternalFrame that allow setting the title, resizability, closability, and maximizability. It also includes a code example that creates multiple internal frames and adds them to a JDesktopPane. Other examples show how to change the background color of a frame using action events and how to create an edit menu for a notepad application.

Uploaded by

Piyush Khalate
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
You are on page 1/ 12

How To Create Internal Frames In Java

Constructor Detail Description


This constructor creates an internal frame that
JInternalFrame() haven't a title, can't be maximize, resize, close
and non-iconifiable.
This constructor creates an internal frame with
JInternalFrame(String title) the title, but can't be maximize, resize, close and
non-iconifiable.
This constructor creates an internal frame with
JInternalFrame(String title, boolean resizable) title that can be resize but, can't be maximize,
close and non-iconifiable.
This constructor creates an internal frame with
JInternalFrame(String title, boolean resizable,
title that can be resize, close but, can't be
boolean closable)
maximize and non-iconifiable.
This constructor creates an internal frame with
JInternalFrame(String title, boolean resizable,
the tile that can be resize, maximize, close, but
boolean closable, boolean maximizable)
non-iconifiable.
JInternalFrame(String title, boolean resizable, This constructor creates an internal frame with
boolean closable, boolean maximizable, boolean the title, that can be resize, maximize, close, and
iconifiable) iconifiable.

JDesktopPane class, can be used to create "multi-document" applications.


A multi-document application can have many windows included in it.

Internal windows add instances of JInternalFrame to the JdesktopPane instance. The


internal windows are the instances of JInternalFrame or its subclasses.

// Program to create multiple windows

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

public class MultipleFrames {

JDesktopPane desk;

JInternalFrame frame1, frame2, frame3, frame4;


JFrame frame;

public static void main(String[] args) {

MultipleFrames d = new MultipleFrames();

public MultipleFrames() {

frame = new JFrame("Multiple Frames");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

desk = new JDesktopPane();

frame1 = new JInternalFrame("Frame1", true, true, true);

frame1.setBounds(20, 200, 150, 100);

frame1.setVisible(true);

frame2 = new JInternalFrame("Frame2", true, true, true);

frame2.setBounds(20, 140, 150, 100);

frame2.setVisible(true);

frame3 = new JInternalFrame("Frame3", true, true, true);

frame3.setBounds(20, 80, 150, 100);

frame3.setVisible(true);

frame4 = new JInternalFrame("Frame4", true, true, true);

frame4.setBounds(20, 20, 150, 100);

frame4.setVisible(true);

desk.add(frame1);

desk.add(frame2);

desk.add(frame3);
desk.add(frame4);

frame.add(desk);

frame.setSize(400, 400);

frame.setVisible(true);

A program to change background color of a frame (Using Action Event)

import java.awt.*; //importing awt package

import javax.swing.*; //importing swing package

import java.awt.event.*; //importing event package

//For an event to occur upon clicking the button, ActionListener interface should be implemented

class StColor extends JFrame implements ActionListener{

JFrame frame;

JPanel panel;

JButton b1,b2,b3,b4,b5;

stColor(){

frame = new JFrame("COLORS");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel(); //Creating a panel which is a container and will hold all the buttons
panel.setSize(100, 50);

b1 = new JButton("BLUE"); //Creating a button named BLUE

b1.addActionListener(this); //Registering the button with the listener

b2 = new JButton("RED"); //Creating a button named RED

b2.addActionListener(this); //Registering the button with the listener

b3 = new JButton("CYAN");//Creating a button named CYAN

b3.addActionListener(this);//Registering the button with the listener

b4 = new JButton("PINK"); //Creating a button named PINK

b4.addActionListener(this); //Registering the button with the listener

b5 = new JButton("MAGENTA"); //Creating a button named MAGENTA

b5.addActionListener(this); //Registering the button with the listener

//Adding buttons to the Panel

panel.add(b1);

panel.add(b2);

panel.add(b3);

panel.add(b4);

panel.add(b5);

frame.getContentPane().add(panel); //adding panel to the frame ,


// content pane is layer of container to which components are added

frame.setSize(500,300);

frame.setVisible(true);

frame.setLayout(new FlowLayout());

//The below method is called whenever a button is clicked

@Override

public void actionPerformed(ActionEvent e) {

//This method returns an object of the button on which the Event-

Pressing of button initially occurred

Object see = e.getSource();

if(see ==(b1)){ //Checking if the object returned is of button1

frame.getContentPane().setBackground(java.awt.Color.blue); //changing the panel color to blue

if(see == b2){ //Checking if the object returned is of button2

frame.getContentPane().setBackground(java.awt.Color.red); //changing the panel color to red

if(see == b3){ //Checking if the object returned is of button3

frame.getContentPane().setBackground(java.awt.Color.cyan);//changing the panel color to cyan

if(see == b4){ //Checking if the object returned is of button4

frame.getContentPane().setBackground(java.awt.Color.pink); //changing the panel color to pink


}

if(see == b5){ //Checking if the object returned is of button5

frame.getContentPane().setBackground(java.awt.Color.magenta); //changing the panel color to


magenta

class Test {

public static void main(String[] args) {

StColor o = new StColor();

}
Create a file "Calculate.java" to sum of a number using Swing.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Calculate extends JTextField {

  public static void main(String[] args) {
  JFrame f = new JFrame("Calculate");
  f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), 
  BoxLayout.Y_AXIS));

  final JTextField  text1 = new JTextField(20);
  

  final JTextField text2 = new JTextField(20);
  

  JPanel panel = new JPanel();
  JButton button = new JButton("Calculate");
  panel.add(button);
  f.getContentPane().add(text1);
  f.getContentPane().add(panel);
  f.getContentPane().add(text2);
  
  ActionListener l = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  System.out.println("Action event from a text field");
  }
  };
  text1.addActionListener(l);
  text2.addActionListener(l);
  button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  int a=Integer.parseInt(text1.getText()); 
  int sum = 0;
  int count=1;
  while(count<=a)
  {
  sum+=count;
  count++;
  }
  text2.setText(String.valueOf(sum));
  }
  });
  f.pack(); // in place of setsize(), we can use pack()
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

JAVA GUI application to find sum of two numbers

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class sum extends JFrame implements ActionListener


{
JLabel l1,l2,l3;
JButton b1;
JTextField t1,t2,t3;

sum()
{
l1=new JLabel("INPUT 1");
l2=new JLabel("INPUT 2");
l3=new JLabel("OUTPUT");
b1=new JButton("BUTTON 1");

t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);

add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);

b1.addActionListener(this);

setSize(200,200);
setLayout(new FlowLayout());
setTitle("Assignment 1");
}

public void actionPerformed(ActionEvent ae)


{
float a,b,c;
if(ae.getSource()==b1)
{
a=Float.parseFloat(t1.getText());
b=Float.parseFloat(t2.getText());
c=a+b;
t3.setText(String.valueOf(c));

}
}

public static void main(String args[])


{
sum a=new sum ();
a.setVisible(true);
a.setLocation(200,200);

Example of creating Edit menu for Notepad:

import javax.swing.*;    
import java.awt.event.*;    
public class MenuExample implements ActionListener{    
JFrame f;    
JMenuBar mb;    
JMenu file,edit,help;    
JMenuItem cut,copy,paste,selectAll;    
JTextArea ta;    
MenuExample(){    
f=new JFrame();    
cut=new JMenuItem("cut");    
copy=new JMenuItem("copy");    
paste=new JMenuItem("paste");    
selectAll=new JMenuItem("selectAll");    
cut.addActionListener(this);    
copy.addActionListener(this);    
paste.addActionListener(this);    
selectAll.addActionListener(this);    
mb=new JMenuBar();    
file=new JMenu("File");    
edit=new JMenu("Edit");    
help=new JMenu("Help");     
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);    
mb.add(file);mb.add(edit);mb.add(help);    
ta=new JTextArea();    
ta.setBounds(5,5,360,320);    
f.add(mb);f.add(ta);    
f.setJMenuBar(mb);  
f.setLayout(null);    
f.setSize(400,400);    
f.setVisible(true);    
}     
public void actionPerformed(ActionEvent e) {    
if(e.getSource()==cut)    
ta.cut();    
if(e.getSource()==paste)    
ta.paste();    
if(e.getSource()==copy)    
ta.copy();    
if(e.getSource()==selectAll)    
ta.selectAll();    
}     
public static void main(String[] args) {    
new MenuExample();    
}    
}    

You might also like