import java.awt.event.*;
import javax.swing.*;
public class ButtonWithAction {
static String firstnum, secondnum, summ;
static float sum, firstn, secondn;
public static void main(String[] args) {
JFrame f=new JFrame("Calculator");
JLabel l1=new JLabel("First number : ");
JLabel l2=new JLabel("Second number : ");
JLabel l3=new JLabel("Result : ");
l1.setBounds(50,50, 100,40);
l2.setBounds(50,100, 100,40);
l3.setBounds(50,150, 100,40);
final JTextField tf1=new JTextField();
tf1.setBounds(150,50, 100,40);
final JTextField tf2=new JTextField();
tf2.setBounds(150,100, 100,40);
final JTextField tf3=new JTextField();
tf3.setBounds(150,150, 100,40);
JButton bsum=new JButton("+");
bsum.setBounds(50,200,55,30);
bsum.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn + secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bdiff=new JButton("-");
bdiff.setBounds(120,200,55,30);
bdiff.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn - secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bmul=new JButton("X");
bmul.setBounds(50,250,55,30);
bmul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn*secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bdiv=new JButton("/");
bdiv.setBounds(120,250,55,30);
bdiv.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn/secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bclr=new JButton("C");
bclr.setBounds(100,300,55,30);
bclr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf1.setText("");
tf2.setText("");
//firstn=Float.parseFloat(firstnum);
//secondn=Float.parseFloat(secondnum);
//sum=firstn/secondn;
//summ=String.valueOf(sum);
tf3.setText("");
}
});
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(tf2);
f.add(l3);
f.add(tf3);
f.add(bsum);
f.add(bdiff);
f.add(bmul);
f.add(bdiv);
f.add(bclr);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
//f.setBackground(Color.yellow);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output
Thanks
Happy programming!
import javax.swing.*;
public class ButtonWithAction {
static String firstnum, secondnum, summ;
static float sum, firstn, secondn;
public static void main(String[] args) {
JFrame f=new JFrame("Calculator");
JLabel l1=new JLabel("First number : ");
JLabel l2=new JLabel("Second number : ");
JLabel l3=new JLabel("Result : ");
l1.setBounds(50,50, 100,40);
l2.setBounds(50,100, 100,40);
l3.setBounds(50,150, 100,40);
final JTextField tf1=new JTextField();
tf1.setBounds(150,50, 100,40);
final JTextField tf2=new JTextField();
tf2.setBounds(150,100, 100,40);
final JTextField tf3=new JTextField();
tf3.setBounds(150,150, 100,40);
JButton bsum=new JButton("+");
bsum.setBounds(50,200,55,30);
bsum.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn + secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bdiff=new JButton("-");
bdiff.setBounds(120,200,55,30);
bdiff.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn - secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bmul=new JButton("X");
bmul.setBounds(50,250,55,30);
bmul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn*secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bdiv=new JButton("/");
bdiv.setBounds(120,250,55,30);
bdiv.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
firstnum=tf1.getText();
secondnum=tf2.getText();
firstn=Float.parseFloat(firstnum);
secondn=Float.parseFloat(secondnum);
sum=firstn/secondn;
summ=String.valueOf(sum);
tf3.setText(summ);
}
});
JButton bclr=new JButton("C");
bclr.setBounds(100,300,55,30);
bclr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf1.setText("");
tf2.setText("");
//firstn=Float.parseFloat(firstnum);
//secondn=Float.parseFloat(secondnum);
//sum=firstn/secondn;
//summ=String.valueOf(sum);
tf3.setText("");
}
});
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(tf2);
f.add(l3);
f.add(tf3);
f.add(bsum);
f.add(bdiff);
f.add(bmul);
f.add(bdiv);
f.add(bclr);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
//f.setBackground(Color.yellow);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output
Thanks
Happy programming!
No comments:
Post a Comment