[go: up one dir, main page]

0% found this document useful (0 votes)
6 views20 pages

Adv Java Adi

The document contains a series of practical exercises for a BCA-II class, each demonstrating different aspects of Java AWT and Swing. Each practical includes an aim, code implementation, and a brief description of the output. The exercises cover various GUI components such as buttons, dialog boxes, menus, lists, choices, checkboxes, scrollbars, radio buttons, toggle buttons, tabbed panes, and trees.

Uploaded by

sachinlambhade75
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)
6 views20 pages

Adv Java Adi

The document contains a series of practical exercises for a BCA-II class, each demonstrating different aspects of Java AWT and Swing. Each practical includes an aim, code implementation, and a brief description of the output. The exercises cover various GUI components such as buttons, dialog boxes, menus, lists, choices, checkboxes, scrollbars, radio buttons, toggle buttons, tabbed panes, and trees.

Uploaded by

sachinlambhade75
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/ 20

Practical No 1

Aim:- Implements and create five button and put on different direction and centre by
using border layout manager.
Roll no :- 36 Class:-BCA-II
___________________________________________________________________

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BorderLayoutDemo extends JFrame {

BorderLayoutDemo() {
JPanel pa = new JPanel();
pa.setLayout(new BorderLayout());
pa.add(new JButton("NORTH"), BorderLayout.NORTH);
pa.add(new JButton("SOUTH"), BorderLayout.SOUTH);
pa.add(new JButton("EAST"), BorderLayout.EAST);
pa.add(new JButton("WEST"), BorderLayout.WEST);
pa.add(new JButton("CENTER"), BorderLayout.CENTER);
add(pa);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
}

class MainFrame {
public static void main(String[] args) {
new BorderLayoutDemo();
}
}
Output:-
Practical No 2
Aim:-Implementation of AWT to create dialog box
Roll no :- 36 Class:-BCA-II
___________________________________________________________________________
Code:-
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}

Output:-
Practical No 3

Aim:- Implementation of AWT to create menubar.


Roll no :- 36 Class:-BCA-II
___________________________________________________________________
Code:-
import java.awt.*;
import java.awt.event.*;

public class MenuExample {

public static void main(String[] args) {


Frame frame = new Frame("Menu Example");
MenuBar menuBar = new MenuBar();
frame.setMenuBar(menuBar);

Menu fileMenu = new Menu("File");


MenuItem openItem = new MenuItem("Open");
MenuItem saveItem = new MenuItem("Save");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();

MenuItem exitItem = new MenuItem("Exit");


exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
Output:-
Practical No 4

Aim:- Implementation of AWT to create list


Roll no :- 36 Class:-BCA-II
___________________________________________________________________

Code:-
import java.awt.*;

public class ListExample1 {

ListExample1() {
Frame f = new Frame();
List l1 = new List(5);

l1.setBounds(100, 100, 75, 75);

l1.add("java");
l1.add("python");
l1.add("css");
l1.add("DBMS");
l1.add("HTML");

f.add(l1);

f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}

public static void main(String args[]) {


new ListExample1();
}
}

Output:-
Practical No 5

Aim:- Implementation of AWT to create choice


Roll no :- 36 Class:-BCA-II
___________________________________________________________________
Code:-

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

public class ChoiceExample2 {

ChoiceExample2() {

Frame f = new Frame();

final Label label = new Label();

label.setAlignment(Label.CENTER);
label.setSize(400, 100);

Button b = new Button("Show");

b.setBounds(200, 100, 50, 20);

final Choice c = new Choice();

c.setBounds(100, 100, 75, 75);


c.add("C");
c.add("C++");
c.add("Java");
c.add("PHP");
c.add("Android");

f.add(c);
f.add(label);
f.add(b);

f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());
label.setText(data);
}
});
}

public static void main(String args[])


{
new ChoiceExample2();
}
}

Output:-
Practical No 06

Aim:- Implementation of AWT to create checkbox.


Roll no :- 36 Class:-BCA-II
___________________________________________________________________
Code:-

import java.awt.*;

public class one1 {

public one1() {

Frame frame = new Frame("Simple Checkbox Example");

Checkbox checkbox = new Checkbox("Agree to terms and conditions");

checkbox.setBounds(50, 50, 250, 30);

frame.add(checkbox);

frame.setSize(400, 200);

frame.setLayout(null);

frame.setVisible(true);
}
public static void main(String[] args) {
new one1();
}
}

Output:-
Practical No 07

Aim:- Implementation of AWT to create scrollbar


Roll no :- 36
Class:-BCA-II
___________________________________________________________________
Code:-

import java.awt.*;
import java.awt.Font.*;
import java.awt.Label.*;
import java.awt.Scrollbar.*;

