Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package calculator;
  2. import java.awt.*;
  3. import java.awt.Font;
  4. import java.awt.event.*;
  5.  
  6. public class Calculator implements ActionListener{
  7. //Declaring Objects
  8. Frame f=new Frame();
  9. Label nom1=new Label("First Number");
  10. Label nom2=new Label("Second Number");
  11. Label result=new Label("Result");
  12.  
  13. TextField t1=new TextField();
  14. TextField t2=new TextField();
  15. TextField t3=new TextField();
  16.  
  17. Button add=new Button("Add");
  18. Button sub=new Button("Sub");
  19. Button mul=new Button("Mul");
  20. Button div=new Button("Div");
  21. Button cancel=new Button("Cancel");
  22. private Object label;
  23.  
  24. Calculator()
  25. {
  26. //Adding font
  27. Font myFont= new Font("Serif",Font.PLAIN,15);
  28. nom1.setFont(myFont);
  29. Font myFont2= new Font("Helvetica",Font.PLAIN,15);
  30. nom2.setFont(myFont2);
  31. Font myFont3= new Font("Serif",Font.PLAIN,15);
  32. result.setFont(myFont3);
  33.  
  34. //Giving Coordinates
  35. nom1.setBounds(50,100,100,20);
  36. nom2.setBounds(50,140,120,20);
  37. result.setBounds(50,180,100,20);
  38.  
  39. t1.setBounds(200,100,100,20);
  40. t2.setBounds(200,140,100,20);
  41. t3.setBounds(200,180,100,20);
  42.  
  43. add.setBounds(50,250,50,20);
  44. sub.setBounds(110,250,50,20);
  45. mul.setBounds(170,250,50,20);
  46. div.setBounds(230,250,50,20);
  47. cancel.setBounds(290,250,50,20);
  48.  
  49. //Adding Components to the Frame
  50. f.add(nom1);
  51. f.add(nom2);
  52. f.add(result);
  53.  
  54. f.add(t1);
  55. f.add(t2);
  56. f.add(t3);
  57.  
  58. f.add(add);
  59. f.add(sub);
  60. f.add(mul);
  61. f.add(div);
  62. f.add(cancel);
  63.  
  64. f.setBackground(Color.ORANGE);
  65.  
  66. add.addActionListener(this);
  67. sub.addActionListener(this);
  68. mul.addActionListener(this);
  69. div.addActionListener(this);
  70. cancel.addActionListener(this);
  71.  
  72. f.setLayout(null);
  73. f.setVisible(true);
  74. f.setSize(400,350);
  75. f.setLocation(500,200);
  76. }
  77. public void actionPerformed(ActionEvent e)
  78. {
  79. int n1=Integer.parseInt(t1.getText());
  80. int n2=Integer.parseInt(t2.getText());
  81.  
  82. if(e.getSource()==add)
  83. {
  84. t3.setText(String.valueOf(n1+n2));
  85. }
  86. if(e.getSource()==sub)
  87. {
  88. t3.setText(String.valueOf(n1-n2));
  89. }
  90. if(e.getSource()==mul)
  91. {
  92. t3.setText(String.valueOf(n1*n2));
  93. }
  94. if(e.getSource()==div)
  95. {
  96. t3.setText(String.valueOf(n1/n2));
  97. }
  98. if(e.getSource()==cancel)
  99. {
  100. System.exit(0);
  101. }
  102. }
  103. public static void main(String[] args) {
  104. new Calculator();
  105. }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement