如何将方法的输出传递给对话框?

时间:2015-07-27 11:35:35

标签: java methods ant dialog

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

    }

上面的方法在控制台上提供了一些输出,但我在对话框中需要它们。我怎样才能做到这一点 ?

2 个答案:

答案 0 :(得分:2)

我认为您正在尝试将控制台日志捕获到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);
}
}

答案 1 :(得分:1)

也许你需要这个片段:

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

另一方面,如果project.fireBuildFinished(buildException);将输出提供给控制台,则必须输入此输出的返回值,并在“Your Text”位置输入此值,或在此方法中输入此MessageDialog。