0% found this document useful (0 votes)
14 views12 pages

Muhammad Mughira Asad123

The document contains 9 code examples demonstrating the creation of graphical user interfaces (GUIs) using Java AWT and Swing. The examples show how to: 1. Create a basic frame with layouts and add components like buttons and labels 2. Use different layout managers like GridLayout 3. Add action listeners to buttons to trigger events 4. Use layouts like null layout and set component bounds 5. Change background color on button click 6. Take user input from text fields and display output 7. Create a calculator GUI with multiple panels 8. Demonstrate checkbox selection in Swing 9. Add and remove components dynamically in Swing

Uploaded by

abdul22momin
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)
14 views12 pages

Muhammad Mughira Asad123

The document contains 9 code examples demonstrating the creation of graphical user interfaces (GUIs) using Java AWT and Swing. The examples show how to: 1. Create a basic frame with layouts and add components like buttons and labels 2. Use different layout managers like GridLayout 3. Add action listeners to buttons to trigger events 4. Use layouts like null layout and set component bounds 5. Change background color on button click 6. Take user input from text fields and display output 7. Create a calculator GUI with multiple panels 8. Demonstrate checkbox selection in Swing 9. Add and remove components dynamically in Swing

Uploaded by

abdul22momin
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/ 12

(1)

import java.awt.*;

//creating class
class GUI
{
GUI(){
initGUI();
}
//creating method for gui interface
void initGUI()
{
//step 2 creating Frame
Frame frame=new Frame("Online Class GUI");
// step 3 setting the layout
frame.setLayout(new FlowLayout());
//step 4 visibility and size
frame.setSize(300,300);
frame.setVisible(true);
}

}
class GUIEx
{
public static void main(String[] args)
{
GUI obj1=new GUI();
}
}

(2)

import java.awt.*;

import javax.swing.*;

public class MyGridLayout{

JFrame f;

MyGridLayout(){

f=new JFrame();

JButton b1=new JButton("1");

JButton b2=new JButton("2");


JButton b3=new JButton("3");

JButton b4=new JButton("4");

JButton b5=new JButton("5");

JButton b6=new JButton("6");

JButton b7=new JButton("7");

JButton b8=new JButton("8");

JButton b9=new JButton("9");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);

f.add(b6);f.add(b7);f.add(b8);f.add(b9);

f.setLayout(new GridLayout(3,3));

//setting grid layout of 3 rows and 3 columns

f.setSize(300,300);

f.setVisible(true);

public static void main(String[] args) {

new MyGridLayout();

(3)

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class GuiExample{
GuiExample(){
gui();}
public void gui(){
Frame f=new Frame();
f.setLayout(null);
TextField t=new TextField("");
TextField t1=new TextField("");
Button b=new Button("Button");
Label l=new Label("lable");
Label l1=new Label("Loan Amount");
Label l2=new Label("Loan Interest");
l1.setBounds(50,80,80,20);
l2.setBounds(50,110,80,20);
t.setBounds(150,80,80,20);
t1.setBounds(150,110,80,20);
b.setBounds(125,170,50,50);
l.setBounds(125,230,80,80);
f.add(l);f.add(b);f.add(t);f.add(t1);f.add(l1);f.add(l2);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s=t.getText();
double d=Double.parseDouble(s);
String s1=t1.getText();
double d1=Double.parseDouble(s1);
l.setText(""+(d*(d1/100)));
}});
}
public static void main(String[] args){
GuiExample g=new GuiExample();
}}

(4)

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class GuiExample{
GuiExample(){
gui();}
public void gui(){
Frame f=new Frame();
f.setLayout(new GridLayout(6,1));
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
Panel p6=new Panel();

Label l1=new Label("Roll No");


Label l2=new Label("Name");
Label l3=new Label("Gender");
Label l4=new Label("Email");
Label l5=new Label("Adress");

TextField t1=new TextField("213");


TextField t2=new TextField("xyz");

Checkbox t3=new Checkbox("male");


Checkbox t4=new Checkbox();
Checkbox box=new Checkbox("female");

TextField t5=new TextField(30);


Button b1=new Button("Submit");
Button b2=new Button("Cancel");
//Label l4=new Label();

f.add(p1);f.add(p2);f.add(p3);f.add(p4);f.add(p5);f.add(p6);p1.add(l1);p1.add(t1);p2.add(l2);p2.add(t2
);p3.add(l3);p3.add(t3);p3.add(box);p4.add(l4);p4.add(t4);
p5.add(l5);p5.add(t5);p6.add(b1);p6.add(b2);
f.setSize(450,450);
f.setVisible(true);
/*f.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s3=t3.getText();
String s=t1.getText();
double d=Double.parseDouble(s);
String s1=t2.getText();
double d1=Double.parseDouble(s1);
if(s3.equals("+")){l4.setText(""+(d+d1));}
if(s3.equals("-")){l4.setText(""+(d-d1));}
if(s3.equals("*")){l4.setText(""+(d*d1));}
if(s3.equals("/")){l4.setText(""+(d/d1));}
if(s3.equals("%")){l4.setText(""+((d/d1)*100));}
if(s3.equals("^")){l4.setText(""+(java.lang.Math.pow(d,d1)));}
}});*/
}
public static void main(String[] args){
GuiExample g=new GuiExample();
}}
(5)

