Calculadora Codigo

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 18

Calculadora codigo

package clasesfuentes;

import java.awt.Choice;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

/**

* @author Personal

*/

public class CalculadoraGui extends JFrame implements ActionListener{

JLabel l1= new JLabel("CALCULADORA");

JLabel l2= new JLabel("Dato 1");

JLabel l3= new JLabel("Dato 2");

JLabel l4= new JLabel("Operacion");

JLabel l5= new JLabel("Resultado");

JTextField t1= new JTextField(10);

JTextField t2= new JTextField(10);

JTextField t3= new JTextField(10);

Choice ch1= new Choice();

JButton b1= new JButton("Calcular");

int numero1,numero2;

int resultado=0;

int operacion=0;

public CalculadoraGui(){
super("MI Calculadora");

setLayout(null);

ch1.addItem("Suma");

ch1.addItem("Resta");

ch1.addItem("Multipicacion");

ch1.addItem("Division");

this.l1.setBounds(100,15,90,23);

this.add(l1);

this.l2.setBounds(10,55,50,23);

this.add(l2);

this.t1.setBounds(100, 55, 90, 23);

this.add(t1);

this.l3.setBounds(10,95,50,23);

this.add(l3);

this.t2.setBounds(100,95,90,23);

this.add(t2);

this.l4.setBounds(10,135,90 ,23);

this.add(l4);

this.ch1.setBounds(100,135,90,23);

this.add(ch1);

this.l5.setBounds(10,175,90,23);

this.add(l5);
this.t3.setBounds(100,175,90,23);

this.add(t3);

this.b1.setBounds(10,215,170,23);

this.b1.addActionListener(this);

this.add(b1);

this.t3.setEditable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(240,290);

this.setResizable(false);

this.setVisible(true);

public static void main(String armg[]){

CalculadoraGui calculadoragui =new CalculadoraGui();

public void actionPerformed(ActionEvent arg0){

if(arg0.getSource()==b1){

operacion = ch1.getSelectedIndex();

numero1 = Integer.parseInt(t1.getText());

numero2 = Integer.parseInt(t2.getText());

CalculosGui cal=new CalculosGui(numero1,numero2,operacion);

resultado = cal.calcular();

t3.setText(""+resultado);

t1.requestFocus();

resultado=0;

}
}

}
Cálculos

package clasesfuentes;

/**

* @author joseomar

*/

public class CalculosGui {

int opera;

int num1;

int num2;

public CalculosGui(int n1,int n2, int op){

this.num1 = n1;

this.num2 = n2;

this.opera = op;

int calcular(){

int res = 0;

if(opera == 0){

res = num1 + num2;

if(opera == 1){

res = num1 - num2;

if(opera == 2){

res = num1 * num2;

if(opera == 3){

if(num2>0){

res = num1 / num2;


}

return(res);

}
Calcular

package guis;

/**

* @author UT

*/

public class Calcular extends javax.swing.JFrame {

/**

* Creates new form Calcular

*/

public Calcular() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

btncalcular = new javax.swing.JButton();

jLabel4 = new javax.swing.JLabel();


txtnumero1 = new javax.swing.JTextField();

txtnumero2 = new javax.swing.JTextField();

choperacion = new javax.swing.JComboBox<>();

txtresultado = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

setTitle("Callcular");

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calculadora",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 16))); //
NOI18N

jLabel1.setText("Número 1:");

jLabel2.setText("Número 2:");

jLabel3.setText("Operación:");

btncalcular.setText("Calcular");

btncalcular.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btncalcularActionPerformed(evt);

});

jLabel4.setText("Resultado:");

choperacion.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Suma",


"Resta", "Multiplicación", "División" }));

txtresultado.setEditable(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jLabel4)

.addGap(65, 65, 65)

.addComponent(txtresultado))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel1)

.addComponent(jLabel2)

.addComponent(jLabel3))

.addGap(62, 62, 62)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)

.addComponent(txtnumero1)

.addComponent(txtnumero2)

.addComponent(choperacion, 0, 173, Short.MAX_VALUE)))

.addComponent(btncalcular, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addContainerGap(58, Short.MAX_VALUE))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(txtnumero1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(txtnumero2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(choperacion, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addComponent(btncalcular)

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(txtresultado, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(46, Short.MAX_VALUE))

);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

.addGap(57, 57, 57)

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(63, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(42, 42, 42)

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(64, Short.MAX_VALUE))

);

pack();

setLocationRelativeTo(null);

}// </editor-fold>

private void btncalcularActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int n1 = Integer.parseInt(txtnumero1.getText());

int n2 = Integer.parseInt(txtnumero2.getText());

int operacion = choperacion.getSelectedIndex();

int resultado = 0;

if(operacion == 0){

resultado = n1 + n2;

if(operacion == 1){

resultado = n1 - n2;

if(operacion == 2){
resultado = n1 * n2;

if(operacion == 3){

if(n2 > 0){

resultado = n1 / n2;

txtresultado.setText(""+resultado);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see


http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {


java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Calcular.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Calcular().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton btncalcular;

private javax.swing.JComboBox<String> choperacion;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JPanel jPanel1;


private javax.swing.JTextField txtnumero1;

private javax.swing.JTextField txtnumero2;

private javax.swing.JTextField txtresultado;

// End of variables declaration

}
Valaidar

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package guis;

/**

* @author UT

*/

public class Validar extends javax.swing.JFrame {

/**

* Creates new form Validar

*/

public Validar() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {


setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

setTitle("Validar");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 400, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 300, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see


http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Validar.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Validar().setVisible(true);

});

// Variables declaration - do not modify

// End of variables declaration

También podría gustarte