3

我用 Java 为 Linux 开发了一个任务管理器。现在的输出显示在控制台中,而选项卡式窗口单独出现(这是使用 Java Swing 完成的)。现在我希望控制台的输出显示在选项卡式窗口中。

我该怎么做?

有我用过的类。一个用于任务管理器功能,另一个用于 GUI。我已经粘贴在编码下方。

**TabbedPaneDemo1.java**

package components;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;


/*
 * TabbedPaneDemo.java requires one additional file:
 *   images/middle.gif.
 */

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class TabbedPaneDemo1
{

public static void main(String args[]) throws Exception
{
    try
    {   
        TextArea textarea = new TextArea();
        TabbedPaneDemo obj = new TabbedPaneDemo();
        obj.fn();
        String line;
        String result = "";
        FileOutputStream out;
        //FileOutputStream out; // declare a file output object
        PrintStream p;
        Process p1 = Runtime.getRuntime().exec("tasklist.exe");
        out = new FileOutputStream("myfile.txt");//write to a file//
        p = new PrintStream( out );
        BufferedReader input = new BufferedReader(new InputStreamReader(p1.getInputStream()));//read form a file//
        while ((line = input.readLine()) != null)
        {
            System.out.println(line);
            //textarea.append(line + "\n");
            result += line+"\n";
            p.println (line);
            //textarea.setVisible(true);    
        }
        //msgBox(result);
        p.close();
        input.close();   
    }
    catch(Exception e)
    {

    }
}
    public static void msgBox(String msg) {
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
    null, msg, "WindowsUtils",
    javax.swing.JOptionPane.DEFAULT_OPTION);
    }
}

**TabbedPaneDemo.java**

package components;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class TabbedPaneDemo extends JPanel {
Integer i;
public TabbedPaneDemo() {
    super(new GridLayout(1, 1));

    JTabbedPane tabbedPane = new JTabbedPane();
    ImageIcon icon = createImageIcon("images");


    JComponent panel1 = makeTextPanel("tasklist");
    tabbedPane.addTab("tasks", icon, panel1,
            "ta");
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

    JComponent panel2 = makeTextPanel("windows");
    tabbedPane.addTab("wins", icon, panel2,
            "wi");
    tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


    add(tabbedPane);`enter code here`
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);    
    }
    protected JComponent makeTextPanel(String text)
    {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TabbedPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
    public static void fn() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        createAndShowGUI();
            }
        });
    }
}
4

2 回答 2

3

这是一个非常幼稚的实现。

package test.t100.t001;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.*;

public class TabbedPaneDemo extends JPanel {

    private static final long serialVersionUID = 1L;
    Integer i;

    JTextArea output = new JTextArea();

    public static void main(String args[]) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                JFrame frame = new JFrame("TabbedPaneDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private String getDetails() throws IOException {
        //fn();
        String line;
        String result = "";
        PrintStream p;
        Process p1 = Runtime.getRuntime().exec("tasklist.exe");
        // read from a process
        BufferedReader input = new BufferedReader(
                new InputStreamReader(p1.getInputStream()));
        while ((line = input.readLine()) != null)
        {
            //System.out.println(line);
            output.append(line + "\n");
            result += line+"\n";
            //p.println (line);
            //textarea.setVisible(true);    
        }
        //msgBox(result);
        //p.close();
        input.close();   

        return result;
    }

    public TabbedPaneDemo() {
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = createImageIcon("images");


        JComponent panel1 = makeTextPanel("tasklist");
        tabbedPane.addTab("tasks", icon, panel1,
                "ta");
        // add it to something!
        panel1.add(new JScrollPane(output));
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        JComponent panel2 = makeTextPanel("windows");
        tabbedPane.addTab("wins", icon, panel2,
                "wi");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


        add(tabbedPane);//`enter code here`
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        try {
            String s = getDetails();
            output.setText(s);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }


    public static void msgBox(String msg) {
        javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
                null, msg, "WindowsUtils",
                javax.swing.JOptionPane.DEFAULT_OPTION);
    }


    protected JComponent makeTextPanel(String text)
    {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

其他注意事项:

  1. 为了尽快获得更好的帮助,请发布SSCCE。特别是不要包含不必要的图像。
  2. 对这种类型的数据使用 a JTable
  3. 该代码将文本区域转储到一个选项卡式窗格中,该窗格显然具有GridLayout- BNI
  4. 不要无故混用 Swing 和 AWT 组件。
  5. 如前所述,使用 aSwingWorker获取数据。

更新

..但是控制台的输出没有出现。

嗯。让我们想想

我用 Java为Linux开发了一个任务管理器

..结合..

Process p1 = Runtime.getRuntime().exec("tasklist.exe");

现在,如果我误解了什么,请纠正我,但 AFIAU Linux 没有 EXE。

我猜代码在 Linux 上会失败,并且由于err进程的流被忽略了,你没有被告知原因。这是上面显示的代码的屏幕截图,它出现在Windows 7 上。

选项卡式窗格中的工作输出

现在,如果数据是硬编码的,您可能会看到类似上面的内容。哪个回答了“(如何)在选项卡式窗口中显示输出”的问题,对吗?

更多信息。您看到的问题是通过实施When Runtime.exec() won't的所有建议来解决的。如果您实施了所有建议并且仍然无法解决问题,我建议您提出一个新问题。

于 2012-05-28T11:53:23.697 回答
2

只有点

  • TextArea textarea = new TextArea();为什么选择 AWT 组件

    使用摇摆JTextArea textarea = new JTextArea();

您可以JTextArea直接将数据加载到

  • FileOutputStreamProcess p1 = Runtime.getRuntime()

或从File

  • JTextArea.read(File)接受"\n"

或来自FileReaderFileXxxxei

  • JTextArea.append(String)

在 Swing 中遇到 Concurency问题,您必须调用 / 来重定向

Process p1 = Runtime.getRuntime()

FileIO

到后台任务,有

  • 通过调用 from Runnable#Thread(输出到JTextArea应该被包装到invokeLater)

或者

  • 使用SwingWorker(输出processpublish可以在 上完成Event Dispatch Thread
于 2012-05-28T10:42:13.280 回答