public class Main{


public static void main(String[] args){
Frame f = new Frame("Scrollbar Example");
Label l = new Label ("ScrollBar");
l.setBounds(130,80,200,30);
l.setFont(new Font ("Arial",Font.BOLD,20));

l.setForeground(Color.RED);
f.add(l);

Scrollbar s1 = new Scrollbar();


s1.setBounds(280,140,40,175);
f.add(s1);

Label l1 = new Label("Vertical Scrollbar");


l1.setBounds(240,300,166,200);
f.add(l1);
Scrollbar s2 = new Scrollbar(Scrollbar.HORIZONTAL);
s2.setBounds(50,250,155,40);
f.add(s2);

Label l2 = new Label("Horizontal Scrollbar");


l2.setBounds(290,330,200,300);
f.add(l2);

f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

Output:-
Practical No 08

Aim:- Implementation of swing to demonstrate of radiobutton.


Roll no :- 36
Class:-BCA-II
___________________________________________________________________
Code:-

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

class Demo extends JFrame {

JRadioButton jRadioButton1;

JRadioButton jRadioButton2;

JButton jButton;

ButtonGroup G1;

JLabel L1;

public Demo()
{

this.setLayout(null);
jRadioButton1 = new JRadioButton();

jRadioButton2 = new JRadioButton();

jButton = new JButton("Click");

G1 = new ButtonGroup();

L1 = new JLabel("Qualification");

jRadioButton1.setText("Under-Graduate");

jRadioButton2.setText("Graduate");

jRadioButton1.setBounds(120, 30, 120, 50);

jRadioButton2.setBounds(250, 30, 80, 50);

jButton.setBounds(125, 90, 80, 30);

L1.setBounds(20, 30, 150, 50);

this.add(jRadioButton1);

this.add(jRadioButton2);

this.add(jButton);

this.add(L1);

G1.add(jRadioButton1);
G1.add(jRadioButton2);
}
}

class RadioButton {
public static void main(String args[])
{
Demo f = new Demo();

f.setBounds(100, 100, 400, 200);

f.setTitle("RadioButtons");

f.setVisible(true);
}
}
Output:-
Practical No 09

Aim:- Implementation of swing to demonstrate of Jtoggle Button.


Roll no :- 36
Class:-BCA-II
___________________________________________________________________
Code:-

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonExamp {

public static void main(String args[])


{

JFrame frame = new JFrame("Selecting Toggle");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JToggleButton toggleButton = new JToggleButton("Toggle Button");

ItemListener itemListener = new ItemListener() {

public void itemStateChanged(ItemEvent itemEvent)


{

int state = itemEvent.getStateChange();


if (state == ItemEvent.SELECTED) {
System.out.println("Selected");
}
else {

System.out.println("Deselected");
}
}
};

toggleButton.addItemListener(itemListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize(300, 125);
frame.setVisible(true);
}
}

Output:-
Practical No 10

Aim:- Implementation of swing to demonstrate of Tabbed panes.


Roll no :- 36 Class:-BCA-II
___________________________________________________________________
Code:-

import javax.swing.*;
import java.awt.*;

public class TabbedUIExample1 {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {

JFrame window = new JFrame("JTabbedPane Example");

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);

JTabbedPane tabPanel = new JTabbedPane();

JPanel page1 = new JPanel();


page1.add(new JLabel("This is Tab 1"));

JPanel page2 = new JPanel();


page2.add(new JLabel("This is Tab 2"));

JPanel page3 = new JPanel();


page3.add(new JLabel("This is Tab 3"));
tabPanel.addTab("Tab 1", page1);
tabPanel.addTab("Tab 2", page2);
tabPanel.addTab("Tab 3", page3);

window.add(tabPanel);

window.setVisible(true);
}
});
}
}

Output:-
Practical No 11

Aim:- Implementation of swing to demonstrate of Jtree.


Roll no :- 36 Class:-BCA-II
___________________________________________________________________
Code:-

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class DynamicTreeExample {


public static void main(String[] args)
{

JFrame frame = new JFrame(


" Java JTree Example");

DefaultMutableTreeNode root
= new DefaultMutableTreeNode("Root");

DefaultMutableTreeNode parent1
= new DefaultMutableTreeNode("Parent 1");
DefaultMutableTreeNode child1_1
= new DefaultMutableTreeNode("Child 1.1");
DefaultMutableTreeNode child1_2
= new DefaultMutableTreeNode("Child 1.2");

parent1.add(child1_1);
parent1.add(child1_2);

DefaultMutableTreeNode parent2
= new DefaultMutableTreeNode("Parent 2");
DefaultMutableTreeNode child2_1
= new DefaultMutableTreeNode("Child 2.1");
DefaultMutableTreeNode child2_2
= new DefaultMutableTreeNode("Child 2.2");

parent2.add(child2_1);
parent2.add(child2_2);

root.add(parent1);
root.add(parent2);

JTree tree = new JTree(root);

frame.add(new JScrollPane(tree));

frame.setSize(400, 400);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:-

You might also like