Lab7 (SWING I)
Lab7 (SWING I)
CHAPTER 17 SWING I
LAYOUT MANAGER
sd
2. Write a program that demonstrates GridLayout.
Java Panels can also be used as a sub-container where a panel can be added to another panel. This gives
better control in developing more complex GUI frames.
public FirstWindow()
{
setTitle("Sample Swing Window");
setSize(600,300);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLayout(new BorderLayout());
p1=new JPanel(new GridLayout(3,2));
Font f1=new Font("Verdana",Font.BOLD,18);
l1=new JLabel("Enter Major axis a : ");
p1.add(l1);
t1=new JTextField(30);
p1.add(t1);
l2=new JLabel("Enter Enter Minor axis b : ");
p1.add(l2);
t2=new JTextField(30);
p1.add(t2);
l3=new JLabel("Area of Ellipse : ");
p1.add(l3);
t3=new JTextField(30);
t3.setFont(f1);
t3.setForeground(Color.RED);
p1.add(t3);
add(p1,BorderLayout.CENTER);
p2=new JPanel(new FlowLayout());
b1=new JButton("Calculate");
b1.addActionListener(this);
b1.setToolTipText("Click here to calculate area");
p2.add(b1);
b2=new JButton("Clear");
b2.addActionListener(this);
p2.add(b2);
add(p2,BorderLayout.SOUTH);
}
1. Write a Java program to create a Frame as shown below and perform the button events
mentioned below.
Hints:
Width : 400
Height : 400
Default close operation: Exiting the application
Button events:
a. When the button “Green” is clicked, background color of panel should be changed to
“green”.
b. When the button “Blue” is clicked, background color of the panel should be changed to
“blue”.
c. When the button “Red” is clicked, background color of the panel should be changed to
“Red”.
2. Write a Java program to create a Frame as shown below and perform the button events
mentioned below.
Hints:
o Width: 300
o Height: 200
o Default close operation: Exiting the window
Font/Color settings:
For all labels:
Font type: Consolas
Font style: Bold
Font size: 20
Text color: green
For all textfields:
Font type: Arial Narrow
Font style: Bold + Italic
Font size: 15
Text color: red
For all buttons:
Font type: Arial Black
Font style: Plain
Font size: 10
Text color: pink
Button events:
a. When the button “Submit” is clicked, check the username and password entered in the
respective textboxes. If the entered username is “your name” and password is “your
student id”, then display the message “You are logged in Successfully” in the message label,
otherwise display the message “Sorry!! Check your username and password” in the
message label.
b. When the button “Clear” is clicked, clear the contents of all textboxes and message label.
3. Write a Java program to create a Frame as shown below and perform the button events
mentioned below.
Hints:
Width : 350
Height : 300
Default close operation: EXIT_ON_CLOSE
Font/Color settings:
For all labels:
Font type: Arial Narrow
Font style: Bold
Font size: 20
Text color: Blue
For all textfields:
Font type: Arial Narrow
Font style: Bold + Italic
Font size: 20
Text color: green
For all buttons:
Font type: Bell MT
Font style: Bold
Font size: 10
Text color: gray
Button events:
a. When the button “Add” is clicked, add the values entered in text field1 and text field2,
display the result in text field3
b. When the button “Subtract” is clicked, subtract the values entered in text field2 from
and text field2, display the result in text field3
c. When the button “Clear” is clicked, clear the values entered in text field1, text field2
and text field3
4. Write a Java program to create a Frame as shown below and perform the button events
mentioned below.
Hints:
Width : 400
Height : 400
Default close operation: EXIT_ON_CLOSE
Font/Color settings:
For all labels:
Font type: Consolas
Font style: Bold
Font size: 20
Text color: green
For all textfields:
Font type: Arial Narrow
Font style: Bold + Italic
Font size: 15
Text color: red
For all buttons:
Font type: Arial Black
Font style: Plain
Font size: 10
Text color: pink
Button Events:
When the button “SimpleInterest” is clicked, using the values in text field1, text field2
and text field3, calculate and display simple interest in text field 4.
When the button “Clear” is clicked, clear the values entered in text field1, text field2,
text field3 and text field 4.
Formula: Simple interest = principal amount * number of years * rate of interest /100;
5. Write a Java program to create a Frame as shown below and perform the button events
mentioned below.
Hints:
o Width: 400
o Height: 400
o Default close operation: Exiting the window
Button events:
1. When the button “Send” is clicked, display the message “Message sent successfully” in the
message label.
2. When the button “Cancel” is clicked, clear the contents from textarea and label.
3. When the button “Exit” is clicked, close and exit the application.