Java tutorial
/* * 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 fracciones; import static oracle.jrockit.jfr.events.Bits.intValue; import org.apache.commons.math3.fraction.Fraction; /** * * @author Usuario */ public class Fracciones extends javax.swing.JFrame { /** * Creates new form Fracciones */ public Fracciones() { 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">//GEN-BEGIN:initComponents private void initComponents() { campoFraccion1_1 = new javax.swing.JTextField(); campoFraccion1_2 = new javax.swing.JTextField(); etiq1 = new javax.swing.JLabel(); campoFraccion2_1 = new javax.swing.JTextField(); campoFraccion2_2 = new javax.swing.JTextField(); etiq2 = new javax.swing.JLabel(); campoResultado = new javax.swing.JTextField(); botonCalcular = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); campoFraccion1_1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { campoFraccion1_1ActionPerformed(evt); } }); etiq1.setText("+"); etiq2.setText("="); campoResultado.setEditable(false); botonCalcular.setText("Calcular"); botonCalcular.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { botonCalcularActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(campoFraccion1_2, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE) .addComponent(campoFraccion1_1)).addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(campoFraccion2_2, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE) .addComponent(campoFraccion2_1))) .addGroup(layout.createSequentialGroup().addGap(42, 42, 42).addComponent(etiq1) .addGap(38, 38, 38).addComponent(etiq2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(campoResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(botonCalcular))) .addContainerGap(14, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(campoFraccion1_1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(campoFraccion2_1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(etiq1).addComponent(etiq2) .addComponent(campoResultado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(botonCalcular)) .addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(campoFraccion1_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(campoFraccion2_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(49, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void campoFraccion1_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoFraccion1_1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_campoFraccion1_1ActionPerformed private void botonCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCalcularActionPerformed int numerador1 = Integer.valueOf(campoFraccion1_1.getText()); int denominador1 = Integer.valueOf(campoFraccion1_2.getText()); Fraction fraccion1 = new Fraction(numerador1, denominador1); int numerador2 = Integer.valueOf(campoFraccion2_1.getText()); int denominador2 = Integer.valueOf(campoFraccion2_2.getText()); Fraction fraccion2 = new Fraction(numerador2, denominador2); Fraction resultado = fraccion1.add(fraccion2); campoResultado.setText(resultado.toString()); // TODO add your handling code here: }//GEN-LAST:event_botonCalcularActionPerformed /** * @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(Fracciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Fracciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Fracciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Fracciones.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 Fracciones().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton botonCalcular; private javax.swing.JTextField campoFraccion1_1; private javax.swing.JTextField campoFraccion1_2; private javax.swing.JTextField campoFraccion2_1; private javax.swing.JTextField campoFraccion2_2; private javax.swing.JTextField campoResultado; private javax.swing.JLabel etiq1; private javax.swing.JLabel etiq2; // End of variables declaration//GEN-END:variables }