Advertisement
Dar954826

GCD and lcm[ENG].java

Mar 9th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1.  
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.JTextField;
  6. import java.awt.Font;
  7. import javax.swing.JButton;
  8. import javax.swing.JLabel;
  9. import javax.swing.border.EmptyBorder;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.Color;
  13.  
  14. public class Lcm extends JFrame {
  15.     private static final long serialVersionUID = 1L;
  16.     private JPanel contentPane;
  17.     private JTextField textField;
  18.     private JTextField textField_1;
  19.     private JLabel lblInserisciSecondoNumero;
  20.     private JLabel lblNewLabel_1;
  21.     private JLabel lblNewLabel_2;
  22.     private JLabel lblNewLabel_3;
  23.     private JLabel lblNewLabel_4;
  24.  
  25.     public static void main(String[] args) {
  26.         EventQueue.invokeLater(new Runnable() {
  27.             public void run() {
  28.                 try {
  29.                     Lcm frame = new Lcm();
  30.                     frame.setVisible(true);
  31.                 } catch (Exception e) {
  32.                     e.printStackTrace();
  33.                 }
  34.             }
  35.         });
  36.     }
  37.  
  38.     public Lcm() {
  39.         setTitle("GCD and lcm");
  40.         setForeground(new Color(0, 255, 255));
  41.         setBackground(new Color(0, 0, 0));
  42.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         setBounds(100, 100, 316, 300);
  44.         contentPane = new JPanel();
  45.         contentPane.setForeground(new Color(0, 255, 255));
  46.         contentPane.setBackground(new Color(0, 0, 0));
  47.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  48.         setContentPane(contentPane);
  49.         contentPane.setLayout(null);
  50.  
  51.         lblInserisciSecondoNumero = new JLabel("Insert second number");
  52.         lblInserisciSecondoNumero.setForeground(new Color(0, 255, 255));
  53.         lblInserisciSecondoNumero.setBackground(new Color(0, 0, 0));
  54.         lblInserisciSecondoNumero.setFont(new Font("Tahoma", Font.PLAIN, 17));
  55.         lblInserisciSecondoNumero.setEnabled(false);
  56.         lblInserisciSecondoNumero.setBounds(50, 84, 192, 31);
  57.         contentPane.add(lblInserisciSecondoNumero);
  58.  
  59.         JLabel lblNewLabel = new JLabel("Insert first number");
  60.         lblNewLabel.setForeground(new Color(0, 255, 255));
  61.         lblNewLabel.setBackground(new Color(0, 0, 0));
  62.         lblNewLabel.setEnabled(false);
  63.         lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
  64.         lblNewLabel.setBounds(55, 26, 179, 31);
  65.         contentPane.add(lblNewLabel);
  66.  
  67.         textField = new JTextField();
  68.         textField.setForeground(new Color(0, 255, 255));
  69.         textField.setBackground(new Color(0, 0, 0));
  70.         textField.setFont(new Font("Tahoma", Font.PLAIN, 20));
  71.         textField.setBounds(42, 25, 200, 31);
  72.         contentPane.add(textField);
  73.         textField.setColumns(10);
  74.  
  75.         textField_1 = new JTextField();
  76.         textField_1.setForeground(new Color(0, 255, 255));
  77.         textField_1.setBackground(new Color(0, 0, 0));
  78.         textField_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
  79.         textField_1.setColumns(10);
  80.         textField_1.setBounds(42, 83, 200, 31);
  81.         contentPane.add(textField_1);
  82.  
  83.         JButton btnNewButton = new JButton("Calculate GCD and lcm");
  84.         btnNewButton.setForeground(new Color(0, 255, 255));
  85.         btnNewButton.setBackground(new Color(0, 0, 0));
  86.         btnNewButton.addMouseListener(new MouseAdapter() {
  87.             @Override
  88.             public void mouseClicked(MouseEvent arg0) {
  89.                 int num = 2;
  90.                 int a = Integer.parseInt(textField.getText());
  91.                 int b = Integer.parseInt(textField_1.getText());
  92.  
  93.                 while (a % num != 0 | b % num != 0) {
  94.                     num++;
  95.                 }
  96.                 lblNewLabel_1.setText(Integer.toString(num));
  97.                 num = 2;
  98.                 while (num % a != 0 | num % b != 0) {
  99.                     num++;
  100.                 }
  101.                 lblNewLabel_2.setText(Integer.toString(num));
  102.             }
  103.         });
  104.         btnNewButton.setBounds(60, 126, 163, 43);
  105.         contentPane.add(btnNewButton);
  106.  
  107.         lblNewLabel_1 = new JLabel("");
  108.         lblNewLabel_1.setForeground(new Color(0, 255, 255));
  109.         lblNewLabel_1.setBackground(new Color(0, 0, 0));
  110.         lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 20));
  111.         lblNewLabel_1.setBounds(97, 164, 101, 59);
  112.         contentPane.add(lblNewLabel_1);
  113.  
  114.         lblNewLabel_2 = new JLabel("");
  115.         lblNewLabel_2.setForeground(new Color(0, 255, 255));
  116.         lblNewLabel_2.setBackground(new Color(0, 0, 0));
  117.         lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 20));
  118.         lblNewLabel_2.setBounds(97, 232, 101, 19);
  119.         contentPane.add(lblNewLabel_2);
  120.  
  121.         lblNewLabel_3 = new JLabel("GCD");
  122.         lblNewLabel_3.setForeground(new Color(0, 255, 255));
  123.         lblNewLabel_3.setBackground(new Color(0, 0, 0));
  124.         lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 20));
  125.         lblNewLabel_3.setBounds(10, 157, 53, 72);
  126.         contentPane.add(lblNewLabel_3);
  127.  
  128.         lblNewLabel_4 = new JLabel("lcm");
  129.         lblNewLabel_4.setForeground(new Color(0, 255, 255));
  130.         lblNewLabel_4.setBackground(new Color(0, 0, 0));
  131.         lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 20));
  132.         lblNewLabel_4.setBounds(10, 229, 53, 24);
  133.         contentPane.add(lblNewLabel_4);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement