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 estacionmeteorologica; import es.javiergarciescobedo.swing.pluviometro.Pluviometro; import java.text.DecimalFormat; import javax.swing.SpinnerNumberModel; import org.apache.commons.lang3.text.WordUtils; /** * * @author Manuel Manzano */ public class main extends javax.swing.JFrame { /** * Creates new form main */ public main() { initComponents(); SpinnerNumberModel modelodia = new SpinnerNumberModel(1, 1, 30, 1); jSpinnerdia.setModel(modelodia); getRootPane().setDefaultButton(jButtonincluir); } /** * 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() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jSpinnerdia = new javax.swing.JSpinner(); jLabel2 = new javax.swing.JLabel(); jTextFieldlitros = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jTextFieldciudad = new javax.swing.JTextField(); jButtonincluir = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextAreatexto = new javax.swing.JTextArea(); jPanel2 = new javax.swing.JPanel(); jButtoniniciar = new javax.swing.JButton(); jButtonparar = new javax.swing.JButton(); jButtonmas = new javax.swing.JButton(); jButtonmenos = new javax.swing.JButton(); jButtontormenta = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jTextFieldintensidad = new javax.swing.JTextField(); lluvia = new es.javiergarciescobedo.swing.pluviometro.Pluviometro(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel1.setText("Da:"); jLabel2.setText("Litros:"); jLabel3.setText("Ciudad:"); jButtonincluir.setText("Aadir"); jButtonincluir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonincluirActionPerformed(evt); } }); jTextAreatexto.setColumns(20); jTextAreatexto.setRows(5); jScrollPane1.setViewportView(jTextAreatexto); 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).addComponent(jScrollPane1) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSpinnerdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldlitros, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextFieldciudad))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonincluir).addGap(0, 1, Short.MAX_VALUE))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(jSpinnerdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2).addComponent(jTextFieldlitros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonincluir).addComponent(jLabel3)) .addGap(32, 32, 32).addComponent(jScrollPane1).addContainerGap())); jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)); jButtoniniciar.setText("Iniciar"); jButtoniniciar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtoniniciarActionPerformed(evt); } }); jButtonparar.setText("Parar"); jButtonparar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonpararActionPerformed(evt); } }); jButtonmas.setText("Ms"); jButtonmas.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonmasActionPerformed(evt); } }); jButtonmenos.setText("Menos"); jButtonmenos.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonmenosActionPerformed(evt); } }); jButtontormenta.setText("Tormenta Inmediata"); jButtontormenta.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtontormentaActionPerformed(evt); } }); jLabel4.setText("Intensidad:"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(lluvia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButtonmenos, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE) .addComponent(jButtonmas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonparar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtoniniciar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(jButtontormenta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel4).addGap(0, 0, Short.MAX_VALUE)) .addComponent(jTextFieldintensidad)).addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(21, 21, 21) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jButtoniniciar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonparar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonmas) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonmenos)) .addComponent(lluvia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(29, 29, 29).addComponent(jButtontormenta).addGap(18, 18, 18).addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldintensidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(28, 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().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonincluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonincluirActionPerformed DecimalFormat formatolitro = new DecimalFormat("# litros"); String litros = formatolitro.format(Integer.valueOf(jTextFieldlitros.getText())); String ciudad = jTextFieldciudad.getText(); String formatotexto = WordUtils.capitalize(ciudad); jTextAreatexto.append("En " + formatotexto + " - Dia: " + jSpinnerdia.getValue() + " = " + litros); }//GEN-LAST:event_jButtonincluirActionPerformed private void jButtoniniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtoniniciarActionPerformed lluvia.setLloviendo(true); jTextFieldintensidad.setText(lluvia.getIntensidad()); }//GEN-LAST:event_jButtoniniciarActionPerformed private void jButtonpararActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonpararActionPerformed lluvia.setLloviendo(false); lluvia.setIntensidad(lluvia.SECO); jTextFieldintensidad.setText(lluvia.getIntensidad()); }//GEN-LAST:event_jButtonpararActionPerformed private void jButtonmasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonmasActionPerformed lluvia.subirIntensidad(); jTextFieldintensidad.setText(lluvia.getIntensidad()); }//GEN-LAST:event_jButtonmasActionPerformed private void jButtonmenosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonmenosActionPerformed lluvia.bajarIntensidad(); jTextFieldintensidad.setText(lluvia.getIntensidad()); }//GEN-LAST:event_jButtonmenosActionPerformed private void jButtontormentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtontormentaActionPerformed lluvia.setIntensidad(lluvia.TORMENTA); jTextFieldintensidad.setText(lluvia.getIntensidad()); }//GEN-LAST:event_jButtontormentaActionPerformed /** * @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(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(main.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 main().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonincluir; private javax.swing.JButton jButtoniniciar; private javax.swing.JButton jButtonmas; private javax.swing.JButton jButtonmenos; private javax.swing.JButton jButtonparar; private javax.swing.JButton jButtontormenta; 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.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSpinner jSpinnerdia; private javax.swing.JTextArea jTextAreatexto; private javax.swing.JTextField jTextFieldciudad; private javax.swing.JTextField jTextFieldintensidad; private javax.swing.JTextField jTextFieldlitros; private es.javiergarciescobedo.swing.pluviometro.Pluviometro lluvia; // End of variables declaration//GEN-END:variables }