📄 principal.java
字号:
/** LiMon-JClient * Copyright (C) 2006 Marcelo Busico * * Author: Marcelo Busico marcelobusico@gmail.com * * This program is free software which I release under the GNU General Public * License. You may redistribute and/or modify this program under the terms * of that license as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. Version 2 is in the * LICENSE.txt file in the top level directory of this distribution. * * To get a copy of the GNU General Puplic License, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package Ventanas;import java.io.IOException;import java.net.UnknownHostException;import limonclient.*;import javax.swing.*;public class Principal extends javax.swing.JFrame { /** Creates new form Principal */ public Principal() { 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. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtDireccion = new javax.swing.JTextField(); txtPuerto = new javax.swing.JTextField(); txtPass = new javax.swing.JPasswordField(); btnConectar = new javax.swing.JButton(); btnSalir = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("LiMon - Linux Monitor"); setResizable(false); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Servidor")); jLabel1.setText("Direcci\u00f3n:"); jLabel2.setText("Puerto:"); jLabel3.setText("Contrase\u00f1a:"); txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtDireccionKeyReleased(evt); } }); txtPuerto.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtPuertoKeyReleased(evt); } }); txtPass.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtPassKeyReleased(evt); } }); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .addContainerGap() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3) .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2) .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(txtDireccion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE) .add(txtPass, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE) .add(txtPuerto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel1) .add(txtDireccion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel2) .add(txtPuerto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(jLabel3) .add(txtPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(14, Short.MAX_VALUE)) ); btnConectar.setText("Conectar"); btnConectar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnConectarActionPerformed(evt); } }); btnConectar.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { btnConectarKeyReleased(evt); } }); btnSalir.setText("Salir"); btnSalir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSalirActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() .add(54, 54, 54) .add(btnSalir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(50, 50, 50) .add(btnConectar)) .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(btnSalir) .add(btnConectar)) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-343)/2, (screenSize.height-189)/2, 343, 189); }// </editor-fold>//GEN-END:initComponents private void btnConectarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnConectarKeyReleased if(evt.getKeyCode()==10) { conectar(); } }//GEN-LAST:event_btnConectarKeyReleased private void btnConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConectarActionPerformed conectar(); }//GEN-LAST:event_btnConectarActionPerformed private void txtPassKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassKeyReleased if(evt.getKeyCode()==10) { btnConectar.grabFocus(); } }//GEN-LAST:event_txtPassKeyReleased private void txtPuertoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPuertoKeyReleased if(evt.getKeyCode()==10) { txtPass.grabFocus(); } }//GEN-LAST:event_txtPuertoKeyReleased private void txtDireccionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyReleased if(evt.getKeyCode()==10) { txtPuerto.grabFocus(); } }//GEN-LAST:event_txtDireccionKeyReleased private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed System.exit(0); }//GEN-LAST:event_btnSalirActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Principal().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnConectar; private javax.swing.JButton btnSalir; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JTextField txtDireccion; private javax.swing.JPasswordField txtPass; private javax.swing.JTextField txtPuerto; // End of variables declaration//GEN-END:variables private void conectar() { // Verificar que los campos no esten vacios if(verificarDireccion(txtDireccion)==false) { txtDireccion.grabFocus(); return; } if(verificarPuerto(txtPuerto)==false) { txtPuerto.grabFocus(); return; } if(verificarPassword(txtPass)==false) { txtPass.grabFocus(); return; } /** Conecta al servidor */ Conexion cn=null; System.out.println("Conectando al servidor..."); try { cn=new Conexion(txtDireccion.getText(), Integer.parseInt(txtPuerto.getText()), new String(txtPass.getPassword())); } catch (IOException e) { //Direccion invalida if(e.getMessage().compareTo("Invalid argument or cannot assign requested address")==0) { JOptionPane.showMessageDialog(this,"Error: Dirección de servidor inválida."); return; } //Servidor desconocido if(UnknownHostException.class.equals(e.getClass())==true) { JOptionPane.showMessageDialog(this,"Error: No se ha encontrado el servidor."); return; } //Puerto rechazado if(e.getMessage().compareTo("Connection refused")==0) { JOptionPane.showMessageDialog(this,"Error: El servidor ha rechazado la conexión al puerto especificado."); return; } //Otro error JOptionPane.showMessageDialog(this,"Error: " + e); } //Pedir confirmacion de contraseña valida Cabecera cab=new Cabecera(Cabecera.IdConfirmaPassword,cn.getPassword()); try { cab.enviarCabecera(cn.getSalida()); //Recibe cabecera del servidor cab.recibirCabecera(cn.getEntrada()); } catch (IOException e) { JOptionPane.showMessageDialog(this,"Error: " + e); } //Verificar cabecera de contraseña if(cab.getIdentificador()==Cabecera.IdPasswordIncorrecta) { JOptionPane.showMessageDialog(this,"Error: La contraseña ingresada es incorrecta"); try { cn.cerrarConexion(); } catch (IOException e) { ; } txtPass.grabFocus(); txtPass.setText(""); return; } //Abre el monitor en tiempo real RTMonitor monitor=null; monitor = new RTMonitor(cn, this); this.setVisible(false); monitor.setVisible(true); } private boolean verificarPuerto (JTextField Texto) { //Verificar que Texto sea numerico positivo y no excesivo int puerto; if(Texto.getText().length()>0) { try { puerto = Integer.parseInt(Texto.getText()); if(puerto<=0) { JOptionPane.showMessageDialog(this, "Puerto: Ingrese un valor mayor a cero."); Texto.setText(null); } else { if(puerto>65535) { JOptionPane.showMessageDialog(this, "Puerto: Ingrese un valor menor a 65536."); Texto.setText(null); } else return true; } } catch(NumberFormatException e) { Texto.setText(null); } } JOptionPane.showMessageDialog(this, "Puerto: Debe ingresar un valor numérico."); return false; } private boolean verificarDireccion (JTextField Texto) { //Verificar que Texto no este vacio if(Texto.getText().length()>0) { return true; } JOptionPane.showMessageDialog(this, "Dirección: Debe ingresar la direccion del servidor."); return false; } private boolean verificarPassword (JTextField Texto) { //Verificar que Texto no este vacio if(Texto.getText().length()>0) { return true; } JOptionPane.showMessageDialog(this, "Contraseña: Debe ingresar la contraseña del servidor."); return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -