Este es su código:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculadora extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField campoN1, campoN2, campoN3, campoN4, campoN5, campoN6, campoN7, campoN8, campoa, campob, campoc, campod;
private JLabel etiquetaIgual;
private JButton boton1, boton2, boton3, boton4;
public static void main(String[] args){
Calculadora marco = new Calculadora();
marco.setSize(400, 185);
marco.crearGUI();
marco.setVisible(true);
}
private void crearGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container ventana = getContentPane();
ventana.setLayout(new FlowLayout());
campoN1 = new JTextField(7);
ventana.add(campoN1);
boton1 = new JButton("+");
ventana.add(boton1);
boton1.addActionListener(this);
boton1.setForeground(Color.BLUE);
campoN2 = new JTextField(7);
ventana.add(campoN2);
etiquetaIgual = new JLabel(" = ");
ventana.add(etiquetaIgual);
campoa = new JTextField(10);
ventana.add(campoa);
campoN3 = new JTextField(7);
ventana.add(campoN3);
boton2 = new JButton(" - ");
ventana.add(boton2);
boton2.addActionListener(this);
boton2.setForeground(Color.BLUE);
campoN4 = new JTextField(7);
ventana.add(campoN4);
etiquetaIgual = new JLabel(" = ");
ventana.add(etiquetaIgual);
campob = new JTextField(10);
ventana.add(campob);
campoN5 = new JTextField(7);
ventana.add(campoN5);
boton3 = new JButton(" x ");
ventana.add(boton3);
boton3.addActionListener(this);
boton3.setForeground(Color.BLUE);
campoN6 = new JTextField(7);
ventana.add(campoN6);
etiquetaIgual = new JLabel(" = ");
ventana.add(etiquetaIgual);
campoc = new JTextField(10);
ventana.add(campoc);
campoN7 = new JTextField(7);
ventana.add(campoN7);
boton4 = new JButton(" / ");
ventana.add(boton4);
boton4.addActionListener(this);
boton4.setForeground(Color.BLUE);
campoN8 = new JTextField(7);
ventana.add(campoN8);
etiquetaIgual = new JLabel(" = ");
ventana.add(etiquetaIgual);
campod = new JTextField(10);
ventana.add(campod);
}
public void actionPerformed(ActionEvent event){
double N1 = Double.parseDouble(campoN1.getText());
double N2 = Double.parseDouble(campoN2.getText());
campoa.setText(Double.toString(N1 + N2));
double N3 = Double.parseDouble(campoN3.getText());
double N4 = Double.parseDouble(campoN4.getText());
campob.setText(Double.toString(N3 - N4));
double N5 = Double.parseDouble(campoN5.getText());
double N6 = Double.parseDouble(campoN6.getText());
campoc.setText(Double.toString(N5 * N6));
double N7 = Double.parseDouble(campoN7.getText());
double N8 = Double.parseDouble(campoN8.getText());
campod.setText(Double.toString(N7 / N8));
}
}Y sus pantallas
No hay comentarios:
Publicar un comentario