Programa Calculadora Java con JFrame
Enviado por Luis Melgar • 7 de Mayo de 2019 • Tarea • 3.192 Palabras (13 Páginas) • 316 Visitas
package calc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class py extends JFrame implements ActionListener {
// base
JPanel base = (JPanel) this.getContentPane();
// pantalla
JTextField pantalla = new JTextField();
// botones
JButton jb0 = new JButton("0");
JButton jb1 = new JButton("1");
JButton jb2 = new JButton("2");
JButton jb3 = new JButton("3");
JButton jb4 = new JButton("4");
JButton jb5 = new JButton("5");
JButton jb6 = new JButton("6");
JButton jb7 = new JButton("7");
JButton jb8 = new JButton("8");
JButton jb9 = new JButton("9");
JButton bPunto = new JButton(".");
JButton bSuma = new JButton("+");
JButton bResta = new JButton("-");
JButton bMult = new JButton("*");
JButton bDiv = new JButton("/");
JButton bAC = new JButton("AC");
JButton bRes = new JButton("=");
public py() {
// propiedades de mi base
base.setLayout(null);
setSize(300, 450);
setTitle("CALCUTRON v2.0");
setVisible(true);
// propiedades de mi pantalla
pantalla.setBounds(10, 10, 280, 50);
pantalla.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(pantalla);
// propiedades de botones
jb7.setBounds(15, 70, 60, 60);
jb7.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb7);
jb7.addActionListener(this);
jb8.setBounds(85, 70, 60, 60);
jb8.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb8);
jb8.addActionListener(this);
jb9.setBounds(155, 70, 60, 60);
jb9.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb9);
jb9.addActionListener(this);
bMult.setBounds(225, 70, 60, 60);
bMult.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bMult);
bMult.addActionListener(this);
jb4.setBounds(15, 140, 60, 60);
jb4.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb4);
jb4.addActionListener(this);
jb5.setBounds(85, 140, 60, 60);
jb5.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb5);
jb5.addActionListener(this);
jb6.setBounds(155, 140, 60, 60);
jb6.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb6);
jb6.addActionListener(this);
bResta.setBounds(225, 140, 60, 60);
bResta.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bResta);
bResta.addActionListener(this);
jb1.setBounds(15, 210, 60, 60);
jb1.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb1);
jb1.addActionListener(this);
jb2.setBounds(85, 210, 60, 60);
jb2.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb2);
jb2.addActionListener(this);
jb3.setBounds(155, 210, 60, 60);
jb3.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb3);
jb3.addActionListener(this);
bSuma.setBounds(225, 210, 60, 60);
bSuma.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bSuma);
bSuma.addActionListener(this);
bDiv.setBounds(15, 280, 60, 60);
bDiv.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bDiv);
bDiv.addActionListener(this);
jb0.setBounds(85, 280, 60, 60);
jb0.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(jb0);
bPunto.setBounds(155, 280, 60, 60);
bPunto.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bPunto);
bPunto.addActionListener(this);
bRes.setBounds(225, 280, 60, 60);
bRes.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bRes);
bRes.addActionListener(this);
bAC.setBounds(120, 350, 60, 60);
bAC.setFont(new Font("Ubuntu", Font.BOLD, 15));
add(bAC);
bAC.addActionListener(this);
}
public static void main(String[] args) {
new py();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bPunto) {
if (pantalla.getText() == "") {
pantalla.setText(".");
}
else {
pantalla.setText(pantalla.getText() + ".");
...