Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package robin.maths;
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class MesurePrincipale
- {
- private static JTextField field1 = new JTextField();
- private static JTextField field2 = new JTextField();
- private static JFrame frame = new JFrame();
- private static JPanel labelPanel1 = new JPanel();
- private static JPanel labelPanel2 = new JPanel();
- private static JPanel labelPanel3 = new JPanel();
- private static JPanel labelPanel4 = new JPanel();
- private static JLabel label1 = new JLabel();
- private static JLabel label2 = new JLabel();
- private static JLabel label3 = new JLabel();
- public static void main(String[] args)
- {
- frame.setTitle("Caculateur de mesure principale");
- frame.setSize(450, 180);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- JPanel text = new JPanel();
- field1.setPreferredSize(new Dimension(150, 30));
- field1.setToolTipText("A");
- field2.setPreferredSize(new Dimension(150, 30));
- field2.setToolTipText("B");
- text.add(field1);
- text.add(field2);
- JPanel buttonPane = new JPanel();
- JButton button = new JButton("Calculer");
- button.setSize(30, 10);
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- tryProcess();
- }
- });
- buttonPane.add(button);
- frame.add(text, BorderLayout.NORTH);
- labelPanel1.setLayout(new BoxLayout(labelPanel1, BoxLayout.LINE_AXIS));
- labelPanel1.add(label1);
- labelPanel2.setLayout(new BoxLayout(labelPanel2, BoxLayout.LINE_AXIS));
- labelPanel2.add(label2);
- labelPanel3.setLayout(new BoxLayout(labelPanel3, BoxLayout.LINE_AXIS));
- labelPanel3.add(label3);
- labelPanel4.setLayout(new BoxLayout(labelPanel4, BoxLayout.PAGE_AXIS));
- labelPanel4.add(buttonPane);
- labelPanel4.add(labelPanel1);
- labelPanel4.add(labelPanel2);
- labelPanel4.add(labelPanel3);
- frame.add(labelPanel4);
- frame.setVisible(true);
- }
- private static void tryProcess()
- {
- String strA = field1.getText();
- String strB = field2.getText();
- long a = 0, b = 0;
- try
- {
- a = Long.parseLong(strA);
- }
- catch(NumberFormatException ex)
- {
- JOptionPane.showMessageDialog(null, "A est invalide", "Erreur", JOptionPane.ERROR_MESSAGE);
- return;
- }
- try
- {
- b = Long.parseLong(strB);
- }
- catch(NumberFormatException ex)
- {
- JOptionPane.showMessageDialog(null, "B est invalide", "Erreur", JOptionPane.ERROR_MESSAGE);
- return;
- }
- process(a, b);
- }
- private static void process(long a, long b)
- {
- if(b == 0)
- {
- JOptionPane.showMessageDialog(null, "B est nul", "Erreur", JOptionPane.ERROR_MESSAGE);
- return;
- }
- long b2 = (2 * b);
- long c = (a % b2);
- if(c > b)
- {
- c -= b2;
- }
- if(c < 0)
- {
- if(c < -b)
- c += b;
- }
- String output = c + "Ο";
- if(c == 0)
- {
- label1.setText(String.valueOf(0));
- label2.setText(" ");
- label3.setText("");
- }
- else
- {
- label1.setText(output);
- label2.setText("ββ");
- label3.setText(String.valueOf(b));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement