如何将方法的输出传输到对话框


try {
        project.fireBuildStarted();
        project.init();
        ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
        project.addReference("ant.projectHelper", projectHelper);
        projectHelper.parse(project, buildFile);
        // If no target specified then default target will be executed.
        String targetToExecute = (target != null && target.trim().length() > 0) ? target
                .trim() : project.getDefaultTarget();
        project.executeTarget(targetToExecute);
        project.fireBuildFinished(null);
        success = true;
    } catch (BuildException buildException) {
        project.fireBuildFinished(buildException);
        JOptionPane.showMessageDialog(null, buildException, "Warning",
                JOptionPane.WARNING_MESSAGE);
        throw new RuntimeException(
                "!!! Unable to restart the IEHS App !!!", buildException);
    }

以上方法在控制台上给出一些输出,但我需要在对话框中使用它们。我该怎么做呢?

我认为您正在尝试捕获控制台日志到UI组件,如果是这种情况,这里是将创建输出流和捕获控制台输出并在UI组件中显示它的示例。你可以试试这个

public class Console extends JDialog {
JTextArea textArea = null;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        Console dialog = new Console();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private void updateTextPane(final String text) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            textArea.append(text);  
        }
    });
}
/**
 * Create the dialog.
 */
public Console() {
    //Creating a stream to move consle output to anything else
    OutputStream out = new OutputStream() {
        @Override
        public void write(final int b) throws IOException {
            updateTextPane(String.valueOf((char) b));
        }
        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextPane(new String(b, off, len));
        }
        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };
    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(out, true));
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(null);
    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("hello");
        }
    });
    btnNewButton.setBounds(91, 192, 91, 23);
    getContentPane().add(btnNewButton);
    textArea = new JTextArea();
    textArea.setBounds(30, 11, 241, 136);
    getContentPane().add(textArea);
}
}

也许你需要这样的代码片段:

JOptionPane.showMessageDialog(null, "Your Text");

另一方面,如果project.fireBuildFinished(buildException);向控制台输出,那么您必须输入该输出的返回值并将其输入到"您的文本"位置或将此MessageDialog输入到您的方法中。

相关内容

  • 没有找到相关文章