Advertisement
robin4002

Untitled

Nov 23rd, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package robin.maths;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.BoxLayout;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextField;
  15.  
  16. public class MesurePrincipale
  17. {
  18.     private static JTextField field1 = new JTextField();
  19.     private static JTextField field2 = new JTextField();
  20.     private static JFrame frame = new JFrame();
  21.     private static JPanel labelPanel1 = new JPanel();
  22.     private static JPanel labelPanel2 = new JPanel();
  23.     private static JPanel labelPanel3 = new JPanel();
  24.     private static JPanel labelPanel4 = new JPanel();
  25.     private static JLabel label1 = new JLabel();
  26.     private static JLabel label2 = new JLabel();
  27.     private static JLabel label3 = new JLabel();
  28.  
  29.     public static void main(String[] args)
  30.     {
  31.         frame.setTitle("Caculateur de mesure principale");
  32.         frame.setSize(450, 180);
  33.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         frame.setLocationRelativeTo(null);
  35.         frame.setResizable(false);
  36.  
  37.         JPanel text = new JPanel();
  38.  
  39.         field1.setPreferredSize(new Dimension(150, 30));
  40.         field1.setToolTipText("A");
  41.         field2.setPreferredSize(new Dimension(150, 30));
  42.         field2.setToolTipText("B");
  43.  
  44.         text.add(field1);
  45.         text.add(field2);
  46.  
  47.         JPanel buttonPane = new JPanel();
  48.         JButton button = new JButton("Calculer");
  49.         button.setSize(30, 10);
  50.         button.addActionListener(new ActionListener()
  51.         {
  52.             public void actionPerformed(ActionEvent event)
  53.             {
  54.                 tryProcess();
  55.             }
  56.         });
  57.         buttonPane.add(button);
  58.  
  59.         frame.add(text, BorderLayout.NORTH);
  60.  
  61.         labelPanel1.setLayout(new BoxLayout(labelPanel1, BoxLayout.LINE_AXIS));
  62.         labelPanel1.add(label1);
  63.         labelPanel2.setLayout(new BoxLayout(labelPanel2, BoxLayout.LINE_AXIS));
  64.         labelPanel2.add(label2);
  65.         labelPanel3.setLayout(new BoxLayout(labelPanel3, BoxLayout.LINE_AXIS));
  66.         labelPanel3.add(label3);
  67.  
  68.         labelPanel4.setLayout(new BoxLayout(labelPanel4, BoxLayout.PAGE_AXIS));
  69.         labelPanel4.add(buttonPane);
  70.         labelPanel4.add(labelPanel1);
  71.         labelPanel4.add(labelPanel2);
  72.         labelPanel4.add(labelPanel3);
  73.         frame.add(labelPanel4);
  74.         frame.setVisible(true);
  75.     }
  76.  
  77.     private static void tryProcess()
  78.     {
  79.         String strA = field1.getText();
  80.         String strB = field2.getText();
  81.  
  82.         long a = 0, b = 0;
  83.         try
  84.         {
  85.             a = Long.parseLong(strA);
  86.         }
  87.         catch(NumberFormatException ex)
  88.         {
  89.             JOptionPane.showMessageDialog(null, "A est invalide", "Erreur", JOptionPane.ERROR_MESSAGE);
  90.             return;
  91.         }
  92.         try
  93.         {
  94.             b = Long.parseLong(strB);
  95.         }
  96.         catch(NumberFormatException ex)
  97.         {
  98.             JOptionPane.showMessageDialog(null, "B est invalide", "Erreur", JOptionPane.ERROR_MESSAGE);
  99.             return;
  100.         }
  101.         process(a, b);
  102.     }
  103.  
  104.     private static void process(long a, long b)
  105.     {
  106.         if(b == 0)
  107.         {
  108.             JOptionPane.showMessageDialog(null, "B est nul", "Erreur", JOptionPane.ERROR_MESSAGE);
  109.             return;
  110.         }
  111.         long b2 = (2 * b);
  112.         long c = (a % b2);
  113.         if(c > b)
  114.         {
  115.             c -= b2;
  116.         }
  117.         String output = c + "Ο€";
  118.         label1.setText(output);
  119.         label2.setText("β€”β€”");
  120.         label3.setText(String.valueOf(b));
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement