You are on page 1of 10

SACHIN PUNDIR ROLL NO:- 29 MCA III B

PROBLEM STATEMENT:-WRITE A JAVA PROGRAM TO WELCOME THE USER OR


SAYING HELLO TO THE USER
CODING:-
package firstgui;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swing


{
public static void main(String[] args)
{
Duo d=new Duo();
}
}
class Duo extends JFrame
{
JLabel l1=null;
JLabel l2=null;
Duo()
{
l1=new JLabel("Welcome to the First Swing Program");
l2=new JLabel("Hello from Graphice Era Hill University");
add(l1);
add(l2);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
}
SACHIN PUNDIR ROLL NO:- 29 MCA III B
SACHIN PUNDIR ROLL NO:- 29 MCA III B

PROBLEM STATEMENT:-WRITE A JAVA PROGRAM TO ADD TWO INTEGERS USING


SWING.
CODING:-
package firstgui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Add


{
public static void main(String[] args)
{
Dummy d=new Dummy();
}
}
class Dummy extends JFrame implements ActionListener
{
JLabel l1=null;
JLabel l2=null,l3;
JTextField t1,t2;
JButton b1;
Dummy()
{
l1=new JLabel("Enter the first number");
l2=new JLabel("Enter the second number");
t1=new JTextField(20);
t2=new JTextField(20);
l3=new JLabel();
b1=new JButton("SUM");
SACHIN PUNDIR ROLL NO:- 29 MCA III B

add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(l3);
b1.addActionListener(this);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
int sum=n1+n2;
l3.setText("Sum of two integers are "+sum);

}
}
SACHIN PUNDIR ROLL NO:- 29 MCA III B

PROBLEM STATEMENT:-WRITE A JAVA PROGRAM TO ADD AND SUB TWO INTEGERS


USING SWINGG.
CODING:-
package firstgui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Swing1


{
public static void main(String[] args)
{
Test t=new Test();
}
}
class Test extends JFrame implements ActionListener
{
JLabel l1=null;
JLabel l2=null,l3;
JTextField t1,t2;
JButton b1,b2;
Test()
{
l1=new JLabel("Enter the first number");
l2=new JLabel("Enter the second number");
l3=new JLabel();
t1=new JTextField(20);
t2=new JTextField(20);
SACHIN PUNDIR ROLL NO:- 29 MCA III B

b1=new JButton("ADD");
b2=new JButton("SUB");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(l3);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
int res;
if(e.getSource()==b1)
{
res=n1+n2;
l3.setText("Result of two integers are "+res);
}
else
{
res=n1-n2;
l3.setText("Result of two integers are "+res);
}
SACHIN PUNDIR ROLL NO:- 29 MCA III B

}
SACHIN PUNDIR ROLL NO:- 29 MCA III B

PROBLEM STATEMENT:- Write a java program for making calculator.


OBJECTIVE: To Understand the concept of JFRAME,SWINGG,JFIELD.
package firstgui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FirstGui


{
public static void main(String[] args) throws Exception
{
Demo d=new Demo();
}
}
class Demo extends JFrame implements ActionListener
{
JLabel l1=null;
JLabel l2=null,l3;
JTextField t1,t2;
JButton b1,b2,b3,b4;
Demo()
{
l1=new JLabel("Enter First Number");
l2=new JLabel("Enter Second Number");
l3=new JLabel();
t1=new JTextField(10);
t2=new JTextField(10);
l3=new JLabel();
SACHIN PUNDIR ROLL NO:- 29 MCA III B

b1=new JButton("ADD");
b2=new JButton("SUB");
b3=new JButton("MUL");
b4=new JButton("DIV");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent e)
{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
int res;
if(e.getSource()==b1)
{
res=n1+n2;
l3.setText("Addition of two integers are "+res);
}
SACHIN PUNDIR ROLL NO:- 29 MCA III B

else if(e.getSource()==b2)
{
res=n1-n2;
l3.setText("Subtraction of two integers are "+res);
}
else if(e.getSource()==b3)
{
res=n1*n2;
l3.setText("Multiplication of two integers are "+res);
}
else
{
res=n1/n2;
l3.setText("Divison of two integers are "+res);
}
}

You might also like