• Lenguaje

    Java usando Swing

  • Descripción

    Suma dos fracciones o números quebrados

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SumaDosFracciones extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JTextField field_dividendo_1, field_dividendo_2, field_divisor_1, field_divisor_2;
    private JLabel label_resultado;
    private JButton button;

    public Algoritmo() {
        field_dividendo_1 = new JTextField(4);
        field_dividendo_2 = new JTextField(4);
        field_divisor_1 = new JTextField(4);
        field_divisor_2 = new JTextField(4);
        label_resultado = new JLabel("", JLabel.RIGHT);
        button = new JButton("Procesar");
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        JPanel panel, subpanel;
        panel = new JPanel(new BorderLayout());
        subpanel = new JPanel(new GridLayout(4, 1));
        subpanel.add(new JLabel("Ingresa el valor de dividendo 1:"));
        subpanel.add(new JLabel("Ingresa el valor de dividendo 2:"));
        subpanel.add(new JLabel("Ingresa el valor de divisor 1:"));
        subpanel.add(new JLabel("Ingresa el valor de divisor 2:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new JPanel(new GridLayout(4, 1));
        subpanel.add(field_dividendo_1);
        subpanel.add(field_dividendo_2);
        subpanel.add(field_divisor_1);
        subpanel.add(field_divisor_2);
        panel.add(subpanel);
        pane.add(panel, BorderLayout.NORTH);
        panel = new JPanel(new FlowLayout());
        panel.add(button);
        pane.add(panel);
        panel = new JPanel(new BorderLayout());
        subpanel = new JPanel(new GridLayout(1, 1));
        subpanel.add(new JLabel("Valor de resultado:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new JPanel(new GridLayout(1, 1));
        subpanel.add(label_resultado);
        panel.add(subpanel);
        pane.add(panel, BorderLayout.SOUTH);
        button.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        double dividendo_1, dividendo_2, divisor_1, divisor_2, resultado;
        try {
            dividendo_1 = Double.parseDouble(field_dividendo_1.getText());
            dividendo_2 = Double.parseDouble(field_dividendo_2.getText());
            divisor_1 = Double.parseDouble(field_divisor_1.getText());
            divisor_2 = Double.parseDouble(field_divisor_2.getText());
        } catch (NumberFormatException numberFormatException) {
            return;
        }
        resultado=(divisor_1*dividendo_2+divisor_2*dividendo_1)/dividendo_2/dividendo_1;
        label_resultado.setText(String.valueOf(resultado));
        pack();
    }

    public static void main(String[] args) {
        new Algoritmo().setVisible(true);
    }

}