0% found this document useful (0 votes)
10 views16 pages

Assignment 5

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Assignment 5

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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", Font.BOLD, 20);
Font titleFont = new Font("SansSerif", Font.BOLD, 30);
Font expressionFont = new Font("SansSerif", Font.BOLD, 15);
actualWindow = new JFrame("Calculator");
resultPanel = new JPanel();
buttonPanel = new JPanel();
infoPanel = new JPanel();

actualWindow.setLayout(new GridLayout(3, 1));


buttonPanel.setLayout(new GridLayout(4, 4));
infoPanel.setLayout(new GridLayout(3, 1));
actualWindow.setResizable(false);

appTitle = new JLabel("My Calculator");


appTitle.setFont(titleFont);
expression = new JLabel("Expression shown here");
expression.setFont(expressionFont); siteTitle = new
JLabel("www.btechsmartclass.com");
siteTitle.setFont(expressionFont);
siteTitle.setHorizontalAlignment(SwingConstants.CENTER);
siteTitle.setForeground(Color.BLUE);

resultTxt = new JTextField(15);


resultTxt.setBorder(null);
resultTxt.setPreferredSize(new Dimension(15, 50));
resultTxt.setFont(txtFont);
resultTxt.setHorizontalAlignment(SwingConstants.RIGHT);

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

resultPanel.add(appTitle);
resultPanel.add(resultTxt);
resultPanel.add(expression);
for(int i = 0; i < 10; i++) {
buttonPanel.add(btn_digits[i]);
}
buttonPanel.add(btn_plus);
buttonPanel.add(btn_minus);
buttonPanel.add(btn_mul);
buttonPanel.add(btn_div);
buttonPanel.add(btn_dot);
buttonPanel.add(btn_equal);
infoPanel.add(btn_clear);
infoPanel.add(siteTitle);

actualWindow.add(resultPanel);
actualWindow.add(buttonPanel);
actualWindow.add(infoPanel);

actualWindow.setSize(300, 500);
actualWindow.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
eventFrom = e.getActionCommand().charAt(0);
String buildNumber;
if(Character.isDigit(eventFrom)) { buildNumber
= resultTxt.getText() + eventFrom;
resultTxt.setText(buildNumber); } else
if(e.getActionCommand() == ".")
{ buildNumber = resultTxt.getText() +
eventFrom; resultTxt.setText(buildNumber);
}
else if(eventFrom != '='){
oparand_1 = Double.parseDouble(resultTxt.getText());
operator = e.getActionCommand();
expression.setText(oparand_1 + " " + operator);
resultTxt.setText("");
} else if(e.getActionCommand() == "Clear") { resultTxt.setText("");
}
else {
operand_2 = Double.parseDouble(resultTxt.getText());

expression.setText(expression.getText() + " " + operand_2);


switch(operator) {
case "+": resultTxt.setText(""+(oparand_1 + operand_2)); break;
case "-": resultTxt.setText(""+(oparand_1 - operand_2)); break;
case "*": resultTxt.setText(""+(oparand_1 * operand_2)); break;
case "/": try { if(operand_2 == 0)
throw new ArithmeticException();
resultTxt.setText(""+(oparand_1 / operand_2)); break;
} catch(ArithmeticException ae) {
JOptionPane.showMessageDialog(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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

panel =new Panel();


panel.setLayout(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);
panel.add(click_text_label);
panel.add(click_text_field);
panel.add(mouse_move_label);
panel.add(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=me.getX(); y=me.getY();
click_text_field.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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");
b.addActionListener(this);

r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");

b1.add(r1);
b1.add(r2); b1.add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);

c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");

c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);

l1=new JLabel("Your Name");


l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20); t2=new
JTextField(30);

p1.setLayout(new GridLayout(5,2));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(l3);
p1.add(r1);p1.add(c1);
p1.add(r2); p1.add(c2);
p1.add(r3);p1.add(c3);

p2.setLayout(new FlowLayout());
p2.add(b);
p2.add(t2);

setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.EAST);

setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==r1)
{
cnt++;
if(cnt==1)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = FY");
}
else if(e.getSource()==r2)
{
cnt++;
if(cnt==1)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = SY");
}
else if(e.getSource()==r3)
{
cnt++;
if(cnt==1)
{
String s =t1.getText();
s1.append("Name = ");
s1.append(s);
}
s1.append(" Class = TY");
}

else if(e.getSource()==c1)
{
s1.append(" Hobbies = Music");
}
else if(e.getSource()==c2)
{
s1.append(" Hobbies = Dance"); }
else if(e.getSource()==c3)
{
s1.append(" Hobbies = Sports");
}
t2.setText(new String(s1));
// t2.setText(s2);

if(e.getSource()==b)
{
t2.setText(" ");
t1.setText(" ");
}

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


fontcb.addItem("Arial");
fontcb.addItem("Sans");
fontcb.addItem("Monospace");

bold = new JCheckBox("Bold");


size = new JLabel("Size");
italic = new JCheckBox("Italic");

sizecb = new JComboBox();


sizecb.addItem("10");
sizecb.addItem("12");
sizecb.addItem("16");
t = new JTextField(10);

p1.setLayout(new GridLayout(4,2));
p1.add(font);
p1.add(style);
p1.add(fontcb);
p1.add(bold);
p1.add(size);
p1.add(italic);
p1.add(sizecb);

p2.setLayout(new FlowLayout());
p2.add(t);

bold.addItemListener(this);
italic.addItemListener(this);
fontcb.addItemListener(this);
sizecb.addItemListener(this);

setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);

setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String f = (String)fontcb.getSelectedItem();
System.out.println("font = "+f);
t.setFont(new Font(f,Font.BOLD,10));
String no =(String)sizecb.getSelectedItem();
int num=Integer.parseInt(no);

if(bold.isSelected())
{
t.setFont(new Font(f,Font.BOLD,num));
}
if(italic.isSelected())
{
t.setFont(new Font(f,Font.ITALIC,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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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");
login.addActionListener(this);
end.addActionListener(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(e.getSource()==end)
{
System.exit(0);

}
if(e.getSource()==login)
{
try
{
String user = nameText.getText();
String pass = new String(passText.getPassword());
if(user.compareTo(pass)==0)
{ JOptionPane.showMessageDialog(null,"Login
Successful","Login",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
{
cnt++;
JOptionPane.showMessageDialog(null,"Login
Failed","Login",JOptionPane.ERROR_MESSAGE);
nameText.setText("");
passText.setText("");
nameText.requestFocus();
if(cnt == 3)
{
JOptionPane.showMessageDialog(null,"3 Attempts
Over","Login",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
public static void main(String args[])
{
new Slip17();
}
}
OUTPUT:

You might also like