ASSIGNMENT NO.
5: GUI Designing,Event Handling
SET A
(1) Write a java program that works as a simple calculator. Use a
grid layout to arrange buttons for the digits and for the +, -, *, %
operations. Add a text field to display the result.
Simple Calculater
1 2 3 +
4 5 6 -
7 8 9 *
0 . = /
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
class BuildCalculator extends JFrame implements ActionListener{
JFrame actualWindow;
JPanel resultPanel, buttonPanel, infoPanel;
JTextField resultTxt;
JButton btn_digits[] = new JButton[10];
JButton btn_plus, btn_minus, btn_mul, btn_div, btn_equal, btn_dot,
btn_clear;
char eventFrom;
JLabel expression, appTitle, siteTitle ;
double oparand_1 = 0, operand_2 = 0;
String operator = "=";
BuildCalculator() {
Font txtFont = new Font("SansSerif", [Link], 20);
Font titleFont = new Font("SansSerif", [Link], 30);
Font expressionFont = new Font("SansSerif", [Link], 15);
actualWindow = new JFrame("Calculator");
resultPanel = new JPanel();
buttonPanel = new JPanel();
infoPanel = new JPanel();
[Link](new GridLayout(3, 1));
[Link](new GridLayout(4, 4));
[Link](new GridLayout(3, 1));
[Link](false);
appTitle = new JLabel("My Calculator");
[Link](titleFont);
expression = new JLabel("Expression shown here");
[Link](expressionFont); siteTitle = new
JLabel("[Link]");
[Link](expressionFont);
[Link]([Link]);
[Link]([Link]);
resultTxt = new JTextField(15);
[Link](null);
[Link](new Dimension(15, 50));
[Link](txtFont);
[Link]([Link]);
for(int i = 0; i < 10; i++)
{ btn_digits[i] = new
JButton(""+i);
btn_digits[i].addActionListener(this);
}
btn_plus = new JButton("+");
btn_plus.addActionListener(this);
btn_minus = new JButton("-");
btn_minus.addActionListener(this);
btn_mul = new JButton("*");
btn_mul.addActionListener(this);
btn_div = new JButton("/");
btn_div.addActionListener(this);
btn_dot = new JButton(".");
btn_dot.addActionListener(this);
btn_equal = new JButton("=");
btn_equal.addActionListener(this);
btn_clear = new JButton("Clear");
btn_clear.addActionListener(this);
[Link](appTitle);
[Link](resultTxt);
[Link](expression);
for(int i = 0; i < 10; i++) {
[Link](btn_digits[i]);
}
[Link](btn_plus);
[Link](btn_minus);
[Link](btn_mul);
[Link](btn_div);
[Link](btn_dot);
[Link](btn_equal);
[Link](btn_clear);
[Link](siteTitle);
[Link](resultPanel);
[Link](buttonPanel);
[Link](infoPanel);
[Link](300, 500);
[Link](true);
}
@Override
public void actionPerformed(ActionEvent e) {
eventFrom = [Link]().charAt(0);
String buildNumber;
if([Link](eventFrom)) { buildNumber
= [Link]() + eventFrom;
[Link](buildNumber); } else
if([Link]() == ".")
{ buildNumber = [Link]() +
eventFrom; [Link](buildNumber);
}
else if(eventFrom != '='){
oparand_1 = [Link]([Link]());
operator = [Link]();
[Link](oparand_1 + " " + operator);
[Link]("");
} else if([Link]() == "Clear") { [Link]("");
}
else {
operand_2 = [Link]([Link]());
[Link]([Link]() + " " + operand_2);
switch(operator) {
case "+": [Link](""+(oparand_1 + operand_2)); break;
case "-": [Link](""+(oparand_1 - operand_2)); break;
case "*": [Link](""+(oparand_1 * operand_2)); break;
case "/": try { if(operand_2 == 0)
throw new ArithmeticException();
[Link](""+(oparand_1 / operand_2)); break;
} catch(ArithmeticException ae) {
[Link](actualWindow, "Divisor
can not be ZERO");
}
}
}
}
}
public class Calculator {
public static void main(String[] args) {
new BuildCalculator();
}
}
OUTPUT:
(2) Design a screen to handle the Mouse Events such as
MOUSE_MOVED and MOUSE_CLICK and display the position of the
Mouse_Click in a TextField.
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
public class Slip2q2 {
public static void main(String[] args) {
new MyFrame("Mouse Events");
}
}
class MyFrame extends JFrame {
TextField click_text_field,
mouse_move_field; Label click_text_label,
mouse_move_label; int x,y;
Panel panel;
MyFrame(String title) {
super(title);
[Link](JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
panel =new Panel();
[Link](new GridLayout(2,2,5,5));
click_text_label = new Label("Co-ordinates of clicking");
mouse_move_label = new Label("Co-ordinates of movement");
click_text_field=new TextField(20);
mouse_move_field =new
TextField(20);
[Link](click_text_label);
[Link](click_text_field);
[Link](mouse_move_label);
[Link](mouse_move_field);
add(panel); addMouseListener(new
MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter {
public void mouseClicked(MouseEvent me)
{ x=[Link](); y=[Link]();
click_text_field.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=[Link]();
y=[Link]();
mouse_move_field.setText("X="+ x +" Y="+y);
}
}
}
OUTPUT:
SET B
(1) Create the following GUI screen using appropriate layout
managers. Accept the name, class, hobbies of the user and apply
the changes and display the selected options in a text box.
Your Name:
Your Class Your Hobbies Font Style
(1) FY Music Arial Bold
(2) SY Sports Size Italic
(3) TY Travelling Underline
Name: Class: Hobbies:
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
class Swing2 extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JButton b;
JRadioButton r1,r2,r3;
JCheckBox c1,c2,c3;
JTextField t1,t2;
ButtonGroup b1;
JPanel p1,p2; static
int cnt;
private StringBuffer s1=new StringBuffer();
Swing2()
{
b1=new
ButtonGroup();
p1=new JPanel();
p2=new JPanel();
b=new JButton("Clear");
[Link](this);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
[Link](r1);
[Link](r2); [Link](r3);
[Link](this);
[Link](this);
[Link](this);
c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");
[Link](this);
[Link](this);
[Link](this);
l1=new JLabel("Your Name");
l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20); t2=new
JTextField(30);
[Link](new GridLayout(5,2));
[Link](l1);[Link](t1);
[Link](l2);[Link](l3);
[Link](r1);[Link](c1);
[Link](r2); [Link](c2);
[Link](r3);[Link](c3);
[Link](new FlowLayout());
[Link](b);
[Link](t2);
setLayout(new BorderLayout());
add(p1,[Link]);
add(p2,[Link]);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==r1)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = FY");
}
else if([Link]()==r2)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = SY");
}
else if([Link]()==r3)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = TY");
}
else if([Link]()==c1)
{
[Link](" Hobbies = Music");
}
else if([Link]()==c2)
{
[Link](" Hobbies = Dance"); }
else if([Link]()==c3)
{
[Link](" Hobbies = Sports");
}
[Link](new String(s1));
// [Link](s2);
if([Link]()==b)
{
[Link](" ");
[Link](" ");
}
public static void main(String arg[])
{
Swing2 s=new Swing2();
}
}
class Swing1 extends JFrame implements ItemListener
{
JLabel font, style, size;
JComboBox fontcb, sizecb;
JCheckBox bold, italic;
JTextField t;
JPanel p1, p2;
Swing1()
{ p1 = new JPanel();
p2 = new JPanel();
font = new JLabel("Font");
style = new JLabel("Style");
fontcb = new JComboBox();
[Link]("Arial");
[Link]("Sans");
[Link]("Monospace");
bold = new JCheckBox("Bold");
size = new JLabel("Size");
italic = new JCheckBox("Italic");
sizecb = new JComboBox();
[Link]("10");
[Link]("12");
[Link]("16");
t = new JTextField(10);
[Link](new GridLayout(4,2));
[Link](font);
[Link](style);
[Link](fontcb);
[Link](bold);
[Link](size);
[Link](italic);
[Link](sizecb);
[Link](new FlowLayout());
[Link](t);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
setLayout(new BorderLayout());
add(p1,[Link]);
add(p2,[Link]);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String f = (String)[Link]();
[Link]("font = "+f);
[Link](new Font(f,[Link],10));
String no =(String)[Link]();
int num=[Link](no);
if([Link]())
{
[Link](new Font(f,[Link],num));
}
if([Link]())
{
[Link](new Font(f,[Link],num));
}
}
public static void main(String args[])
{
Swing1 f1 = new Swing1();
}
}
OUTPUT:
(2) Write a java program to design a screen using Awt that will
take a user name and password. If the user name and password are
not same, raise an Exception with appropriate message. User can
have 3 login chances only. Use clear button to clear the TextFields.
PROGRAM:
import [Link].*;
import [Link].*;
import [Link].*;
class InvalidPasswordException extends Exception
{}
class Slip17 extends JFrame implements ActionListener
{
JLabel name, pass;
JTextField nameText;
JPasswordField
passText; JButton login,
end; static int cnt=0;
Slip17()
{
name = new JLabel("Name : "); pass =
new JLabel("Password : "); nameText = new
JTextField(20); passText = new
JPasswordField(20); login = new
JButton("Login"); end = new
JButton("End");
[Link](this);
[Link](this);
setLayout(new GridLayout(3,2));
add(name); add(nameText);
add(pass); add(passText); add(login);
add(end); setTitle("Login Check");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==end)
{
[Link](0);
}
if([Link]()==login)
{
try
{
String user = [Link]();
String pass = new String([Link]());
if([Link](pass)==0)
{ [Link](null,"Login
Successful","Login",JOptionPane.INFORMATION_MESSAGE);
[Link](0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
{
cnt++;
[Link](null,"Login
Failed","Login",JOptionPane.ERROR_MESSAGE);
[Link]("");
[Link]("");
[Link]();
if(cnt == 3)
{
[Link](null,"3 Attempts
Over","Login",JOptionPane.ERROR_MESSAGE);
[Link](0);
}
}
}
}
public static void main(String args[])
{
new Slip17();
}
}
OUTPUT: