0% found this document useful (0 votes)
8 views19 pages

Advance Java Programmiing

Uploaded by

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

Advance Java Programmiing

Uploaded by

rathodnikil07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ADVANCE JAVA PROGRAMMIING: UNIT 1

We can create frame by 2 ways


1) By extending frame class (Association)
2) By instance of frame class

By extending frame class (Association)

import java.awt.*;
public class Practice extends Frame{
public static void main(String[] args) {

Practice f = new Practice();


Label l = new Label("helloworld");
f.setVisible(true);
f.setSize(500, 500);
f.add(l);
f.setTitle("Using association");
}
}

By instance of frame class

import java.awt.*;

public class Practice {


public static void main(String[] args) {
Frame f = new Frame();
f.setTitle("Frame using inharitance");
f.setVisible(true);
f.setSize(500, 500);
Label l = new Label("hello");
f.add(l);
}
}
AWT Controls:
1) Label

import java.awt.Frame;
import java.awt.Label;

public class newpractice extends Frame{


public newpractice(){

setTitle("first label program");


setVisible(true);
setSize(500, 500);

Label l1 = new Label("hey there this is the label",Label.CENTER);


add(l1);

}
public static void main(String[] args) {
new newpractice();
}
}
2) Button

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout());
Button b = new Button("click");
add(b);
}
public static void main(String[] args) {
new newpractice();
}
}

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout());

Button b = new Button("click");


add(b);
try{
Thread.sleep(3000);
}catch(Exception e)
{

}
remove(b);
}
public static void main(String[] args) {
new newpractice();
}
}
3) Checkbox

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout());

Checkbox c = new Checkbox("male", true);


add(c);
}
public static void main(String[] args) {
new newpractice();
}
}

4) checkbox group

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout());
setTitle("Checkbox Group");

CheckboxGroup check = new CheckboxGroup();


Checkbox c1 = new Checkbox("male", check, false);
Checkbox c2 = new Checkbox("female", check, false);
Checkbox c3 = new Checkbox("otehrs", check, false);
add(c1);
add(c2);
add(c3);

}
public static void main(String[] args) {
new newpractice();
}
}
5) scroll bar

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(600, 600);
setVisible(true);
setLayout(new FlowLayout());
Scrollbar s = new Scrollbar(Scrollbar.HORIZONTAL);
Scrollbar s1 = new Scrollbar(Scrollbar.VERTICAL);
Scrollbar s2 = new Scrollbar();
add(s);
add(s1);
add(s2);

}
public static void main(String[] args) {
new newpractice();
}
}

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(600, 600);
setVisible(true);
setLayout(null);
Scrollbar s = new Scrollbar(Scrollbar.VERTICAL);
s.setBounds(570,30,20,550);
add(s);

}
public static void main(String[] args) {
new newpractice();
}
}
6) text field

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(1000, 1000);
setVisible(true);
setLayout(new FlowLayout());
Label l = new Label("enter your name : ");
add(l);
TextField t = new TextField(50);
add(t);

}
public static void main(String[] args) {
new newpractice();
}
}

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(1000, 1000);
setVisible(true);
setLayout(new FlowLayout());
Label l = new Label("Enter Your Name: ");
add(l);
TextField t = new TextField(20);
add(t);
Label l2 = new Label("Enter Your Password : ");
add(l2);
TextField t2 = new TextField(20);
t2.setEchoChar('*'); // if you want to set privacy on your text.
add(t2);
}
public static void main(String[] args) {
new newpractice();
}
}
import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(1000, 1000);
setVisible(true);
setLayout(new FlowLayout());
Label l = new Label("Enter Your Name: ");
add(l);
TextField t = new TextField(20);
t.setEditable(false);
add(t);

}
public static void main(String[] args) {
new newpractice();
}
}

7) text area

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(1000, 1000);
setVisible(true);
setLayout(new FlowLayout());
Label l = new Label("Enter Your Address : ");
add(l);
TextArea t = new TextArea(20, 30);
t.setEditable(true);
add(t);

}
public static void main(String[] args) {
new newpractice();
}
}
8) List

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(600, 600);
setVisible(true);
List l = new List(5, false);
l.add("arvi");
l.add("asti");
l.add("karnja");
l.add("selu");
l.add("wardha");
l.add("devli");
l.add("hinganghat");
l.add("samudrapur");
add(l);
}
public static void main(String[] args) {
new newpractice();
}
}

How to use array of object in AWT components???

import java.awt.*;

public class newpractice extends Frame{


public newpractice(){
setSize(500, 500);
setVisible(true);
setLayout(new FlowLayout());
setTitle("arry of buttons");

Button b[] = new Button[5];


String months[] = {"jan", "feb", "march", "april", "may"};

for(int i = 0; i<=5; i++){


b [i] = new Button(months[i]);
add(b[i]);
}
}
public static void main(String[] args) {
new newpractice();
}
}
Layout Managers:
1) Flow Layout
> It is the default layout for Frame and applet.
> And the default alignment of the Flow Layout is the center.

import java.awt.*;

public class newpractice extends Frame


{

newpractice()
{
setTitle("use of FlowLayout");
setSize(600, 600);
setVisible(true);

// FlowLayout f = new FlowLayout(); //also use this


// FlowLayout f = new FlowLayout(FlowLayout.CENTER);
// FlowLayout f = new FlowLayout(FlowLayout.LEFT);
// FlowLayout f = new FlowLayout(FlowLayout.RIGHT);
// FlowLayout f = new FlowLayout(FlowLayout.LEADING);
FlowLayout f = new FlowLayout(FlowLayout.TRAILING, 20, 20);
setLayout(f);

Button b = new Button("1");


Button b1 = new Button("2");
Button b2= new Button("3");
Button b3 = new Button("4");
Button b4= new Button("5");

add(b);
add(b1);
add(b2);
add(b3);
add(b4);
}

public static void main(String[] args)


{
new newpractice();
}
}
2) Border Layout

import java.awt.*;

public class newpractice extends Frame


{

newpractice()
{
setTitle("use of BorderLayout");
setSize(600, 600);
setVisible(true);
// BorderLayout f = new BorderLayout();
BorderLayout f = new BorderLayout(140, 140);
setLayout(f);

Button b = new Button("1");


Button b1 = new Button("2");
Button b2= new Button("3");
Button b3 = new Button("4");
Button b4= new Button("5");

add(b,BorderLayout.EAST);
add(b1,BorderLayout.WEST);
add(b2,BorderLayout.NORTH);
add(b3,BorderLayout.SOUTH);
add(b4,BorderLayout.CENTER);

public Insets getInsets() // optional if you want margint in your frame.


{
return new Insets(100, 100, 100, 100); // this function provides a margine.
}

public static void main(String[] args)


{
new newpractice();
}
}
3) Grid layout
> These are the combinations of rows and column (r * c)
> Drawback: all the component are having same size. This drawback is uncovered in gridbag layout.
> Components are place left to right and top to bottom.

import java.awt.*;

public class newpractice extends Frame


{

newpractice()
{
setTitle("use of GridLayout");
setSize(600, 600);
setVisible(true);
// GridLayout f = new GridLayout(); // by defulat its add column in the grid.
GridLayout f = new GridLayout(3, 3);
setLayout(f);

for(int i = 1 ; i<=9; i++)


{
Button b = new Button(""+i);
add(b);
}
}

public static void main(String[] args)


{
new newpractice();
}
}

Output:
4) Gridbag Layout
➔ It is same as the grid Layout but in this layout we can set size of the component and also we can set position
of each component individually by giving the value of grid x and grid y.

import java.awt.*;

public class newpractice extends Frame


{

newpractice()
{
setTitle("use of GridbagLayout");
setSize(600, 600);
setVisible(true);

GridBagLayout gb = new GridBagLayout();


GridBagConstraints gc = new GridBagConstraints();
setLayout(gb);

Button b1 = new Button("button 1");


{ gc.fill=GridBagConstraints.HORIZONTAL;
gc.gridx = 1 ;
gc.gridy = 1 ;
add(b1,gc);
}
Button b2 = new Button("button 2");
{ gc.fill=GridBagConstraints.VERTICAL;
gc.gridx = 2;
gc.gridy = 1;
add(b2,gc);
}
Button b3 = new Button("button 3");
{
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 3 ;
gc.gridy = 1 ;
add(b3,gc);
}
Button b4 = new Button("button 4");
{
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1 ;
gc.gridy = 2 ;
gc.gridwidth = 3;
add(b4,gc);
}
Button b5 = new Button("button 5");
{
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridx = 1;
gc.gridy = 3;
gc.gridwidth = 4 ;
add(b5,gc);
}
}

public static void main(String[] args)


{
new newpractice();
}
}

Output:
5) Card Layout
➔ This layout is dynamically change on the user input.
➔ In this layout the different layouts are created using panel class.
➔ It is the type of layout in which we can combine the other Layout. Like flow Layout, border Layout, grid
Layout and gridbagLayout.
➔ All the layouts are arranged like a card that why it called card layout and one layout can visible at a time in
this layout.

import java.awt.*;

import javax.smartcardio.Card;

public class newpractice extends Frame


{

newpractice()
{
setTitle("CardLayout");
setSize(600, 600);
setVisible(true);
CardLayout cl = new CardLayout();
setLayout(cl);

Button b1 = new Button("button 1");


Button b2 = new Button("button 2");

add("Card2",b1);
add("Card2",b2);

public static void main(String[] args)


{
new newpractice();
}
}
Output:
• MENUBAR AND MENUS
➔ THIS MENUS DOES NOT APPY TO YOUR APPLET. IT CAN ONLY APPLEY TO THE TOP LEVAL WINDOWS.

import java.awt.*;

public class newpractice extends Frame


{
newpractice()
{
setTitle("MenuBar");
setSize(500, 500);
setVisible(true);

MenuBar mb = new MenuBar();


setMenuBar(mb);

Menu m = new Menu("New");


mb.add(m);

Menu m2 = new Menu("Old");


mb.add(m2);

Menu m3 = new Menu("Modern");


mb.add(m3);

m3.setEnabled(false);
m3.setLabel("new gen");

Menu mt1 = new Menu("mustang 1969");


MenuItem mt2 = new MenuItem("mazda");
MenuItem mt3 = new MenuItem("old continental gt");

m.add(mt1);
m.addSeparator(); //add a line between components
m.add(mt2);
m.add(mt3);

MenuItem mt1m = new MenuItem("mt1m");


MenuItem mt2m = new MenuItem("mt2m");
MenuItem mt3m = new MenuItem("mt3m");

CheckboxMenuItem cb = new CheckboxMenuItem("hello"); // add checkkbox in menu


mt1.add(cb);

mt1.add(mt1m);
mt1.add(mt2m);
mt1.add(mt3m);

}
public static void main(String[] args)
{
new newpractice();
}
}

Output:
• DIALOG BOXES
> THE DIALOG BOXES ARE USE TO HOLD CONTROLS/ GETTING THE INPUT FROM THE USER.
> TYPES OF DIALOG BOX
1) MODEL
2) MODEL-LESS (IT CAN BE CLOSE)

import java.awt.*;

public class newpractice extends Frame


{
newpractice()
{
setTitle("Dialog Box");
setSize(500, 500);
setVisible(true);

Dialog d = new Dialog(this, "Dialog Box");


d.setSize(400, 200);
d.setVisible(true);
}

public static void main(String[] args)


{
new newpractice();
}
}

Output:
• FILE DIALOG
OPERATION OF FILE DIALOGS
1) SAVE
2) OPEN (LOAD)

NOTE :: BY DEFUALT THE OPRATION OF THE FILE DIALOG IS LOAD

import java.awt.*;

public class newpractice


{

public static void main(String[] args)


{
Frame f1 = new Frame();
f1.setTitle("file dialog");
f1.setSize(600, 600);
f1.setVisible(true);

FileDialog f = new FileDialog(f1,"File Dialog", FileDialog.LOAD);


f.setVisible(true);
f.setSize(600, 600);

}
}

Adding AWT control on the applet:


import java.applet.*;
import java.awt.*;

/*
<applet code = "A" width = 500 height = 500 >
</applet>
*/

public class A extends Applet {

public void init(){


Label l = new Label("hello");
add(l);
}
}

You might also like