Guest User

Untitled

a guest
Sep 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package ru.mirea.rybchenko.lab5;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class Rabota extends JFrame {
  9. JTextField jta1 = new JTextField(10);
  10. JTextField jta2 = new JTextField(10);
  11. JButton button1 = new JButton(" + ");
  12. JButton button2 = new JButton(" - ");
  13. JButton button3 = new JButton(" * ");
  14. JButton button4 = new JButton(" / ");
  15. JButton button5 = new JButton(" x2 ");
  16. JButton button6 = new JButton(" sqr ");
  17. Font font = new Font("Times new roman",Font.BOLD,100);
  18.  
  19. Rabota(){
  20. super("Calc");
  21. setLayout(new FlowLayout());
  22. setSize(400,200);
  23. add(new JLabel("Number A"));
  24. add (jta1);
  25. add (new JLabel("Number B"));
  26. add (jta2);
  27. add(button1);
  28. add(button2);
  29. add(button3);
  30. add(button4);
  31. add(button5);
  32. add(button6);
  33.  
  34. button1.addActionListener(new ActionListener() {
  35. @Override
  36. public void actionPerformed(ActionEvent e) {
  37. try{
  38. double x1 = Double.parseDouble(jta1.getText().trim());
  39. double x2 = Double.parseDouble(jta2.getText().trim());
  40. JOptionPane.showMessageDialog(null, "Result = "+(x1+x2), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  41. }
  42. catch (Exception e1){
  43. JOptionPane.showMessageDialog(null, "Incorrect number.\nTry again!", "Attention!", JOptionPane.ERROR_MESSAGE);
  44. }
  45. }
  46. });
  47.  
  48. button2.addActionListener(new ActionListener() {
  49. @Override
  50. public void actionPerformed(ActionEvent e) {
  51. try{
  52. double x1 = Double.parseDouble(jta1.getText().trim());
  53. double x2 = Double.parseDouble(jta2.getText().trim());
  54. JOptionPane.showMessageDialog(null, "Result = "+(x1-x2), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  55. }
  56. catch (Exception e2){
  57. JOptionPane.showMessageDialog(null,"Incorrect number.\nTry again!","Attention!",JOptionPane.ERROR_MESSAGE);
  58. }
  59. }
  60. });
  61.  
  62. button3.addActionListener(new ActionListener() {
  63. @Override
  64. public void actionPerformed(ActionEvent e) {
  65. try{
  66. double x1 = Double.parseDouble(jta1.getText().trim());
  67. double x2 = Double.parseDouble(jta2.getText().trim());
  68. JOptionPane.showMessageDialog(null, "Result = "+(x1*x2), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  69. }
  70. catch (Exception e3){
  71. JOptionPane.showMessageDialog(null,"Incorrect number.\nTry again!","Attention!",JOptionPane.ERROR_MESSAGE);
  72. }
  73. }
  74. });
  75.  
  76. button4.addActionListener(new ActionListener() {
  77. @Override
  78. public void actionPerformed(ActionEvent e) {
  79. try{
  80. double x1 = Double.parseDouble(jta1.getText().trim());
  81. double x2 = Double.parseDouble(jta2.getText().trim());
  82. JOptionPane.showMessageDialog(null, "Result = "+(x1/x2), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  83. }
  84. catch (Exception e4){
  85. JOptionPane.showMessageDialog(null,"Incorrect number.\nTry again!","Attention!",JOptionPane.ERROR_MESSAGE);
  86. }
  87. }
  88. });
  89.  
  90. button5.addActionListener(new ActionListener() {
  91. @Override
  92. public void actionPerformed(ActionEvent e) {
  93. try{
  94. double x1 = Double.parseDouble(jta1.getText().trim());
  95. double x2 = Double.parseDouble(jta2.getText().trim());
  96. JOptionPane.showMessageDialog(null, "Result = "+(Math.pow(x1,2)), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  97. }
  98. catch (Exception e5){
  99. JOptionPane.showMessageDialog(null,"Incorrect number.\nTry again!","Attention!",JOptionPane.ERROR_MESSAGE);
  100. }
  101. }
  102. });
  103.  
  104. button1.addActionListener(new ActionListener() {
  105. @Override
  106. public void actionPerformed(ActionEvent e) {
  107. try{
  108. double x1 = Double.parseDouble(jta1.getText().trim());
  109. double x2 = Double.parseDouble(jta2.getText().trim());
  110. JOptionPane.showMessageDialog(null, "Result = "+(Math.sqrt(x1)), "Attention!", JOptionPane.INFORMATION_MESSAGE);
  111. }
  112. catch (Exception e6){
  113. JOptionPane.showMessageDialog(null,"Incorrect number.\nTry again!","Attention!",JOptionPane.ERROR_MESSAGE);
  114. }
  115. }
  116. });
  117.  
  118. setVisible(true);
  119.  
  120. }
  121.  
  122. public static void main(String[] args) {
  123. new Rabota();
  124. }
  125.  
  126.  
  127. }
Add Comment
Please, Sign In to add comment