You are on page 1of 10

Codigo del JavaClass para que se visualice el gui

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template

*/

package Labjava;

/**

* @author danie

*/

public class labjava {

public static void main(String[] args) {

gui windows=new gui();

windows.setTitle("Control sensor ultrasonido");

windows.setVisible(true);

Código del Jframe Form para el gui:

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

*/

package Labjava;

package arduino2;
import Arduino.Arduino;

import gnu.io.SerialPortEvent;

import gnu.io.SerialPortEventListener;

import java.util.concurrent.TimeUnit;

/**

* @author danie

*/

public class gui extends javax.swing.JFrame {

Arduino Arduino=new Arduino();

SerialPortEventListener evento=new SerialPortEventListener(){

@Override

public void serialEvent(SerialPortEvent spe) {

if(Arduino.MessageAvailable()==true){

System.out.println(Arduino.PrintMessage());

};

/**

* Creates new form gui

*/

public gui() {

initComponents();

try{
Arduino.ArduinoRXTX("COM3", 2000, 9600, evento);

}catch(Exception ex){

/**

* 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() {

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

muestras = new javax.swing.JTextField();

BotonEncender = new javax.swing.JButton();

BotonApagar = new javax.swing.JButton();

jLabel3 = new javax.swing.JLabel();

distancia = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N

jLabel1.setText("Control sensor ultrasonido");


jLabel2.setText("Ingrese el número de muestras");

muestras.setText(" ");

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

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

muestrasActionPerformed(evt);

});

BotonEncender.setText("Encender sistema");

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

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

BotonEncenderActionPerformed(evt);

});

BotonApagar.setText("Apagar sistema");

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

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

BotonApagarActionPerformed(evt);

});

distancia.setText("-----");

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()

.addGap(81, 81, 81)

.addComponent(jLabel1))

.addGroup(layout.createSequentialGroup()

.addGap(28, 28, 28)

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

.addGroup(layout.createSequentialGroup()

.addComponent(BotonEncender)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87,
Short.MAX_VALUE)

.addComponent(BotonApagar))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

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

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

.addGap(56, 56, 56))

.addGroup(layout.createSequentialGroup()

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

.addGroup(layout.createSequentialGroup()

.addGap(75, 75, 75)

.addComponent(jLabel3))

.addGroup(layout.createSequentialGroup()

.addGap(181, 181, 181)

.addComponent(distancia)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);
layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1)

.addGap(43, 43, 43)

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

.addComponent(jLabel2)

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

.addGap(57, 57, 57)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createSequentialGroup()

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

.addComponent(BotonEncender)

.addComponent(BotonApagar))

.addGap(59, 59, 59)

.addComponent(jLabel3))

.addComponent(distancia))

.addContainerGap(56, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

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

// TODO add your handling code here:

muestras.setText("---");

}
private void BotonEncenderActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try{

double mue;

mue=Double.parseDouble(muestras.getText());

for (int i=1;i<=mue;i=i+1){

Arduino.SendData("1");

distancia.setText("Sistema encendido");

TimeUnit.SECONDS.sleep(3);

}catch(Exception ex){

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

// TODO add your handling code here:

try{

Arduino.SendData("2");

distancia.setText("Sistema apagado");

TimeUnit.SECONDS.sleep(3);

}catch(Exception ex){

}
}

/**

* @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(gui.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

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

} catch (IllegalAccessException ex) {

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

} catch (javax.swing.UnsupportedLookAndFeelException ex) {


java.util.logging.Logger.getLogger(gui.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 gui().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton BotonApagar;

private javax.swing.JButton BotonEncender;

private javax.swing.JLabel distancia;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JTextField muestras;

// End of variables declaration

You might also like