• Lenguaje

    Java usando Applet

  • Descripción

    Lee un número por teclado y dice si es positivo o negativo.

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
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class PositivoONegativo extends Applet implements ActionListener {

    private static final long serialVersionUID = 1L;
    private TextField field_un_numero;
    private Button button;

    @Override
    public void init() {
        field_un_numero = new TextField(4);
        button = new Button("Procesar");
        setLayout(new BorderLayout());
        Panel panel, subpanel;
        panel = new Panel(new BorderLayout());
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(new Label("Ingresa el valor de un numero:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new Panel(new GridLayout(1, 1));
        subpanel.add(field_un_numero);
        panel.add(subpanel);
        add(panel, BorderLayout.NORTH);
        panel = new Panel(new FlowLayout());
        panel.add(button);
        add(panel);
        button.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        double un_numero;
        try {
            un_numero = Double.parseDouble(field_un_numero.getText());
        } catch (NumberFormatException numberFormatException) {
            return;
        }
        if(un_numero<0)
            javax.swing.JOptionPane.showMessageDialog(this, "Negativo");
        else
            javax.swing.JOptionPane.showMessageDialog(this, "Positivo");
    }

}