Ver Mensaje Individual
  #3 (permalink)  
Antiguo 06/03/2015, 07:17
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: Netbeans 8 y encontrar fallos.

Hola:

Código algo corregido.
Código Java:
Ver original
  1. import java.io.OutputStream;
  2. import gnu.io.CommPortIdentifier;
  3. import gnu.io.SerialPort;
  4. import java.util.Enumeration;
  5. import javax.swing.JOptionPane;
  6.  
  7. /*
  8.  * To change this license header, choose License Headers in Project Properties.
  9.  * To change this template file, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12.  
  13. /**
  14.  *
  15.  * @author Meta
  16.  */
  17. public class JAVADUINO_JFrame extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form JAVADUINO_JFrame
  21.      */
  22.    
  23.     private static final String L8ON = "Led_8_ON";
  24.     private static final String L8OFF = "Led_8_OFF";
  25.    
  26.     // Variables de conexión.
  27.     private final OutputStream output = null;
  28.     SerialPort serialPort;
  29.     private final String PUERTO = "COM4";
  30.    
  31.     private static final int TIMEOUT = 2000;
  32.    
  33.     private static final int DATA_RATE = 115200;
  34.    
  35.     public JAVADUINO_JFrame() {
  36.         initComponents();
  37.         inicializarConexion();
  38.     }
  39.  
  40.     public final void inicializarConexion(){
  41.         CommPortIdentifier puertoID = null;
  42.         Enumeration puertoEnum = CommPortIdentifier.getPortIdentifiers();
  43.        
  44.         while(puertoEnum.hasMoreElements()){
  45.             CommPortIdentifier actualPuertoID = (CommPortIdentifier) puertoEnum.nextElement();
  46.             if (PUERTO.equals(actualPuertoID.getName())){
  47.                 puertoID = actualPuertoID;
  48.                 break;
  49.             }
  50.         }
  51.        
  52.             if (puertoID == null){
  53.     mostrarError("No se puede conectar al puerto.");
  54.     System.exit(ERROR);
  55. }
  56.    
  57.     try{
  58.         serialPort = (SerialPort) puertoID.open(this.getClass().getName(), TIMEOUT);
  59.         // Parámetros puerto serie.
  60.         serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
  61.         output = serialPort.getOutputStream();
  62. }
  63.     catch (Exception e){
  64.     mostrarError(e.getMessage());
  65.     System.exit(ERROR);
  66. }
  67.     }
  68.    
  69.  
  70.     private void enviarDatos(String datos){
  71.         try{
  72.             output.write(datos.getBytes());
  73.         }
  74.         catch(Exception e){
  75.             mostrarError("ERROR");
  76.             System.exit(ERROR);
  77.         }
  78.     }
  79.    
  80.     public void mostrarError(String mensaje){
  81.         JOptionPane.showMessageDialog(this, mensaje, "ERROR", JOptionPane.ERROR_MESSAGE);
  82.        
  83.     }
  84.     /**
  85.      * This method is called from within the constructor to initialize the form.
  86.      * WARNING: Do NOT modify this code. The content of this method is always
  87.      * regenerated by the Form Editor.
  88.      */
  89.     @SuppressWarnings("unchecked")
  90.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  91.     private void initComponents() {
  92.  
  93.         jButton1 = new javax.swing.JButton();
  94.         jButton2 = new javax.swing.JButton();
  95.  
  96.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  97.  
  98.         jButton1.setText("ON");
  99.  
  100.         jButton2.setText("OFF");
  101.  
  102.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  103.         getContentPane().setLayout(layout);
  104.         layout.setHorizontalGroup(
  105.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.             .addGroup(layout.createSequentialGroup()
  107.                 .addGap(21, 21, 21)
  108.                 .addComponent(jButton1)
  109.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
  110.                 .addComponent(jButton2)
  111.                 .addGap(45, 45, 45))
  112.         );
  113.         layout.setVerticalGroup(
  114.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  115.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  116.                 .addContainerGap(250, Short.MAX_VALUE)
  117.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  118.                     .addComponent(jButton1)
  119.                     .addComponent(jButton2))
  120.                 .addGap(27, 27, 27))
  121.         );
  122.  
  123.         pack();
  124.     }// </editor-fold>                        
  125.  
  126.     /**
  127.      * @param args the command line arguments
  128.      */
  129.     public static void main(String args[]) {
  130.         /* Set the Nimbus look and feel */
  131.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  132.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  133.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  134.          */
  135.         try {
  136.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  137.                 if ("Nimbus".equals(info.getName())) {
  138.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  139.                     break;
  140.                 }
  141.             }
  142.         } catch (ClassNotFoundException ex) {
  143.             java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  144.         } catch (InstantiationException ex) {
  145.             java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  146.         } catch (IllegalAccessException ex) {
  147.             java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  148.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  149.             java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  150.         }
  151.         //</editor-fold>
  152.  
  153.         /* Create and display the form */
  154.         java.awt.EventQueue.invokeLater(new Runnable() {
  155.             public void run() {
  156.                 new JAVADUINO_JFrame().setVisible(true);
  157.             }
  158.         });
  159.     }
  160.  
  161.     // Variables declaration - do not modify                    
  162.     private javax.swing.JButton jButton1;
  163.     private javax.swing.JButton jButton2;
  164.     // End of variables declaration                  
  165.  
  166.     private void mostrarError(String no_se_puede_conectar_al_puerto) {
  167.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  168.     }
  169. }

Me sale este error.
Cita:
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - method mostrarError(java.lang.String) is already defined in class JAVADUINO_JFrame
at JAVADUINO_JFrame.<clinit>(JAVADUINO_JFrame.java:17 4)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar