Aim
To implement layout like border, grid bag, flow card ,grid layout in java program | CS1404 INTERNET PROGRAMMING LABORATORY in Java Program
Algorithm
Start the program
Import at the necessary package
Declare the class in public FlowLayoutDemo
Then declare the layout with their syntax
In the graphics function to display the object
Terminate the program
Source Code | Java Program | Flow layout
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="FlowLayoutDemo" width=300 height=300>
</applet>
*/
public class FlowLayoutDemo extends Applet implements
ItemListener {
Checkbox chkWinXP, chkWin2003, chkRed, chkFed;
public void init() {
setLayout(new FlowLayout(FlowLayout.LEFT));
Label lblOS = new Label("Operating System(s)
Knowledge :- ");
chkWinXP = new Checkbox("Windows XP");
chkWin2003 = new Checkbox("Windows 2003 Server");
chkRed = new Checkbox("Red Hat Linux");
chkFed = new Checkbox("Fedora");
add(lblOS);
add(chkWinXP);
add(chkWin2003);
add(chkRed);
add(chkFed);
chkWinXP.addItemListener(this);
chkWin2003.addItemListener(this);
chkRed.addItemListener(this);
chkFed.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
public void paint(Graphics g) {
g.drawString("Operating System(s) Knowledge : ", 10,
130);
g.drawString("Windows Xp : " + chkWinXP.getState(),
10, 150);
g.drawString("Windows 2003 Server : " +
chkWin2003.getState(), 10, 170);
g.drawString("Red Hat Linux : " + chkRed.getState(),
10, 190);
g.drawString("Fedora : " + chkFed.getState(), 10,
210);
}
}
Output:-
Compile java FlowLayoutDemo program
C:\IPLAB>javac FlowLayoutDemo.java
C:\IPLAB>appletviewer FlowLayoutDemo.java
Source Code | Java Program | BorderLayout
import java.awt.*;
import java.applet.*;
import java.util.*;
/*
<applet code="BorderLayoutDemo" width=500 height=250>
</applet>
*/
public class BorderLayoutDemo extends Applet {
public void init() {
setLayout(new BorderLayout());
add(new Button("Rajalakshmi Engineering College"),
BorderLayout.NORTH);
add(new Label("Rajalakshmi Nagar, Thandalam, Chennai
- 602 105"),
BorderLayout.SOUTH);
add(new Button("Mission"), BorderLayout.EAST);
add(new Button("Vision"), BorderLayout.WEST);
String msg = "Rajalakshmi Engineering College was
established \n" +
"in the year 1997 under the aegis of Rajalakshmi
Educational Trust \n" +
"whose members have had consummate experience in the
fields of \n" +
"education and industry.";
add(new TextArea(msg), BorderLayout.CENTER);
}
}
Output:-
C:\IPLAB>javac BorderLayoutDemo.java
C:\IPLAB>appletviewer BorderLayoutDemo.java
Source Code | Java Program | Grid layout
import java.awt.*;
import java.applet.*;
/*
<applet code="GridLayoutDemo" width=400 height=200>
</applet>
*/
public class GridLayoutDemo extends Applet {
public void init() {
setLayout(new GridLayout(4, 4));
setFont(new Font("SansSerif", Font.BOLD, 24));
for(int i = 1; i <=15 ; i++) {
add(new Button("" + i));
}
}
}
Output:-
C:\IPLAB>javac GridLayoutDemo.java
C:\IPLAB>appletviewer GridLayoutDemo.java
Source Code | Java Program | CardLayout
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CardLayoutDemo" width=300 height=100>
</applet>
*/
public class CardLayoutDemo extends Applet implements
ActionListener, MouseListener {
Checkbox chkVB, chkASP, chkJ2EE, chkJ2ME;
Panel pnlTech;
CardLayout cardLO;
Button btnMicrosoft, btnJava;
public void init() {
btnMicrosoft = new Button("Microsoft Products");
btnJava = new Button("Java Products");
add(btnMicrosoft);
add(btnJava);
cardLO = new CardLayout();
pnlTech = new Panel();
pnlTech.setLayout(cardLO);
chkVB = new Checkbox("Visual Basic");
chkASP = new Checkbox("ASP");
chkJ2EE = new Checkbox("J2EE");
chkJ2ME = new Checkbox("J2ME");
Panel pnlMicrosoft = new Panel();
pnlMicrosoft.add(chkVB);
pnlMicrosoft.add(chkASP);
Panel pnlJava = new Panel();
pnlJava.add(chkJ2EE);
pnlJava.add(chkJ2ME);
pnlTech.add(pnlMicrosoft, "Microsoft");
pnlTech.add(pnlJava, "Java");
add(pnlTech);
btnMicrosoft.addActionListener(this);
btnJava.addActionListener(this);
addMouseListener(this);
}
public void mousePressed(MouseEvent me) {
cardLO.next(pnlTech);
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == btnMicrosoft) {
cardLO.show(pnlTech, "Microsoft");
}
else {
cardLO.show(pnlTech, "Java");
}
}
}
Output:-
C:\IPLAB>javac CardLayoutDemo.java
C:\IPLAB>appletviewer CardLayoutDemo.java
0 comments:
Post a Comment