Advertisement
apl-mhd

spring 2017 Problem5

Jan 12th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package spring2017;
  2.  
  3. import producerConsumer.Producer;
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. public class Problem5 extends JFrame implements ActionListener {
  11.  
  12.     @Override
  13.     public void actionPerformed(ActionEvent e) {
  14.  
  15.         if(e.getSource()==button){
  16.  
  17.             try {
  18.  
  19.                  sum += Double.parseDouble(textField.getText());
  20.  
  21.             }
  22.             catch (Exception f){
  23.                 sum =sum;
  24.                 JOptionPane.showMessageDialog(null, "Please enter valid number");
  25.             }
  26.  
  27.             textField.setText("");
  28.  
  29.            labelSum.setText("Total Sale:"+Double.toString(sum));
  30.         }
  31.     }
  32.  
  33.  
  34.     JLabel label, labelSum;
  35.     JButton button;
  36.     JTextField textField;
  37.  
  38.  
  39.     double sum=0.0;
  40.  
  41.     Problem5(){
  42.  
  43.         setLayout(new FlowLayout());
  44.  
  45.         label = new JLabel("Enter sale");
  46.         textField = new JTextField(20);
  47.         button = new JButton("Record");
  48.         labelSum = new JLabel("Total sales: "+sum);
  49.  
  50.  
  51.         button.addActionListener(this);
  52.         add(label);add(textField); add(button); add(labelSum);
  53.  
  54.         setSize(400,400);
  55.         setVisible(true);
  56.  
  57.     }
  58.  
  59.     public static void main(String[] args) {
  60.  
  61.  
  62.         new Problem5();
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement