【问题标题】:GUI is frozen whent he ftp site is not connected未连接 ftp 站点时 GUI 冻结
【发布时间】:2013-02-24 15:50:25
【问题描述】:

这是我的主要课程:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import java.io.*;
import java.lang.Process.*;

public class FTP {



  public static void main (String []args)
  {
    Runnable runner = new Runnable(){



      public void run()
      {

        LookAndFeel nimbusLook = new LookAndFeel();
        nimbusLook.NimbusLookAndFeel();

        JFrame frame = new JFrame("BNA FTP Diagnose");
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(540, 420);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);


        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        final JMenuItem exitMenuItem = new JMenuItem("Exit");
        fileMenu.add(exitMenuItem);


        JMenu helpMenu = new JMenu("Help");
        menuBar.add(new JPanel());
        menuBar.add(helpMenu);
        final JMenuItem aboutMenuItem = new JMenuItem("About");
        helpMenu.add(aboutMenuItem);



        JPanel titlePanel = new JPanel(new BorderLayout());
        frame.add(titlePanel, BorderLayout.NORTH);

        JLabel titleLabel = new JLabel("FTP Diagnose", JLabel.CENTER);
        titleLabel.setFont(new Font(null, Font.BOLD, 14));
        titleLabel.setForeground(Color.BLUE);
        titlePanel.add(titleLabel);

        JPanel gridPanel = new JPanel(new GridLayout(1, 1));
        frame.add(gridPanel);

        JPanel vendorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        gridPanel.add(vendorPanel);

        final String vendor [] = {"LLesiant" ,"WK-CCH", "Proquest", "Notes", "Research Institute of America", "Thomson", 
          "BNAI PDF Processing", " TM Portfolios to Indexing", "Postscript to PRODLOGIN1", "www.firstdoor.net", "psp.bna.com", "WEST", "LexisNexis", "McArdle Printing Company", 
          "Breaking News Email", "Ex Libris", "Pandora", "Bloomberg Law", "Acquire Media Site 1", "Acquire Media Site 2", "Quicksite", "QA Quicksite"};
        final JComboBox vendorList = new JComboBox(vendor);
        vendorPanel.add(vendorList);

        JPanel diagnoseButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        gridPanel.add(diagnoseButtonPanel);
        final JButton diagnoseButton = new JButton("Diagnose");
        diagnoseButtonPanel.add(diagnoseButton);


        JPanel centerPanel = new JPanel(new BorderLayout());
        frame.add(centerPanel, BorderLayout.SOUTH);
        JPanel commandPanel = new JPanel(new GridLayout(1, 0));
        centerPanel.add(commandPanel);


        final JTextArea commandResultArea = new JTextArea(7, 0);
        JScrollPane scroll = new JScrollPane(commandResultArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        commandPanel.add(scroll);
        commandResultArea.setEditable(false);




        ActionListener buttonListener = new ActionListener(){

          public void actionPerformed(ActionEvent ae)

          {
            int selectedIndex = vendorList.getSelectedIndex();

            String llesiant = "ftp.llesiant.com";
            String wkCCH = "FTP1.cch.com";
            String proquest = "ftp.proquest.com";
            String notes = "notes5.bna.com";


            //String lineThree = null;

            CommandClass readCommand = new CommandClass();

            if (selectedIndex == 0)
            {
              commandResultArea.setText(readCommand.getCommand(llesiant)); //these return strings

            }
            else if (selectedIndex == 1)
            {
              commandResultArea.setText(readCommand.getCommand(wkCCH));
            }
            else if (selectedIndex == 2)
            {
              commandResultArea.setText(readCommand.getCommand(proquest));
            }
            else if (selectedIndex == 3)
            {
              commandResultArea.setText(readCommand.getCommand(notes));
            }

          }

        };
        diagnoseButton.addActionListener(buttonListener);

        ActionListener exitListener = new ActionListener (){

          public void actionPerformed(ActionEvent el)
          {

            if (el.getSource()== exitMenuItem)
            {
              JOptionPane.showMessageDialog(null, "FTP Program will exit now!");
              System.exit(0);
            }

          }

        };

        exitMenuItem.addActionListener(exitListener);

        ActionListener aboutListener = new ActionListener()
        {
          public void actionPerformed(ActionEvent al)
          {

            if (al.getSource()== aboutMenuItem)

            {
              JOptionPane.showMessageDialog(null, "This Software was made for Editors to. \nDiagnose BNA Bloomberg client FTP site.");

            }
          }

        };
        aboutMenuItem.addActionListener(aboutListener);             
      }

    };
    EventQueue.invokeLater(runner);

  }

}

Here is my Look and feel class: 

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class LookAndFeel {


    public void NimbusLookAndFeel()
    {

        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }


    }




}


Here is my CommandClass: 

import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;


public class CommandClass {



    public String getCommand(String ftpSite){
        String command = "ftp ";
        StringBuffer output = new StringBuffer();
        try{
             Process p = Runtime.getRuntime().exec(command +ftpSite);
            InputStreamReader ir = new InputStreamReader (p.getInputStream());
            int outputChar = 0;
            while((outputChar = ir.read()) != -1){
                output.append((char)outputChar);
            if(!ir.ready()){ // If the next read is not guarenteed, come out of loop.
                break;
            }
                }
              ir.close();
             JOptionPane.showMessageDialog(null, "FTP is connected");

        }catch (IOException e){
            e.printStackTrace();
        }
        return output.toString();
    }


}

我有这个 FTP GUI,它假设连接到一个 FTP 站点并返回状态。如果已连接,则会显示连接提示。

我收到JTextArea 以在建立 FTP 连接时显示消息,但是当它未连接到 ftp 站点时,例如我的第 4 个 ftp 站点 notes5.bna.com,它会冻结程序。还有一个小问题是,如果您有程序 FTP 到像“shlfsdklaflkhasdlhfas”这样的站点。只有在JOptionPane 表明 FTP 已连接后,它才会返回未找到 ftp 站点。我不确定它有什么问题。

【问题讨论】:

    标签: java swing user-interface ftp


    【解决方案1】:

    notes5.bna.com 冻结的原因是该站点没有响应FTP 连接请求。由于您只是使用Runtime#exec 进行连接,因此这将无限期地阻止响应。考虑使用 3rd 方 FTP 客户端,例如 Apache FTPClient,它允许您指定 连接超时

    一个相关的问题是某些网站要求用户名和密码才能访问。同样FTPClient 允许您提供登录详细信息。

    最后但同样重要的是,不要让重量级的非 UI 任务冻结您的 Swing 应用程序。 Swing 有处理这些问题的机制,例如SwingWorker objects

    【讨论】:

    • 那么有什么办法可以解决它。你能告诉我当我 ftp 到 nots5.bna.com 时我需要进行什么类型的修改,它在命令提示符“未连接”上显示。当我遇到这种情况时,有没有办法显示它。你所说的摇摆工人是什么意思,有没有办法不让我的程序冻结,以防万一我遇到这种问题。如果有你能告诉我如何或给我一个例子
    • 您需要在此处进行一些重构。下载 FTPClient 的 jar。请参阅 javadoc,它包含一个简单的示例。先让这个工作。然后构建一个简单的SwingWorker 测试应用程序。从上面的SwingWorker objects 追踪。这是another example。把两个放在一起。
    • 这似乎我可能需要更改很多代码。有没有一种更简单的方法可以通过 Joption 提示告诉用户主机未连接或将其写入我的 JTextArea 而不是完成所有这些过程。
    • 我会试一试,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多