Assignment No: 5
Set A:
a)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 filed to display the result.
CODE:
👇
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class calc extends JFrame implements ActionListener
- {
- String msg=" ";
- int v1,v2,result;
- JTextField t;
- JButton b[]=new JButton[10];
- JButton add,sub,mul,div,clear,equals;
- char choice;
- JPanel p,p1;
- calc()
- {
- setLayout(new BorderLayout());
- p =new JPanel();
- t=new JTextField(20);
- p.add(t);
- p1=new JPanel();
- p1.setLayout(new GridLayout(5,4));
- for(int i=0;i<10;i++)
- {
- b[i]=new JButton(""+i);
- }
- equals=new JButton("=");
- add=new JButton("+");
- sub=new JButton("-");
- mul=new JButton("*");
- div=new JButton("/");
- clear=new JButton("C");
- for(int i=0;i<10;i++)
- {
- p1.add(b[i]);
- }
- p1.add(equals);
- p1.add(add);
- p1.add(sub);
- p1.add(mul);
- p1.add(div);
- p1.add(clear);
- for(int i=0;i<10;i++)
- {
- b[i].addActionListener(this);
- }
- add.addActionListener(this);
- sub.addActionListener(this);
- mul.addActionListener(this);
- div.addActionListener(this);
- clear.addActionListener(this);
- equals.addActionListener(this);
- add(p,BorderLayout.NORTH);
- add(p1);
- setVisible(true);
- setSize(500,500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent ae)
- {
- String str = ae.getActionCommand();
- char ch = str.charAt(0);
- if ( Character.isDigit(ch))
- t.setText(t.getText()+str);
- else if(str.equals("+"))
- {
- v1=Integer.parseInt(t.getText());
- choice='+';
- t.setText("");
- }
- else if(str.equals("-"))
- {
- v1=Integer.parseInt(t.getText());
- choice='-';
- t.setText("");
- }
- else if(str.equals("*"))
- {
- v1=Integer.parseInt(t.getText());
- choice='*';
- t.setText("");
- }
- else if(str.equals("/"))
- {
- v1=Integer.parseInt(t.getText());
- choice='/';
- t.setText("");
- }
- if(str.equals("="))
- {
- v2=Integer.parseInt(t.getText());
- if(choice=='+')
- result=v1+v2;
- else if(choice=='-')
- result=v1-v2;
- else if(choice=='*')
- result=v1*v2;
- else if(choice=='/')
- result=v1/v2;
- t.setText(""+result);
- }
- if(str.equals("C"))
- {
- t.setText("");
- }
- }
- public static void main(String a[])
- {
- calc ob = new calc();
- }
- }
b)Design a screen to handle the Mouse Event such as MOUSE_MOVED and MOUSE_CLICK and display the position of the Mouse_Click in a TextField.
CODE:
👇
- import java.awt.event.*;
- import java.awt.*;
- import javax.swing.*;
- public class MouseEvents extends JFrame implements MouseListener,MouseMotionListener
- {
- JTextField txt1;
- MouseEvents()
- {
- setSize(500,500);
- setLayout(new FlowLayout());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- txt1=new JTextField(30);
- add(txt1);
- addMouseListener(this);
- addMouseMotionListener(this);
- setVisible(true);
- }
- @Override
- public void mouseDragged(MouseEvent e)
- {}
- @Override
- public void mouseMoved(MouseEvent e)
- {
- txt1.setText("Mouse Moved"+e.getX()+","+e.getY());
- }
- @Override
- public void mouseClicked(MouseEvent e)
- {
- txt1.setText("Mouse Clicked"+e.getX()+","+e.getY());
- }
- @Override
- public void mousePressed(MouseEvent e)
- {}
- @Override
- public void mouseReleased(MouseEvent e)
- {}
- @Override
- public void mouseEntered(MouseEvent e)
- {}
- @Override
- public void mouseExited(MouseEvent e)
- {}
- public static void main(String[]args)
- {
- MouseEvents mouseEvents = new MouseEvents();
- }
- }
Set B:
a) Create the following GUI screen using appropriate layout managers. Accept the name, class,
hobbies of the user and apply the changes and the selected options in a text box.
CODE:
👇
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- class form 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;
- String s;
- static int cnt;
- private StringBuffer s1=new StringBuffer();
- form()
- {
- 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.CENTER);
- setSize(600,300);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==r1)
- {
- cnt++;
- if(cnt==1)
- {
- 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));
- if(e.getSource()==b)
- {
- t2.setText(" ");
- t1.setText(" ");
- }
- }
- public static void main(String arg[])
- {
- form f=new form();
- }
- }
b) Write a java program to design a screen using Awt that will take a username and password. If the username and password are not the same, raise an Exception with the appropriate message. User can have 3 login chance only. Use clear button to clear the TextFields.
CODE:
👇