Bukisoh

tp3

Nov 18th, 2020
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.26 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2.  
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.border.TitledBorder;
  9. import javax.swing.JTextPane;
  10. import java.awt.Color;
  11. import java.awt.SystemColor;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14.  
  15. import javax.swing.JTextField;
  16. import javax.swing.JRadioButton;
  17. import javax.swing.ButtonGroup;
  18. import javax.swing.JButton;
  19. import javax.swing.JLabel;
  20. import javax.swing.JList;
  21. import javax.swing.JScrollBar;
  22. import javax.swing.border.MatteBorder;
  23. import java.lang.Math;
  24. import javax.swing.JSlider;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.DefaultListModel;
  27.  
  28. public class TPLUCASPANSARDI extends JFrame implements ActionListener {
  29.  
  30.     private JPanel contentPane;
  31.     private JTextField textFieldTaille;
  32.     private JTextField textFieldPoids;
  33.     private final ButtonGroup buttonGroup = new ButtonGroup();
  34.     private JTextField textField_PdsIdéal;
  35.     private JPanel panel_1;
  36.     private JButton btnIMC;
  37.     private JButton btnpoidsid;
  38.     private JTextPane textPane;
  39.     private JTextPane textRsltIMC;
  40.     private JButton btnEffacer;
  41.     private JRadioButton rdbtnHomme;
  42.     private JRadioButton rdbtnFemme;
  43.    
  44.     DefaultListModel<String> dlm;
  45.     private JScrollPane scrollPane;
  46.     private JList<String> list;
  47.  
  48.     /**
  49.      * Launch the application.
  50.      */
  51.     public static void main(String[] args) {
  52.         EventQueue.invokeLater(new Runnable() {
  53.             public void run() {
  54.                 try {
  55.                     TPLUCASPANSARDI frame = new TPLUCASPANSARDI();
  56.                     frame.setVisible(true);
  57.                 } catch (Exception e) {
  58.                     e.printStackTrace();
  59.                 }
  60.             }
  61.         });
  62.     }
  63.  
  64.     /**
  65.      * Create the frame.
  66.      */
  67.     public TPLUCASPANSARDI() {
  68.         setTitle("Masse Corporelle");
  69.         setBackground(Color.WHITE);
  70.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  71.         setBounds(100, 100, 450, 416);
  72.         contentPane = new JPanel();
  73.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  74.         setContentPane(contentPane);
  75.         contentPane.setLayout(null);
  76.        
  77.         JPanel panel = new JPanel();
  78.         panel.setBorder(new TitledBorder(null, "Mes Param\u00E8tres", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  79.         panel.setBounds(10, 11, 414, 85);
  80.         contentPane.add(panel);
  81.         panel.setLayout(null);
  82.        
  83.         JTextPane txtpnTaille = new JTextPane();
  84.         txtpnTaille.setBackground(SystemColor.control);
  85.         txtpnTaille.setText("Taille :");
  86.         txtpnTaille.setBounds(10, 23, 38, 20);
  87.         panel.add(txtpnTaille);
  88.        
  89.         JTextPane txtpnCm = new JTextPane();
  90.         txtpnCm.setText("cm");
  91.         txtpnCm.setBackground(SystemColor.control);
  92.         txtpnCm.setBounds(141, 23, 26, 20);
  93.         panel.add(txtpnCm);
  94.        
  95.         JTextPane txtpnPoids = new JTextPane();
  96.         txtpnPoids.setText("poids");
  97.         txtpnPoids.setBackground(SystemColor.control);
  98.         txtpnPoids.setBounds(177, 23, 37, 20);
  99.         panel.add(txtpnPoids);
  100.        
  101.         JTextPane txtpnKg = new JTextPane();
  102.         txtpnKg.setBackground(SystemColor.control);
  103.         txtpnKg.setText("Kg");
  104.         txtpnKg.setBounds(320, 23, 26, 20);
  105.         panel.add(txtpnKg);
  106.        
  107.         textFieldTaille = new JTextField();
  108.         textFieldTaille.setText("170");
  109.         textFieldTaille.setBounds(53, 23, 78, 20);
  110.         panel.add(textFieldTaille);
  111.         textFieldTaille.setColumns(10);
  112.        
  113.         textFieldPoids = new JTextField();
  114.         textFieldPoids.setText("72");
  115.         textFieldPoids.setBounds(224, 23, 86, 20);
  116.         panel.add(textFieldPoids);
  117.         textFieldPoids.setColumns(10);
  118.        
  119.         rdbtnHomme = new JRadioButton("Homme");
  120.         buttonGroup.add(rdbtnHomme);
  121.         rdbtnHomme.setBounds(47, 50, 86, 23);
  122.         panel.add(rdbtnHomme);
  123.        
  124.         rdbtnFemme = new JRadioButton("Femme");
  125.         buttonGroup.add(rdbtnFemme);
  126.         rdbtnFemme.setBounds(165, 50, 109, 23);
  127.         panel.add(rdbtnFemme);
  128.        
  129.         panel_1 = new JPanel();
  130.         panel_1.setBorder(new TitledBorder(null, "Mes Calculs", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  131.         panel_1.setBounds(10, 104, 414, 143);
  132.         contentPane.add(panel_1);
  133.         panel_1.setLayout(null);
  134.        
  135.         btnIMC = new JButton("IMC");
  136.         btnIMC.setBounds(10, 21, 89, 23);
  137.         panel_1.add(btnIMC);
  138.        
  139.         btnIMC.addActionListener(new ActionListener() {
  140.                 public void actionPerformed(ActionEvent e) {
  141.                     OnIMC();
  142.                 }
  143.             });
  144.        
  145.         JTextPane txtpnInterpretation = new JTextPane();
  146.         txtpnInterpretation.setText("Interpretation");
  147.         txtpnInterpretation.setBackground(SystemColor.control);
  148.         txtpnInterpretation.setBounds(20, 55, 79, 23);
  149.         panel_1.add(txtpnInterpretation);
  150.        
  151.         btnpoidsid = new JButton("poids id\u00E9al");
  152.         btnpoidsid.setBounds(10, 89, 89, 23);
  153.         panel_1.add(btnpoidsid);
  154.         btnpoidsid.addActionListener(new ActionListener() {
  155.             public void actionPerformed(ActionEvent e) {
  156.                 OnPoidsIdeal();
  157.             }
  158.         });
  159.        
  160.         textField_PdsIdéal = new JTextField();
  161.         textField_PdsIdéal.setBounds(129, 90, 86, 20);
  162.         panel_1.add(textField_PdsIdéal);
  163.         textField_PdsIdéal.setColumns(10);
  164.        
  165.         JLabel lblNewLabel = new JLabel("Kg");
  166.         lblNewLabel.setBounds(227, 93, 46, 14);
  167.         panel_1.add(lblNewLabel);
  168.        
  169.         textRsltIMC = new JTextPane();
  170.         textRsltIMC.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
  171.         textRsltIMC.setBackground(Color.YELLOW);
  172.         textRsltIMC.setBounds(129, 21, 98, 20);
  173.         panel_1.add(textRsltIMC);
  174.        
  175.         textPane = new JTextPane();
  176.         textPane.setBackground(Color.GREEN);
  177.         textPane.setBounds(129, 55, 146, 20);
  178.         panel_1.add(textPane);
  179.        
  180.         JPanel panel_2 = new JPanel();
  181.         panel_2.setBorder(new TitledBorder(null, "M\u00E9morisation", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  182.         panel_2.setBounds(10, 258, 414, 108);
  183.         contentPane.add(panel_2);
  184.         panel_2.setLayout(null);
  185.        
  186.         dlm = new DefaultListModel<String>();
  187.         list = new JList<String>(dlm);
  188.         list.setBackground(Color.WHITE);
  189.        
  190.         scrollPane = new JScrollPane(list);
  191.         scrollPane.setBounds(28, 30, 261, 67);
  192.         panel_2.add(scrollPane);
  193.    
  194.    
  195.         btnEffacer = new JButton("Effacer");
  196.         btnEffacer.setBounds(304, 47, 89, 23);
  197.         panel_2.add(btnEffacer);
  198.        
  199.         btnEffacer.addActionListener(new ActionListener() {
  200.             public void actionPerformed(ActionEvent e) {
  201.                 OnEffacer();
  202.             }
  203.         });
  204.        
  205.         textField_PdsIdéal.setEditable(false);
  206.         btnpoidsid.setEnabled(false);
  207.         btnEffacer.setEnabled(false);
  208.         rdbtnHomme.setSelected(true);
  209.        
  210.     }
  211.    
  212.      
  213.     public void  OnPoidsIdeal() {
  214.        
  215.         String s1=textFieldTaille.getText();
  216.         double d1=Double.parseDouble(s1);      
  217.  
  218.         if(rdbtnHomme.isSelected()) {
  219.             textField_PdsIdéal.setText("" + Math.round(d1 - 100 - ((d1-150)/4)));
  220.         }
  221.         else {
  222.             textField_PdsIdéal.setText("" + Math.round(d1 - 100 - ((d1-150)/2.5)));
  223.         }
  224.     }
  225.        
  226.    
  227.     public void  OnEffacer() {
  228.         btnpoidsid.setEnabled(false);
  229.         btnEffacer.setEnabled(false);
  230.         textFieldTaille.setText("");
  231.         textFieldPoids.setText("");
  232.         textRsltIMC.setText("");
  233.         textPane.setText("");
  234.         textField_PdsIdéal.setText("");
  235.     }
  236.     public void  OnIMC() {
  237.        
  238.         btnpoidsid.setEnabled(true);
  239.         btnEffacer.setEnabled(true);
  240.        
  241.         String s1=textFieldTaille.getText();
  242.         String s2=textFieldPoids.getText();
  243.        
  244.          try{
  245.                 double d1=Double.parseDouble(s1);      
  246.                 d1=d1/100;
  247.                 double d2=Double.parseDouble(s2);
  248.                 double Result = d2 /( Math.pow(d1, 2));
  249.                 textRsltIMC.setText("" + Math.round(Result));
  250.                
  251.                
  252.                 if(Result<=18.5) {
  253.                     textPane.setText("Trop maigre");
  254.                     textPane.setBackground(Color.CYAN);
  255.                      dlm.addElement("Poids:"+d2+" taille:"+d1*100+" IMC:"+Math.round(Result)+" Trop maigre");
  256.                    
  257.                 }
  258.                 else if(Result<=25) {
  259.                     textPane.setText("Normal");
  260.                     textPane.setBackground(Color.GREEN);
  261.                      dlm.addElement("Poids:"+d2+" taille:"+d1*100+" IMC:"+Math.round(Result)+" Normal");
  262.                 }
  263.                 if(Result>25) {
  264.                     textPane.setText("Trop gros");
  265.                     textPane.setBackground(Color.RED);
  266.                      dlm.addElement("Poids:"+d2+" taille:"+d1*100+" IMC:"+Math.round(Result)+" Trop gros");
  267.                 }
  268.             }
  269.             catch(NumberFormatException numberformatexception) {
  270.                 textFieldTaille.setText("Erreur");
  271.                 textFieldPoids.setText("Erreur");
  272.                 textRsltIMC.setText("Erreur");
  273.                 textPane.setText("Erreur");
  274.             }
  275.          
  276.        
  277.          
  278.          
  279.     }
  280. }
  281.  
Advertisement
Add Comment
Please, Sign In to add comment