Assignment 5: GUI Designing, Event Handling
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 field to display the result.
Simple Calculator
b) 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.
// import java.awt.event.MouseListener;
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(null);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt1 = new JTextField(30);
add(txt1);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
// MouseMotionListener Mothods
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
txt1.setText("Mouse Moved " + e.getX() + "," + e.getY());
}
// MouseListener Mothods
@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 display the selected options in a text box.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SetB1 extends JFrame implements ActionListener
{
JLabel name,class1,hobbies,font,style,size;
JButton clear_button;
JRadioButton r1,r2,r3;
JCheckBox c1,c2,c3;
JTextField name_textfield,final_textfield;
ButtonGroup b1;
JPanel p1,p2;
JComboBox fontcb,sizecb;
JCheckBox bold,italic,underline;
static int cnt;
private StringBuffer s1 = new StringBuffer();
SetB1()
{
b1=new ButtonGroup();
p1=new JPanel();
p2=new JPanel();
clear_button=new JButton("clear");
clear_button.addActionListener(this);
name=new JLabel("Your name");
class1=new JLabel("Your class");
hobbies=new JLabel("Your hobbies");
font=new JLabel("font");
style=new JLabel("style");
size=new JLabel("size");
name_textfield=new JTextField(20);
final_textfield=new JTextField(30);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
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);
fontcb=new JComboBox();
fontcb.addItem("Arial");
fontcb.addItem("Sans");
fontcb.addItem("Monospace");
bold=new JCheckBox("Bold");
italic=new JCheckBox("Italic");
underline=new JCheckBox("Underline");
size=new JLabel("Size");
sizecb=new JComboBox();
sizecb.addItem("10");
sizecb.addItem("15");
sizecb.addItem("18");
p1.setLayout(new GridLayout(5,2));
p1.add(name);
p1.add(name_textfield);
p1.add(class1);
p1.add(hobbies);
p1.add(style);
p1.add(font);
p1.add(r1);
p1.add(c1);
p1.add(bold);
p1.add(fontcb);
p1.add(r2);
p1.add(c2);
p1.add(italic);
p1.add(size);
p1.add(r3);
p1.add(c3);
p1.add(underline);
p1.add(sizecb);
p2.setLayout(new FlowLayout());
p2.add(clear_button);
p2.add(final_textfield);
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 = name_textfield.getText();
s1.append("Name : ");
s1.append(s);
}
s1.append("Class : FY");
}
else if(e.getSource()==r2)
{
cnt++;
if(cnt==1)
{
String s = name_textfield.getText();
s1.append("Name : ");
s1.append(s);
}
s1.append("Class : SY");
}
else if(e.getSource()==r3)
{
cnt++;
if(cnt==1)
{
String s = name_textfield.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");
}
final_textfield.setText(new String(s1));
if(e.getSource()==clear_button)
{
final_textfield.setText(" ");
name_textfield.setText(" ");
}
String f = (String)fontcb.getSelectedItem();
System.out.println("font = "+f);
final_textfield.setFont(new Font(f,Font.BOLD,10));
String no = (String)sizecb.getSelectedItem();
int num=Integer.parseInt(no);
if(bold.isSelected())
{
final_textfield.setFont(new Font(f,Font.BOLD,num));
}
if(italic.isSelected())
{
final_textfield.setFont(new Font(f,Font.ITALIC,num));
}
}
public static void main(String args[])
{
SetB1 s = new SetB1();
}
}
b) 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.
import java.awt.*;
import java.awt.event.*;
class InvalidPasswordException extends Exception
{
InvalidPasswordException()
{
System.out.println(” User name and Password is not same”);
}
}
public class PasswordDemo extends Frame implements ActionListener
{
Label uname,upass;
TextField nametext;
TextField passtext,msg;
Button login,Clear;
Panel p;
int attempt=0;
char c= ‘ * ‘ ;
public void login()
{
p=new Panel();
uname=new Label(“Use Name: ” ,Label.CENTER);
upass=new Label (“Password: “,Label.RIGHT);
nametext=new TextField(20);
passtext =new TextField(20);
passtext.setEchoChar(c);
msg=new TextField(10);
msg.setEditable(false);
login=new Button(“Login”);
Clear=new Button(“Clear”);
login.addActionListener(this);
Clear.addActionListener(this);
p.add(uname);
p.add(nametext);
p.add(upass);
p.add(passtext);
p.add(login);
p.add(Clear);
p.add(msg);
add(p);
setTitle(“Login “);
setSize(290,200);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Button btn=(Button)(ae.getSource());
if(attempt<3)
{
if((btn.getLabel())==”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”);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)
{
msg.setText(“Error”);
}
attempt++;
}
}
else
{
System.out.println(“you are using 3 attempt”);
System.exit(0);
}
}
public static void main(String args[])
{
PasswordDemo pd=new PasswordDemo();
pd.login();
}
}
Set C
a) Write a java program to create the following GUI for user registration form
b) Write a program to display the following menus and sub-menus.
import javax.swing.*;
public class SwingMenu{
public static void main(String[] args) {
SwingMenu s = new SwingMenu();
}
public SwingMenu(){
JFrame frame = new JFrame("Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
frame.setJMenuBar(menubar);
frame.setSize(400,400);
frame.setVisible(true);
}
}
No comments:
Post a Comment