Advertisement
thotfrnk

SwingArithmetics.java

Jun 7th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. // school work
  2. import java.awt.*; //using awt's layouts
  3. import java.awt.event.*; //using awt's event classes and listener interfaces
  4. import javax.swing.*; //using swing's components and container
  5.  
  6. //a swing application extends from javax.swing.JFrame
  7.  
  8. public class SwingArithmetics extends JFrame {
  9.     private JTextField tfNumber1, tfNumber2, tfResult;
  10.     private JButton btnAdd, btnSub, btnMulti, btnDiv, btnRemain, btnClear;
  11.     private double number1, number2, result;
  12.  
  13.     //constructor to set up ui components and event handlers
  14.  
  15.     public SwingArithmetics() {
  16.         //swing components should be added to the content-pane of the JFrame
  17.         Container cp = getContentPane();
  18.         //set this container to grid layout of 6 rows and 2 columns
  19.         cp.setLayout(new GridLayout(6, 2, 10, 3));
  20.  
  21.         //components are added left-to-right, top-to-bottom
  22.         cp.add(new JLabel("First Number "));
  23.         tfNumber1 = new JTextField(10);
  24.         tfNumber1.setHorizontalAlignment(JTextField.RIGHT);
  25.         cp.add(tfNumber1);
  26.  
  27.         cp.add(new JLabel("Second Number "));
  28.         tfNumber2 = new JTextField(10);
  29.         tfNumber2.setHorizontalAlignment(JTextField.RIGHT);
  30.         cp.add(tfNumber2);
  31.  
  32.         cp.add(new JLabel("Result "));
  33.         tfResult = new JTextField(10);
  34.         tfResult.setHorizontalAlignment(JTextField.RIGHT);
  35.         cp.add(tfResult);
  36.  
  37.         btnAdd = new JButton("+");
  38.         cp.add(btnAdd);
  39.         btnAdd.addActionListener(new ActionListener() {
  40.             @Override
  41.             public void actionPerformed(ActionEvent evt) {
  42.                 number1 = Integer.parseInt(tfNumber1.getText());
  43.                 number2 = Integer.parseInt(tfNumber2.getText());
  44.  
  45.                 result = number1 + number2; //to calculate the sum
  46.  
  47.                 tfResult.setText(result + "");
  48.             }
  49.         });
  50.  
  51.         btnSub = new JButton("-");
  52.         cp.add(btnSub);
  53.         btnSub.addActionListener(new ActionListener() {
  54.             @Override
  55.             public void actionPerformed(ActionEvent evt) {
  56.                 number1 = Integer.parseInt(tfNumber1.getText());
  57.                 number2 = Integer.parseInt(tfNumber2.getText());
  58.  
  59.                 result = number1 - number2;
  60.  
  61.                 tfResult.setText(result + "");
  62.             }
  63.         });
  64.  
  65.         btnMulti = new JButton("*");
  66.         cp.add(btnMulti);
  67.         btnMulti.addActionListener(new ActionListener() {
  68.             @Override
  69.             public void actionPerformed(ActionEvent evt) {
  70.                 number1 = Integer.parseInt(tfNumber1.getText());
  71.                 number2 = Integer.parseInt(tfNumber2.getText());
  72.  
  73.                 result = number1 * number2;
  74.  
  75.                 tfResult.setText(result + "");
  76.             }
  77.         });
  78.  
  79.         btnDiv = new JButton("/");
  80.         cp.add(btnDiv);
  81.         btnDiv.addActionListener(new ActionListener() {
  82.             @Override
  83.             public void actionPerformed(ActionEvent evt) {
  84.                 number1 = Integer.parseInt(tfNumber1.getText());
  85.                 number2 = Integer.parseInt(tfNumber2.getText());
  86.  
  87.                 result = number1 / number2;
  88.  
  89.                 tfResult.setText(result + "");
  90.             }
  91.         });
  92.  
  93.         btnRemain = new JButton("%");
  94.         cp.add(btnRemain);
  95.         btnRemain.addActionListener(new ActionListener() {
  96.             @Override
  97.             public void actionPerformed(ActionEvent evt) {
  98.                 number1 = Integer.parseInt(tfNumber1.getText());
  99.                 number2 = Integer.parseInt(tfNumber2.getText());
  100.  
  101.                 result = number1 % number2;
  102.  
  103.                 tfResult.setText(result + "");
  104.             }
  105.         });
  106.  
  107.         btnClear = new JButton("CLEAR");
  108.         cp.add(btnClear);
  109.         btnClear.addActionListener(new ActionListener() {
  110.             @Override
  111.             public void actionPerformed(ActionEvent evt) {
  112.  
  113.                 tfNumber1.setText(""); //to clear num 1 field
  114.                 tfNumber2.setText(""); //to clear num 2 field
  115.                 tfResult.setText(""); //to clear result field
  116.             }
  117.         });
  118.  
  119.         setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
  120.         setTitle("Swing Arithmetics");
  121.         setSize(300, 170);
  122.         setVisible(true);
  123.     }
  124.  
  125.     // The entry main() method
  126.     public static void main(String[] args) {
  127.         // For thread safety, use the event-dispatching thread to construct UI
  128.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  129.             @Override
  130.             public void run() {
  131.                 new SwingArithmetics(); // Let the constructor does the job
  132.             }
  133.         });
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement