Calculadora científica Java
Aldo1411Práctica o problema25 de Noviembre de 2019
4.408 Palabras (18 Páginas)324 Visitas
import javax.swing.*;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.*;
import java.math.MathContext;
public class calculadora extends JFrame implements ActionListener
{
public JPanel panel;
JButton b1 = new JButton();
JButton b2 = new JButton();
JButton b3 = new JButton();
JButton b4 = new JButton();
JButton b5 = new JButton();
JButton b6 = new JButton();
JButton b7 = new JButton();
JButton b8 = new JButton();
JButton b9 = new JButton();
JButton b10 = new JButton();
JButton b11 = new JButton();
JButton b12 = new JButton();
JButton b13 = new JButton();
JButton b14 = new JButton();
JButton b15 = new JButton();
JButton b16 = new JButton();
JButton b17 = new JButton();
JButton b18 = new JButton();
JButton b19 = new JButton();
JButton b20 = new JButton();
JButton b21 = new JButton();
JButton b22 = new JButton();
JButton b23 = new JButton();
JButton b24 = new JButton();
JButton b25 = new JButton();
JButton b26 = new JButton();
JButton b27 = new JButton();
JButton b28 = new JButton();
JButton b29 = new JButton();
JButton b30 = new JButton();
JButton b31 = new JButton();
JButton sqrt = new JButton();
JButton sqrt2 = new JButton();
JButton adelante = new JButton();
JButton atras = new JButton();
JTextField caja1 = new JTextField ();
double num1;
double num2;
double angulos;
double rad;
String op;
public calculadora () {
setSize(685,605); //se estabece el tamaño de la ventana
setDefaultCloseOperation(EXIT_ON_CLOSE); //close on exit
setTitle("Calculadora Científica"); //se establece el título
setLocationRelativeTo(null); //se establece la ventana en el centro
iniciarcomponentes (); //activo mis componentes
}
private void iniciarcomponentes () {
colocarpanel(); //iniciamos el panel
crearbotones(); //iniciamos los botones
colocarcajadetexto();
}
private void colocarpanel() {
panel = new JPanel (); //crear mi panel
getContentPane().add(panel); //agregar el panel
panel.setLayout(null);
}
private void crearbotones() {
b1.setText("Sin"); //establecer texto al botón
b1.setBounds(50, 140, 90, 60); //definir posición del botón
b1.addActionListener(this); //agregamos oyente de acción
// creamos botón 2
b2.setText("Cos");
b2.setBounds(150, 140, 90, 60);
b2.addActionListener(this);
//creamos botón 3
b3.setText("Tan");
b3.setBounds(250, 140, 90, 60);
b3.addActionListener(this);
//creamos botón 4
b4.setText("Sin -1");
b4.setBounds(50, 210, 90, 60);
b4.addActionListener(this);
//creamos botón 5
b5.setText("Cos -1");
b5.setBounds(150, 210, 90, 60);
b5.addActionListener(this);
//creamos botón 6
b6.setText("Tan -1");
b6.setBounds(250, 210, 90, 60);
b6.addActionListener(this);
//creamos botón 7
b7.setText("Pi");
b7.setBounds(50, 280, 90, 130);
b7.addActionListener(this);
//creamos botón 8
b8.setText("Ln");
b8.setBounds(150, 280, 90, 130);
b8.addActionListener(this);
//creamos botón 9
b9.setText("Log");
b9.setBounds(250, 280, 90, 130);
b9.addActionListener(this);
//creamos botón 10
b10.setText("1/x");
b10.setBounds(50, 420, 90, 60);
b10.addActionListener(this);
//creamos botón 11
b11.setText("x^2");
b11.setBounds(150, 420, 90, 60);
b11.addActionListener(this);
//creamos botón 12
b12.setText("x^x");
b12.setBounds(250, 420, 90, 60);
b12.addActionListener(this);
//creamos botón raíz cudrada
sqrt.setText("sqrt");
sqrt.setBounds(50, 490, 140, 60);
sqrt.addActionListener(this);
//creamos botón raíz
sqrt2.setText("sqrt^x");
sqrt2.setBounds(200, 490, 140, 60);
sqrt2.addActionListener(this);
//creamos botón 13
b13.setText("c");
b13.setBounds(350, 140, 60, 60);
b13.addActionListener(this);
//creamos botón 14
b14.setText("ce");
b14.setBounds(420, 140, 60, 60);
b14.addActionListener(this);
//creamos botón 15
b15.setText("/");
b15.setBounds(490, 140, 60, 60);
b15.addActionListener(this);
//creamos botón 16
b16.setText("%");
b16.setBounds(560, 140, 60, 60);
b16.addActionListener(this);
//creamos botón 17
b17.setText("1");
b17.setBounds(350, 210, 60, 60);
b17.addActionListener(this);
//creamos botón 18
b18.setText("2");
b18.setBounds(420, 210, 60, 60);
b18.addActionListener(this);
//creamos botón 19
b19.setText("3");
b19.setBounds(490, 210, 60, 60);
b19.addActionListener(this);
//creamos botón 20
b20.setText("4");
b20.setBounds(350, 280, 60, 60);
b20.addActionListener(this);
//creamos botón 21
b21.setText("5");
b21.setBounds(420, 280, 60, 60);
b21.addActionListener(this);
//creamos botón 22
b22.setText("6");
b22.setBounds(490, 280, 60, 60);
b22.addActionListener(this);
//creamos botón 23
b23.setText("7");
b23.setBounds(350, 350, 60, 60);
b23.addActionListener(this);
//creamos botón 24
b24.setText("8");
b24.setBounds(420, 350, 60, 60);
b24.addActionListener(this);
//creamos botón 25
b25.setText("9");
b25.setBounds(490, 350, 60, 60);
b25.addActionListener(this);
//creamos botón 26
b26.setText("0");
b26.setBounds(350, 420, 130, 60);
b26.addActionListener(this);
//creamos botón 27
b27.setText("=");
b27.setBounds(490, 420, 60, 60);
b27.addActionListener(this);
//creamos botón adelante //////////////
adelante.setText("adelante");
adelante.setBounds(350, 490, 130, 60);
adelante.addActionListener(this);
//creamos botón atrás //////////////
atras.setText("atrás");
atras.setBounds(490, 490, 130, 60);
atras.addActionListener(this);
//creamos botón 28
b28.setText("+");
b28.setBounds(560, 210, 60, 60);
b28.addActionListener(this);
//creamos botón 29
b29.setText("-");
b29.setBounds(560, 280, 60, 60);
b29.addActionListener(this);
//creamos botón 30
b30.setText("*");
b30.setBounds(560, 350, 60, 60);
b30.addActionListener(this);
//creamos botón 31
b31.setText(".");
b31.setBounds(560, 420, 60, 60);
b31.addActionListener(this);
//b1.setMnemonic('h'); ==
// alt + h = presionar el botón de hola
b1.setForeground(Color.BLUE); //establecemos el color de la letra del botón
b1.setFont(new Font("arial", Font.BOLD, 12)); //establecemos fuente de la letra del botón
//en el estilo (segundo parámetro) 0 es estilo plano, 1 negrita, 2 cursiva y 3 es negrita y cursiva
panel.add(b1); //agregamos botón al panel
panel.add(b2); //agregamos botón 2
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b10);
panel.add(b11);
panel.add(b12);
panel.add(b13);
panel.add(b14);
panel.add(b15);
panel.add(b16);
panel.add(b17);
panel.add(b18);
panel.add(b19);
panel.add(b20);
panel.add(b21);
panel.add(b22);
panel.add(b23);
panel.add(b24);
panel.add(b25);
panel.add(b26);
panel.add(b27);
panel.add(b28);
panel.add(b29);
panel.add(b30);
panel.add(b31);
panel.add(sqrt);
panel.add(sqrt2);
panel.add(adelante);
panel.add(atras);
}
private
...