mport 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()*256));
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();
}
}

(6)

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
//import java.awt.event.WindowListener;
import java.awt.event.ActionListener;
class GUI_event{

GUI_event()
{
gui();
}
void gui()
{
Frame frame = new Frame("Lab Friday");
frame.setLayout(null);
TextField tfd=new TextField();
Button btn1= new Button("Button");
Label lbl = new Label("0");
tfd.setBounds(125,80,80,20);
btn1.setBounds(125,170,50,50);
lbl.setBounds(125,230,80,80);
frame.add(tfd);
frame.add(btn1);
frame.add(lbl);
// Add Action Listener to the
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String val=tfd.getText();
lbl.setText("" + val);

}
});
// 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 bound_ex
{
public static void main(String[] args)
{
GUI_event gi=new GUI_event();
}
}

(7)

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

(8)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class GUISwing implements ActionListener


{
JFrame frame;
JLabel lbl,lbl1;
JButton btn,btn1;
JCheckBox box1,box2;
GUISwing()
{
guiinit();
}
void guiinit()
{
frame=new JFrame("Swing GUI");
frame.setLayout(new FlowLayout());
lbl=new JLabel("Creating the Swing Components");
btn=new JButton("Button");
lbl1=new JLabel("Creating New Label");
btn1=new JButton("New Button ");
box1=new JCheckBox("C");
box2=new JCheckBox("Java");

btn.addActionListener(this);
btn1.addActionListener(this);

frame.add(box1);
frame.add(lbl);
frame.add(btn);

frame.add(box2);
frame.add(lbl1);
frame.add(btn1);

frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String b1="", b2="";

if(e.getSource()==btn)
{
if(box1.isSelected())
{
b1="C";
}
else
{
b1="not selected";
}

lbl.setText(b1);

}
else if(e.getSource()==btn1)
{
if(box2.isSelected())
{
b2="Java";
}
else
{
b2="not selected";
}

lbl1.setText(b2);

}
JOptionPane.showMessageDialog(frame,b1+"\n "+b2);

}
}

class MainGUISwing
{
public static void main(String [] args)
{
GUISwing obj=new GUISwing();
}

}
(9)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Gui3 implements ActionListener{
JFrame f;
JCheckBox b1,b2;
JButton b;
Gui3(){gui();}
void gui(){
f=new JFrame();
f.setLayout(new FlowLayout());
b1=new JCheckBox("Male");
b2=new JCheckBox("Female");
b=new JButton("click");

b.addActionListener(this);

f.add(b1);f.add(b2);f.add(b);
f.setSize(250,250);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e){


if(b1.isSelected()){JOptionPane.showMessageDialog(f,"congratulation you are male");}
if(b2.isSelected()){JOptionPane.showMessageDialog(f,"oh you are female");}
}

public static void main(String[] args){


Gui3 g=new Gui3();
}
}

(10)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Gui3 implements ActionListener{
JFrame f;
JCheckBox b1,b2,b3;
JButton b;
Gui3(){gui();}
void gui(){
f=new JFrame();
f.setLayout(new FlowLayout());
b1=new JCheckBox("pizaa");
b2=new JCheckBox("burger");
b3=new JCheckBox("tea");
b=new JButton("click");

b.addActionListener(this);

f.add(b1);f.add(b2);f.add(b);f.add(b3);
f.setSize(250,250);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e){


double amount=0;
if(b1.isSelected()){amount+=100;}
if(b2.isSelected()){amount+=50;}
if(b3.isSelected()){amount+=30;}
JOptionPane.showMessageDialog(f,"Total amount:"+amount);
}

public static void main(String[] args){


Gui3 g=new Gui3();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy