Java JTextArea中的BufferedReader输出

Java JTextArea中的BufferedReader输出,java,swing,jtextarea,bufferedreader,Java,Swing,Jtextarea,Bufferedreader,我试图调用另一个类中的BufferedReader中的一行,我希望能够在我的主类中的JTextArea中输出结果 这是我的主要课程: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.UIManager.LookAndFeelInfo; import java.io.*; impor

我试图调用另一个类中的
BufferedReader
中的一行,我希望能够在我的主类中的
JTextArea
中输出结果

这是我的主要课程:

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)
                            {
                                readCommand.getCommand(llesiant);
                            }
                            else if (selectedIndex == 1)
                            {
                                readCommand.getCommand(wkCCH);
                            }
                            else if (selectedIndex == 2)
                            {
                                readCommand.getCommand(proquest);
                            }
                            else if (selectedIndex == 3)
                            {
                                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);

        }

}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;


public class CommandClass {




    public String line;



    public void getCommand(String ftpSite)

    {


        String command = "ping ";


        try
        {
        Process p = Runtime.getRuntime().exec(command +ftpSite);

        BufferedReader readOutput = new BufferedReader(new InputStreamReader (p.getInputStream()));


        JOptionPane.showMessageDialog(null, "FTP is connected");

        while ((line = readOutput.readLine()) != null)
        {

                System.out.println(line);   

        }
        readOutput.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();

        }

    }





}
这是我的命令类:

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)
                            {
                                readCommand.getCommand(llesiant);
                            }
                            else if (selectedIndex == 1)
                            {
                                readCommand.getCommand(wkCCH);
                            }
                            else if (selectedIndex == 2)
                            {
                                readCommand.getCommand(proquest);
                            }
                            else if (selectedIndex == 3)
                            {
                                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);

        }

}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;


public class CommandClass {




    public String line;



    public void getCommand(String ftpSite)

    {


        String command = "ping ";


        try
        {
        Process p = Runtime.getRuntime().exec(command +ftpSite);

        BufferedReader readOutput = new BufferedReader(new InputStreamReader (p.getInputStream()));


        JOptionPane.showMessageDialog(null, "FTP is connected");

        while ((line = readOutput.readLine()) != null)
        {

                System.out.println(line);   

        }
        readOutput.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();

        }

    }





}
这是我的外观课程:

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.
        }


    }




}

我想做的是:输出在控制台中打印得很好,但我希望能够在mail类的JTextArea中打印出来。我还尝试返回字符串行的值,但它似乎总是返回null。如果我想做的是不可能的。有没有办法让my JTextArea.setText to(commandCLass)

改进您的
getCommand
方法,像这样返回正确的
字符串
,并在主类中使用该返回值显示在
JTextArea

public String getCommand(String ftpSite){
    String command = "ping ";
    StringBuffer output = new StringBuffer();
    try{
         Process p = Runtime.getRuntime().exec(command +ftpSite);
         //BufferedReader readOutput = new BufferedReader(new InputStreamReader (p.getInputStream()));
        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");
         //while ((line = readOutput.readLine()) != null){
              //System.out.println(line);   
              //output.append(line).append("\n");
         //}
         //readOutput.close();
    }catch (IOException e){
        e.printStackTrace();
    }
    return output.toString();
}
FTP类中的if-else块

                CommandClass readCommand = new CommandClass();

                        if (selectedIndex == 0)
                        {
                            commandResultArea.setText(readCommand.getCommand(llesiant));
                        }
                        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));
                        }
FTP类:

我这样做是为了当你ping服务器时,它会返回一个字符串。 您可以修改它以主动编辑JTextField中的文本,但这很好

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

  }

}
命令类:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;


public class CommandClass extends FTP {




  public String line, output = "";



  public String getCommand(String ftpSite)

  {


    String command = "ping ";


    try
    {
      Process p = Runtime.getRuntime().exec(command +ftpSite);

      BufferedReader readOutput = new BufferedReader(new InputStreamReader (p.getInputStream()));


      JOptionPane.showMessageDialog(null, "FTP is connected");

      while ((line = readOutput.readLine()) != null)
      {

        System.out.println(line);   
        output += line + "\n";

      }
      readOutput.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();

    }
    return output;
  }





}

您能解释一下为什么需要扩展FTP类,以及为什么必须将该类声明为返回值的字符串吗。输出字符串的目的似乎是在顶部设置为nothing。我真的很感激我正在尝试学习这个。你能解释一下为什么你需要扩展FTP类,以及为什么你必须将类声明为返回值的字符串吗。输出字符串的目的似乎是在顶部设置为nothing。如果我没有继承
FTP
类,我将非常感激我正在努力学习这一点。我修改了
CommandClass
中的
getCommand
方法。在您的
FTP
类中,if-else梯形图将被更改,如我的帖子所示。更新了我的帖子。另外,我有一个问题,如果我要将命令从ping更改为ftp,当您在cmd或unix上键入ftp时,它不会打印任何消息说ftp已连接,并且它说它已连接的站点是它,因为我没有为我的命令输出流,如果我想在ftp连接时显示消息,我该怎么做或者,当您在cmd或unix上运行ftp命令时,我如何打印打印机通常输出的内容感谢millionI刚刚尝试使用ftp命令,但没有得到任何响应它必须是url还是类似“ftp.llesiant.com”的内容当我尝试在codeUsing
InputStreamReader
中列出的一个ftp站点时,在JTextArea上没有得到任何响应,我能够实现这一点。非
BufferedReader
。更新我的答案。要更快地获得更好的帮助,请发布一个。