Advertisement
thotfrnk

swingadder.java

Jun 7th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 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. public class SwingAdder extends JFrame {
  8.     private JTextField tfNumber1, tfNumber2, tfResult;
  9.     private JButton btnAdd, btnClear;
  10.     private int number1, number2, result;
  11.  
  12.     // Constructor to set up UI components and event handlers
  13.     public SwingAdder() {
  14.         // Swing components should be added to the content-pane of the JFrame.
  15.         Container cp = getContentPane();
  16.         // Set this Container to grid layout of 4 rows and 2 columns
  17.         cp.setLayout(new GridLayout(4, 2, 10, 3));
  18.  
  19.         // Components are added from left-to-right, top-to-bottom
  20.         cp.add(new JLabel("First Number "));     // at (1, 1)
  21.         tfNumber1 = new JTextField(10);
  22.         tfNumber1.setHorizontalAlignment(JTextField.RIGHT);
  23.         cp.add(tfNumber1);                       // at (1, 2)
  24.  
  25.         cp.add(new JLabel("Second Number "));
  26.         tfNumber2 = new JTextField(10);
  27.         tfNumber2.setHorizontalAlignment(JTextField.RIGHT);
  28.         cp.add(tfNumber2);
  29.  
  30.         cp.add(new JLabel("Result "));
  31.         tfResult = new JTextField(10);
  32.         tfResult.setHorizontalAlignment(JTextField.RIGHT);
  33.         cp.add(tfResult);
  34.  
  35.         btnAdd = new JButton("ADD");
  36.         cp.add(btnAdd);                         // at (4, 1)
  37.         btnAdd.addActionListener(new ActionListener() {
  38.             @Override
  39.             public void actionPerformed(ActionEvent evt) {
  40.                 number1 = Integer.parseInt(tfNumber1.getText());
  41.                 number2 = Integer.parseInt(tfNumber2.getText());
  42.  
  43.                 result = number1 + number2; //to calculate the sum
  44.  
  45.                 tfResult.setText(result + "");
  46.             }
  47.         });
  48.  
  49.         btnClear = new JButton("CLEAR");
  50.         cp.add(btnClear);                          // at (4, 2)
  51.         btnClear.addActionListener(new ActionListener() {
  52.             @Override
  53.             public void actionPerformed(ActionEvent evt) {
  54.  
  55.                 tfNumber1.setText(""); //to clear num 1 field
  56.                 tfNumber2.setText(""); //to clear num 2 field
  57.                 tfResult.setText(""); //to clear result field
  58.             }
  59.         });
  60.  
  61.         setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
  62.         setTitle("Swing Adder");
  63.         setSize(300, 170);
  64.         setVisible(true);
  65.     }
  66.  
  67.     // The entry main() method
  68.     public static void main(String[] args) {
  69.         // For thread safety, use the event-dispatching thread to construct UI
  70.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  71.             @Override
  72.             public void run() {
  73.                 new SwingAdder(); // Let the constructor does the job
  74.             }
  75.         });
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement