assign 5 java
assign 5 java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
UIManager.setLookAndFeel(UIManager.getSys
temLookAndFeelClassName());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}*/
calculator c = new calculator();
l = new JTextField(16);
l.setEditable(false);
JButton b0, b1, b2, b3, b4, b5,
b6, b7, b8, b9, ba, bs, bm, bd, be, beq,
beq1;
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);
ba.addActionListener(c);
bs.addActionListener(c);
bm.addActionListener(c);
bd.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);
p.add(l);
p.add(ba);
p.add(bs);
p.add(bm);
p.add(bd);
p.add(be);
p.add(beq);
p.add(beq1);
p.add(b0);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
f.add(p);
f.setLocation(200, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON
_CLOSE);
f.setSize(200, 220);
f.show();
}
public void
actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
l.setText(s0 + s1 + s2);
} else if (s.charAt(0) == 'C') {
s0 = s1 = s2 = "";
l.setText(s0 + s1 + s2);
} else if (s.charAt(0) == '=') {
double te;
if (s1.equals("+"))
te =
(Double.parseDouble(s0) +
Double.parseDouble(s2));
else if (s1.equals("-"))
te =
(Double.parseDouble(s0) -
Double.parseDouble(s2));
else if (s1.equals("/"))
te =
(Double.parseDouble(s0) /
Double.parseDouble(s2));
else
te =
(Double.parseDouble(s0) *
Double.parseDouble(s2));
l.setText(s0 + s1 + s2 + "="
+ te);
s0 = Double.toString(te);
s1 = s2 = "";
} else {
if (s1.equals("") ||
s2.equals(""))
s1 = s;
else {
double te;
if (s1.equals("+"))
te =
(Double.parseDouble(s0) +
Double.parseDouble(s2));
else if (s1.equals("-"))
te =
(Double.parseDouble(s0) -
Double.parseDouble(s2));
else if (s1.equals("/"))
te =
(Double.parseDouble(s0) /
Double.parseDouble(s2));
else
te =
(Double.parseDouble(s0) *
Double.parseDouble(s2));
s0 = Double.toString(te);
s1 = s;
s2 = "";
}
l.setText(s0 + s1 + s2);
}
}
}
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame {
TextField t, t1;
Label l, l1;
int x, y;
Panel p;
MyFrame(String title) {
super(title);
setLayout(new FlowLayout());
p = new Panel();
p.setLayout(new GridLayout(2, 2,
5, 5));
t = new TextField(20);
l = new Label("Co-ordinates of
clicking");
l1 = new Label("Co-ordinates of
movement");
t1 = new TextField(20);
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new
MyMove());
addWindowListener(new
WindowAdapter() {
public void
windowClosing(WindowEvent we) {
System.exit(0);
}
}
);
setSize(500, 500);
//setDefaultCloseOperation(Frame.EXIT_ON_
CLOSE);
setVisible(true);
}
class main {
public static void main(String
args[]) {
MyFrame f = new MyFrame("Mouse
Events");
}
}
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 display the selected options in a text
box.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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);
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_C
LOSE);
}
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");
}
t2.setText(new String(s1));
// t2.setText(s2);
if (e.getSource() == b) {
t2.setText(" ");
t1.setText(" ");
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
setTitle("Login");
setSize(290, 200);
setVisible(true);
setTitle("Password");
setSize(290, 200);
setVisible(true);
}
public void
actionPerformed(ActionEvent ae) {
Button btn = (Button)
(ae.getSource());
if (attempt < 2) {
if (btn == Clear) {
nametext.setText("");
passtext.setText("");
}
if
((btn.getLabel()).equals("Login")) {
try {
String user =
nametext.getText();
String upass =
passtext.getText();
if
(user.compareTo(upass) == 0) {
msg.setText("Valid");
System.out.println("Username is valid");
System.exit(0);
} else {
throw new
InvalidPasswordException();
}
} catch (Exception e) {
msg.setText("Error");
}
attempt++;
}
} else {
System.out.println("Youm are
using 3 attempt");
System.exit(0);
}
}