0% found this document useful (0 votes)
38 views

Assignment No 3 MRJ

The document contains 3 Java programs that create graphical user interfaces (GUIs) using Swing and AWT components. Program 1 demonstrates how to create a GUI with a label, button, and panels using a GridLayout. It updates the label count and randomly changes the panel background color when the button is clicked. Program 2 creates a calculator GUI with text fields, buttons, and labels to add and subtract two numbers input by the user. Program 3 implements a food ordering GUI using checkboxes for selection and a button to display the total cost of selected items in a message dialog.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Assignment No 3 MRJ

The document contains 3 Java programs that create graphical user interfaces (GUIs) using Swing and AWT components. Program 1 demonstrates how to create a GUI with a label, button, and panels using a GridLayout. It updates the label count and randomly changes the panel background color when the button is clicked. Program 2 creates a calculator GUI with text fields, buttons, and labels to add and subtract two numbers input by the user. Program 3 implements a food ordering GUI using checkboxes for selection and a button to display the total cost of selected items in a message dialog.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NAME: SYED MARJAN GHAZI

ROLL NO :253
BSCS – 2ND
ASSIGNMENT : 3
Program 1:
import java.awt.*;
import java.awt.event.ActionEvent;
//import java.awt.event.WindowListener;
import java.awt.event.ActionListener;
class GUI_event{
private int b1=0;

GUI_event()
{
gui();
}
void gui()
{
Frame frame = new Frame("Enhanced GUI");
// Set Layout Manager
frame.setLayout(new GridLayout(1,1));
// Create Components
Panel pnl=new Panel();
Panel pnl2=new Panel();
Label lbl = new Label("0");
Button btn1= new Button("Button");
pnl.add(lbl);
pnl2.add(btn1);
//int count=0;
frame.add(pnl);
frame.add(pnl2);
// Add Action Listener to the
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String val=lbl.getText();
int count=Integer.parseInt(val);
count++;
lbl.setText("" + count);
Color colorSchme=new
Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*2
56));
pnl.setBackground(colorSchme);

}
});
// Set Frame Size and Visibility
frame.setSize(450,450);
frame.setVisible(true);
// Handle Window Closing Event
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
}

class gui_event_new
{
public static void main(String[] args)
{
GUI_event gi=new GUI_event();
}}

PROGRAM 2:
import java.awt.*;
import java.awt.event.ActionEvent;
//import java.awt.event.WindowListener;
import java.awt.event.ActionListener;
class CalEx{
CalEx()
{
gui();
}
void gui()
{
Frame frame = new Frame("Calculate");
Panel pnl1=new Panel();
Panel pnl2=new Panel();
Panel pnl3=new Panel();
Panel pnl4=new Panel();

frame.setLayout(new GridLayout(4,1));

Label lb1 = new Label("input1");


Label lb2 = new Label("input2");

TextField f1 = new TextField(20);


TextField f2 = new TextField(20);

Button btn1= new Button("+");


Button btn2= new Button("-");
Label lb3 = new Label("Result");
pnl1.add(lb1);
pnl1.add(f1);
pnl2.add(lb2);
pnl2.add(f2);
pnl3.add(btn1);
pnl3.add(btn2);
pnl4.add(lb3);
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);

btn1.setPreferredSize(new Dimension(60, 60));


btn2.setPreferredSize(new Dimension(60, 60));
lb3.setPreferredSize(new Dimension(150, 30));

btn1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String val=f1.getText();
Double val1 = Double.parseDouble(val);
String val2=f2.getText();
Double val3 = Double.parseDouble(val2);

lb3.setText("Addition Result is :" + (val1+val3));

}
});

btn2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String val=f1.getText();
Double val1 = Double.parseDouble(val);
String val2=f2.getText();
Double val3 = Double.parseDouble(val2);

lb3.setText("Substraction Result is :" + (val1-val3));

}
});
frame.setSize(450,450);
frame.setVisible(true);

frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});

class Calculator
{
public static void main(String[] args)
{
CalEx gi=new CalEx();
}
}

PROGRAM 3:
import javax.swing.*;
import java.awt.event.*;
class CheckBoxExample implements ActionListener{
JLabel l;
JCheckBox cb1,cb2,cb3;
JButton b;
JFrame frame;
CheckBoxExample(){
frame=new JFrame("Food");
l=new JLabel("Food Ordering System");
l.setBounds(50,50,300,20);
cb1=new JCheckBox("Pizza @ 100");
cb1.setBounds(100,100,150,20);
cb2=new JCheckBox("Burger @ 30");
cb2.setBounds(100,150,150,20);
cb3=new JCheckBox("Tea @ 10");
cb3.setBounds(100,200,150,20);
b=new JButton("Order");
b.setBounds(100,250,80,30);
b.addActionListener(this);
frame.add(l);
frame.add(cb1);
frame.add(cb2);
frame.add(cb3);
frame.add(b);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
// frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
float amount=0;
String msg="";
if(cb1.isSelected()){
amount+=100;
msg="Pizza: 100\n";
}
if(cb2.isSelected()){
amount+=30;
msg+="Burger: 30\n";
}
if(cb3.isSelected()){
amount+=10;
msg+="Tea: 10\n";
}
msg+="-----------------\n";
JOptionPane.showMessageDialog(frame,msg+"Total:"+amount);
}
public static void main(String[] args) { new CheckBoxExample(); } }

You might also like