Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package questions;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JTextField;
  12. import javax.swing.JButton;
  13.  
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16.  
  17. import liste.Maillon;
  18.  
  19. public class Calculer extends JFrame implements ActionListener {
  20.  
  21.     private JPanel contentPane;
  22.     private JTextField champMontant;
  23.     private JTextField champTps;
  24.     private JTextField champTvq;
  25.     private JTextField champTotal;
  26.  
  27.     /**
  28.      * Launch the application.
  29.      */
  30.     public static void main(String[] args) {
  31.         EventQueue.invokeLater(new Runnable() {
  32.             public void run() {
  33.                 try {
  34.                     Calculer frame = new Calculer();
  35.                     frame.setVisible(true);
  36.                 } catch (Exception e) {
  37.                     e.printStackTrace();
  38.                 }
  39.             }
  40.         });
  41.     }
  42.  
  43.     /**
  44.      * Create the frame.
  45.      */
  46.     public Calculer() {
  47.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.         setBounds(100, 100, 350, 160);
  49.         contentPane = new JPanel();
  50.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  51.         setContentPane(contentPane);
  52.         contentPane.setLayout(null);
  53.        
  54.         JLabel etiquetteMontant = new JLabel("Montant :");
  55.         etiquetteMontant.setBounds(6, 6, 61, 16);
  56.         contentPane.add(etiquetteMontant);
  57.        
  58.         champMontant = new JTextField();
  59.         champMontant.setBounds(79, 0, 68, 28);
  60.         contentPane.add(champMontant);
  61.         champMontant.setColumns(10);
  62.         champMontant.addActionListener(this);
  63.        
  64.         JLabel etiquetteTps = new JLabel("TPS :");
  65.         etiquetteTps.setBounds(6, 45, 61, 16);
  66.         contentPane.add(etiquetteTps);
  67.        
  68.         champTps = new JTextField();
  69.         champTps.setEditable(false);
  70.         champTps.setBounds(79, 40, 68, 28);
  71.         contentPane.add(champTps);
  72.         champTps.setColumns(10);
  73.        
  74.         JLabel etiquetteTvq = new JLabel("TVQ :");
  75.         etiquetteTvq.setBounds(6, 88, 61, 16);
  76.         contentPane.add(etiquetteTvq);
  77.        
  78.         champTvq = new JTextField();
  79.         champTvq.setEditable(false);
  80.         champTvq.setBounds(79, 82, 68, 28);
  81.         contentPane.add(champTvq);
  82.         champTvq.setColumns(10);
  83.        
  84.         JLabel etiquetteTotal = new JLabel("Total :");
  85.         etiquetteTotal.setBounds(195, 6, 48, 16);
  86.         contentPane.add(etiquetteTotal);
  87.        
  88.         champTotal = new JTextField();
  89.         champTotal.setEditable(false);
  90.         champTotal.setBounds(255, 0, 68, 28);
  91.         contentPane.add(champTotal);
  92.         champTotal.setColumns(10);
  93.        
  94.         JButton boutonCalculer = new JButton("Calculer");
  95.         boutonCalculer.addActionListener(this);
  96.         boutonCalculer.setBounds(205, 41, 117, 29);
  97.         contentPane.add(boutonCalculer);
  98.     }
  99.    
  100.     @Override
  101.     public void actionPerformed(ActionEvent e) {
  102.         if (champMontant.getText() != null) {
  103.             try {
  104.                 double mt = Double.parseDouble(champMontant.getText());
  105.                 if (mt < 0.0) {
  106.                     throw new NumberFormatException("Le montant doit être positif ou nul");
  107.                 }
  108.                 double tps = mt * 0.05;
  109.                 double tvq = mt * 0.09975;
  110.                 double total = mt + tps + tvq;
  111.                 champTps.setText(String.format("%.2f", tps));
  112.                 champTvq.setText(String.format("%.2f", tvq));
  113.                 champTotal.setText(String.format("%.2f", total));
  114.             } catch (NumberFormatException ex) {
  115.                 JOptionPane.showMessageDialog(champMontant, ex.getMessage());
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement