ventanacliente.java

来自「Example FTP server client/server」· Java 代码 · 共 209 行

JAVA
209
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * ventanaCliente.java * * Created on 31/03/2009, 05:35:03 AM */package vista;import controlador.cliente;import java.io.File;import java.io.IOException;import java.net.UnknownHostException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JFileChooser;import javax.swing.JOptionPane;/** * * @author Camilo Andres */public class ventanaCliente extends javax.swing.JFrame {    private  cliente client;    /** Creates new form ventanaCliente */    public ventanaCliente(cliente client)    {        initComponents();        setVisible(true);        this.client=client;        try        {                       String []lista=client.listaArchivosServidor();            if (lista!=null)ListaArchivos.setListData(lista);        }        catch (Exception ex)        {            ex.printStackTrace();            JOptionPane.showMessageDialog(null, "No se pudo conectar al servidor FTP","Error",JOptionPane.ERROR_MESSAGE)                ;        }    }    /** 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() {        jScrollPane1 = new javax.swing.JScrollPane();        ListaArchivos = new javax.swing.JList();        BDownLoad = new javax.swing.JButton();        BUpload = new javax.swing.JButton();        jButton1 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setResizable(false);        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista Archivos"));        jScrollPane1.setName("jScrollPane1"); // NOI18N        ListaArchivos.setName("ListaArchivos"); // NOI18N        jScrollPane1.setViewportView(ListaArchivos);        BDownLoad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/Download_48x48-32.png"))); // NOI18N        BDownLoad.setText("Descargar");        BDownLoad.setName("BDownLoad"); // NOI18N        BDownLoad.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                BDownLoadActionPerformed(evt);            }        });        BUpload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/UpLoad_48x48-32.png"))); // NOI18N        BUpload.setText("    Subir");        BUpload.setName("BUpload"); // NOI18N        BUpload.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                BUploadActionPerformed(evt);            }        });        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/Refresh_48x48-32.png"))); // NOI18N        jButton1.setText("Refrescar");        jButton1.setName("jButton1"); // NOI18N        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jButton1ActionPerformed(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()                .addContainerGap()                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    .addComponent(BUpload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)                    .addComponent(BDownLoad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))                .addGap(30, 30, 30))        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addGap(21, 21, 21)                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(BDownLoad)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(BUpload, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))                    .addGroup(layout.createSequentialGroup()                        .addContainerGap()                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void BDownLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDownLoadActionPerformed        // TODO add your handling code here:()        String nombreArchivo=(String)ListaArchivos.getSelectedValue();        try {            client.recibirArchivo(nombreArchivo);           // JOptionPane.showMessageDialog(null, "Descarga Exitosa");        }        catch (Exception ex)        {            Logger.getLogger(ventanaCliente.class.getName()).log(Level.SEVERE, null, ex);            JOptionPane.showMessageDialog(null, "Error al descargar el Archivo","Error",JOptionPane.ERROR_MESSAGE)                ;        }    }//GEN-LAST:event_BDownLoadActionPerformed    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed        try {            // TODO add your handling code here:            String[] lista = client.listaArchivosServidor();            if (lista != null) {                ListaArchivos.setListData(lista);            }        } catch (IOException ex) {            Logger.getLogger(ventanaCliente.class.getName()).log(Level.SEVERE, null, ex);        } catch (ClassNotFoundException ex) {            Logger.getLogger(ventanaCliente.class.getName()).log(Level.SEVERE, null, ex);        }    }//GEN-LAST:event_jButton1ActionPerformed    private void BUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BUploadActionPerformed        JFileChooser elegirFile=new JFileChooser();        int opc=elegirFile.showOpenDialog(this);        if (opc==JFileChooser.APPROVE_OPTION)        {                    try                    {                        File f = elegirFile.getSelectedFile();                        client.enviarArchivo(f);                    }                    catch (IOException ex)                    {                        Logger.getLogger(ventanaCliente.class.getName()).log(Level.SEVERE, null, ex);                    }                    catch (ClassNotFoundException ex)                    {                        Logger.getLogger(ventanaCliente.class.getName()).log(Level.SEVERE, null, ex);                    }        }    }//GEN-LAST:event_BUploadActionPerformed    /**    * @param args the command line arguments    */        // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton BDownLoad;    private javax.swing.JButton BUpload;    private javax.swing.JList ListaArchivos;    private javax.swing.JButton jButton1;    private javax.swing.JScrollPane jScrollPane1;    // End of variables declaration//GEN-END:variables}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?