DEPARTMENT OF INFORMATION TECHNOLOGY
KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL
(An Autonomous Institute under Kakatiya University, Warangal)
TUTORIAL 12
Course Code /
U18IT406–Java Programming Branch IT
Name
Semester IV Section 1
Topics Covered Java Swings
Tutorial P. No. Tutorial Problem CO CDLL
No.
1. Create a Jframe with two buttons and yes and no. A label to CO4 [Ap]
display the presssed button information.
import [Link].*;
import [Link].*;
import [Link].*;
class myProgram extends Frame implements ActionListener
{
Panel p1=new Panel();
Panel p2=new Panel();
// create two buttons
Button yes=new Button("YES");
Button no=new Button("NO");
// create two labels
Label l1=new Label("YES LABEL");
Label l2=new Label("NO LABEL");
// create a constructor
myProgram()
{
[Link](yes);
[Link](no);
T-12 [Link](this);
[Link](this);
Frame f=new Frame();
[Link](500,500);
[Link](p1);
[Link](p2);
[Link](true);
}
public void actionPerformed(ActionEvent ae)
{
String str = [Link]();
if([Link]("YES"))
{
[Link](l1);
}
else {
[Link](l2);
}
revalidate();
repaint();
}
2. Create a Jframe with two text fields. A label to display the CO4 [Ap]
text field information.
import [Link].*;
import [Link].*;
class text extends JFrame implements ActionListener
{
JTextField t1;
JTextField t2;
JFrame f;
JButton b;
// label to display text
JLabel l;
text()
{
}
public static void main(String[] args)
{
// create a new frame to store text field and button
f = new JFrame("textfield");
// create a label to display text
l = new JLabel("nothing entered");
// create a new button
b = new JButton("submit");
// create a object of the text class
text te = new text();
// addActionListener to button
[Link](te);
// create a object of JTextField with 16 columns
t1 = new JTextField(16);
t2 = new JTextField(16);
// create a panel to add buttons and textfield
JPanel p = new JPanel();
// add buttons and textfield to panel
[Link](t1);
[Link](t2);
[Link](b);
[Link](l);
// add panel to frame
[Link](p);
// set the size of frame
[Link](300, 300);
[Link]();
}
// if the button is pressed
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
if ([Link]("submit")) {
[Link]([Link]());
[Link]([Link]());
// set the text of field to blank
[Link](" ");
}
}
}
2. Create a JApplet with the group of radio buttons. A label to CO4 [Ap]
display the text of selected radio button.
3. Create a JApplet with the check boxes. A label to display CO4 [Ap]
the text of selected check boxes.
1. // importing AWT class
2. import [Link].*;
3. public class CheckboxExample1
4. {
5. // constructor to initialize
6. CheckboxExample1() {
7. // creating the frame with the title
8. Frame f = new Frame("Checkbox Example");
9. // creating the checkboxes
10. Checkbox checkbox1 = new Checkbox("C++");
11. [Link](100, 100, 50, 50);
12. Checkbox checkbox2 = new Checkbox("Java", true);
13. // setting location of checkbox in frame
14. [Link](100, 150, 50, 50);
15. // adding checkboxes to frame
16. [Link](checkbox1);
17. [Link](checkbox2);
18.
19. // setting size, layout and visibility of frame
20. [Link](400,400);
21. [Link](null);
22. [Link](true);
23. }
24. // main method
25. public static void main (String args[])
26. {
27. new CheckboxExample1();
28. }
29. }
4. Create a JDialogBox to illustrate the multiple window CO4 [Ap]
concept.
// java Program to create a simple JDialog
import [Link].*;
import [Link].*;
import [Link].*;
class solve extends JFrame implements ActionListener {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
solve s = new solve();
// create a panel
JPanel p = new JPanel();
JButton b = new JButton("click");
// add actionlistener to button
[Link](s);
// add button to panel
[Link](b);
[Link](p);
// set the size of frame
[Link](400, 400);
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
if ([Link]("click")) {
// create a dialog Box
JDialog d = new JDialog(f, "dialog Box");
// create a label
JLabel l = new JLabel("this is a dialog box");
[Link](l);
// setsize of dialog
[Link](100, 100);
// set visibility of dialog
[Link](true);
}
}
}
5. Create a caculator to perform the arithmetic operation like CO4 [Ap]
addition, multiplication, subtraction and division.
import [Link].*;
import [Link].*;
import [Link].*;
class calculator extends JFrame implements
ActionListener {
static JFrame f;
static JTextField l;
String s0, s1, s2;
calculator()
{ s0 = s1 = s2 = "";
}
public static void main(String args[])
{ f = new JFrame("calculator");
try {
[Link]([Link]
LookAndFeelClassName());
}
catch (Exception e) {
[Link]([Link]());
}
calculator c = new calculator();
l = new JTextField(16);
[Link](false);
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd,
bm, be, beq, beq1;
b0 = new JButton("0"); b1 = new JButton("1");
b2 = new JButton("2"); b3 = new JButton("3");
b4 = new JButton("4"); b5 = new JButton("5");
b6 = new JButton("6"); b7 = new JButton("7");
b8 = new JButton("8");
b9 = new Button("9"); // equals button
beq1 = new JButton("=");
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");
be = new JButton(".");
JPanel p = new JPanel();
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c); [Link](c);
[Link](c);
// add elements to panel
[Link](l);
[Link](ba);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bs);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bm);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bd);
[Link](be);
[Link](b0);
[Link](beq);
[Link](beq1);
// set Background of panel
[Link]([Link]);
// add panel to frame
[Link](p);
[Link](200, 220);
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
// if the value is a number
if (([Link](0) >= '0' && [Link](0) <= '9') ||
[Link](0) == '.') {
// if operand is present then add to second no
if ()
s2 = s2 + s;
else
s0 = s0 + s;
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == '=') {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) +
[Link](s2));
else if ([Link]("-"))
te = ([Link](s0) -
[Link](s2));
else if ([Link]("/"))
te = ([Link](s0) /
[Link](s2));
else
te = ([Link](s0) *
[Link](s2));
// set the value of text
[Link](s0 + s1 + s2 + "=" + te);
// convert it to string
s0 = [Link](te);
s1 = s2 = "";
}
else {
// if there was no operand
if ([Link]("") || [Link](""))
s1 = s;
// else evaluate
else {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) +
[Link](s2));
else if ([Link]("-"))
te = ([Link](s0) -
[Link](s2));
else if ([Link]("/"))
te = ([Link](s0) /
[Link](s2));
else
te = ([Link](s0) *
[Link](s2));
s0 = [Link](te);
// place the operator
s1 = s;
// make the operand blank
s2 = "";
}
// set the value of text
[Link](s0 + s1 + s2);
}
}
}
6. Compare the different layout managers like flow layout, border CO4 [An]
layout and grid layout.
FlowLayout:
Components are laid out from left to right in a row,
wrapping to the next row if necessary.
Components are centered within their cell, and there's a
small gap between them.
Ideal for arranging components in a single row or column,
such as toolbars or simple forms.
Doesn't provide much control over component placement.
BorderLayout:
Divides the container into five regions: north, south, east,
west, and center.
Each region can contain only one component, and
components in the center region stretch to fill the
remaining space.
Useful for creating complex layouts with a header, footer,
sidebar, and main content area.
Offers flexibility in arranging components within different
regions of the container.
GridLayout:
Organizes components into a grid of rows and columns,
where each cell has the same size.
Components are added to the grid one by one, filling each
row before moving to the next.
Useful for creating forms, tables, or any layout requiring a
consistent grid structure.
All cells in the grid have the same size, which may not be
suitable for layouts requiring components of different
sizes.
****
Prepared by:
Dr. Y. Bhavani
Dept. of IT