0% found this document useful (0 votes)
5 views11 pages

Java Swings

The document contains multiple Java Swing examples demonstrating the creation of graphical user interfaces (GUIs) using JFrame, JButton, JTextField, and JCheckBox components. It includes examples of simple buttons, calculators, text fields, and checkboxes, showcasing the use of action listeners and event handling. Each example is structured with a main method to run the GUI application.

Uploaded by

encyclopedia2k21
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)
5 views11 pages

Java Swings

The document contains multiple Java Swing examples demonstrating the creation of graphical user interfaces (GUIs) using JFrame, JButton, JTextField, and JCheckBox components. It includes examples of simple buttons, calculators, text fields, and checkboxes, showcasing the use of action listeners and event handling. Each example is structured with a main method to run the GUI application.

Uploaded by

encyclopedia2k21
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

// Creating container JFrame

import [Link].*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


[Link](130,100,100, 40);//x axis, y axis, width, height

[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](true);//making the frame visible
}
}

// JFrame using Constructor

import [Link].*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


[Link](130,100,100, 40);

[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}

public static void main(String[] args) {


new Simple();
}
}

// Using inheritance

import [Link].*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
[Link](130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}

// TextField
import [Link].*;
class TextField
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to ISL");
[Link](50,100, 200,30);
t2=new JTextField("Swings Tutorial");
[Link](50,150, 200,30);
[Link](t1); [Link](t2);
[Link](400,400);
[Link](null);
[Link](true);
}
}

// TextField
import [Link].*;
import [Link].*;
public class TextFieldExample implements ActionListener{
JTextField tf1,tf2,tf3;
JButton b1,b2;
TextFieldExample(){
JFrame f= new JFrame();
tf1=new JTextField();
[Link](50,50,150,20);
tf2=new JTextField();
[Link](50,100,150,20);
tf3=new JTextField();
[Link](50,150,150,20);
[Link](false);
b1=new JButton("+");
[Link](50,200,50,50);
b2=new JButton("-");
[Link](120,200,50,50);
[Link](this);
[Link](this);
[Link](tf1);[Link](tf2);[Link](tf3);[Link](b1);[Link](b2);
[Link](300,300);
[Link](null);
[Link](true);
}
public void actionPerformed(ActionEvent e) {
String s1=[Link]();
String s2=[Link]();
int a=[Link](s1);
int b=[Link](s2);
int c=0;
if([Link]()==b1){
c=a+b;
}else if([Link]()==b2){
c=a-b;
}
String result=[Link](c);
[Link](result);
}
public static void main(String[] args) {
new TextFieldExample();
}}

// Java JButton

import [Link].*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
[Link](50,100,95,30);
[Link](b);
[Link](400,400);
[Link](null);
[Link](true);
}
}

// Java JButton with ActionListener


import [Link].*;
import [Link].*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
[Link](50,50, 150,20);
JButton b=new JButton("Click Here");
[Link](50,100,95,30);
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
[Link]("ISL Engineering College");
}
});
[Link](b);[Link](tf);
[Link](400,400);
[Link](null);
[Link](true);
}
}

//Button
import [Link].*;
import [Link].*;
import [Link].*;
public class ButtonDemo extends JFrame
{
JButton yes,no,close;
JLabel lbl;
ButtonDemo()
{
yes = new JButton("YES");
no = new JButton ("No");
close = new JButton ("CLOSE");
lbl = new JLabel ("");
setLayout (new GridLayout(4,1));
setSize (400,200);
add(yes);
add(no);
add(close);
add(lbl);
setVisible(true);

//setDefaultCloseOperation(JFrame.EXIT_NO_CLOSE);
ButtonHandler bh = new ButtonHandler();
[Link](bh);
[Link](bh);
[Link](bh);
[Link](bh);
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if ([Link]()==yes)
{
[Link]("Button Yes is pressed");
}
if ([Link]()==no)
{
[Link]("Button No is pressed");
}
if ([Link]()==close)
{
[Link](0);
}
}
}
public static void main(String args[])
{
new ButtonDemo();
}
}
//Calculator
import [Link].*;
import [Link].*;
class Calc implements ActionListener
{
JFrame f;
JTextField t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;

static double a=0,b=0,result=0;


static int operator=0;

Calc()
{
f=new JFrame("Calculator");
t=new JTextField();
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=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bdel=new JButton("Delete");
bclr=new JButton("Clear");
[Link](30,40,280,30);
[Link](40,100,50,40);
[Link](110,100,50,40);
[Link](180,100,50,40);
[Link](250,100,50,40);
[Link](40,170,50,40);
[Link](110,170,50,40);
[Link](180,170,50,40);
[Link](250,170,50,40);
[Link](40,240,50,40);
[Link](110,240,50,40);
[Link](180,240,50,40);
[Link](250,240,50,40);
[Link](40,310,50,40);
[Link](110,310,50,40);
[Link](180,310,50,40);
[Link](250,310,50,40);
[Link](60,380,100,40);
[Link](180,380,100,40);
[Link](t);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bdiv);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bmul);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bsub);
[Link](bdec);
[Link](b0);
[Link](beq);
[Link](badd);
[Link](bdel);
[Link](bclr);
[Link](null);
[Link](true);
[Link](350,500);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](false);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}

public void actionPerformed(ActionEvent e)


{
if([Link]()==b1)
[Link]([Link]().concat("1"));
if([Link]()==b2)
[Link]([Link]().concat("2"));
if([Link]()==b3)
[Link]([Link]().concat("3"));
if([Link]()==b4)
[Link]([Link]().concat("4"));
if([Link]()==b5)
[Link]([Link]().concat("5"));
if([Link]()==b6)
[Link]([Link]().concat("6"));
if([Link]()==b7)
[Link]([Link]().concat("7"));
if([Link]()==b8)
[Link]([Link]().concat("8"));
if([Link]()==b9)
[Link]([Link]().concat("9"));
if([Link]()==b0)
[Link]([Link]().concat("0"));
if([Link]()==bdec)
[Link]([Link]().concat("."));
if([Link]()==badd)
{
a=[Link]([Link]());
operator=1;
[Link]("");
}
if([Link]()==bsub)
{
a=[Link]([Link]());
operator=2;
[Link]("");
}
if([Link]()==bmul)
{
a=[Link]([Link]());
operator=3;
[Link]("");
}
if([Link]()==bdiv)
{
a=[Link]([Link]());
operator=4;
[Link]("");
}
if([Link]()==beq)
{
b=[Link]([Link]());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
[Link](""+result);
}
if([Link]()==bclr)
[Link]("");
if([Link]()==bdel)
{
String s=[Link]();
[Link]("");
for(int i=0;i<[Link]()-1;i++)
[Link]([Link]()+[Link](i));
}
}

public static void main(String...s)


{
new Calc();
}
}

// Number Format

import [Link].*;
import [Link].*;
import [Link].*;
class Number_Frame extends JFrame implements ActionListener
{
JLabel textLabel, Label1, Label2;
JTextField Text1, Text2, Text3;
JButton ok, exit;
JPanel pan1, pan2;
int occurrences = 0, i = 0;
public Number_Frame()
{
textLabel = new JLabel("Enter Number ",[Link]);
Label1 = new JLabel("Previous Number ",[Link]);
Label2 = new JLabel("Next Number ",[Link]);
Text1 = new JTextField(20);
Text2 = new JTextField(20);
Text3 = new JTextField(20);
pan1 = new JPanel();
[Link](new GridLayout(4,2));
[Link](textLabel);
[Link](Text1);
[Link](Label1);
[Link](Text2);
[Link](Label2);
[Link](Text3);
ok = new JButton("ok");
exit = new JButton("Exit");
[Link](this);
[Link](this);
pan2 = new JPanel();
[Link](new FlowLayout());
[Link](ok);
[Link](exit);
add(pan1,"Center");
add(pan2,"South");
setTitle("Number Operation");
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
JButton btn = (JButton)[Link]();
if (btn == ok)
{
String f = [Link]();
int s=[Link](f);
s=s+1;
[Link]([Link](s));
int a=[Link](f);
a=a-1;
[Link]([Link](a));
}
if (btn == exit)
{
[Link](0);
}
}
public static void main(String[] args)
{
new Number_Frame();
}
}

// CheckBox
import [Link].*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
[Link](100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
[Link](100,150, 50,50);
[Link](checkBox1);
[Link](checkBox2);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}

// CheckBox
import [Link].*;
import [Link].*;
public class CheckBoxExample1
{
CheckBoxExample1(){
JFrame f= new JFrame("CheckBox Example");
final JLabel label = new JLabel();
[Link]([Link]);
[Link](400,100);
JCheckBox checkbox1 = new JCheckBox("C++");
[Link](150,100, 50,50);
JCheckBox checkbox2 = new JCheckBox("Java");
[Link](150,150, 50,50);
[Link](checkbox1); [Link](checkbox2); [Link](label);
[Link](new ItemListener() {
public void itemStateChanged(ItemEvent e) {
[Link]("C++ Checkbox: "
+ ([Link]()==1?"checked":"unchecked"));
}
});
[Link](new ItemListener() {
public void itemStateChanged(ItemEvent e) {
[Link]("Java Checkbox: "
+ ([Link]()==1?"checked":"unchecked"));
}
});
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new CheckBoxExample1();
}
}

// CheckBox

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

You might also like