0% found this document useful (0 votes)
14 views

Lab7 (SWING I)

The document provides examples of Java Swing programs to create GUI frames using different layout managers and components. It includes 5 programming tasks that involve creating frames with panels, labels, text fields, buttons and performing events like changing colors, validating login details, performing calculations and displaying messages.

Uploaded by

sa9661623
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab7 (SWING I)

The document provides examples of Java Swing programs to create GUI frames using different layout managers and components. It includes 5 programming tasks that involve creating frames with panels, labels, text fields, buttons and performing events like changing colors, validating login details, performing calculations and displaying messages.

Uploaded by

sa9661623
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JUBAIL UNIVERSITY COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


CS 231 PROGRAMMING II

CHAPTER 17 SWING I
LAYOUT MANAGER

1. Write a program that demonstrates Flow Layout.

sd
2. Write a program that demonstrates GridLayout.

3. Write a program that demonstrates BorderLayout.


USING PANEL, LAYOUT MANAGER, BUTTON, LABEL, TEXTBOX

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.

4. Write a program that creates a GUI similar to a Microwave Oven interface.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstWindow extends JFrame implements ActionListener
{
JPanel p1,p2;
JButton b1,b2;
JLabel l1,l2,l3;
JTextField t1,t2,t3;

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);
}

public void actionPerformed(ActionEvent e)


{
String k=e.getActionCommand();
if(k.equals("Calculate"))
{
double a=Double.parseDouble(t1.getText());
double b=Double.parseDouble(t2.getText());
double res=3.14*a*b;
t3.setText(Double.toString(res));
}
if(k.equals("Clear"))
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
class demo
{
public static void main(String args[])
{
FirstWindow fw=new FirstWindow();
fw.setVisible(true);
}
}
I. DIY: (Using Layouts, Panel, Button, Label, TextField, PasswordField, TextArea,
Color and Font)

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.

You might also like