java代码中的本地方法的输出,如何重定向到文本区域控件?
网上看到的类似代码,只能将System.out.println("abcd");这样的句子的输出重定向到某个文本框内,而我的代码是:JAVA调用了一个由MATLAB编译后生成的JAR文件中的方法,这有点类似于JAVA调用DLL动态库吧。在这个JAR文件中的方法的输出可以在控制台上看到,却无法重定向到我指定的文本区域中,不知哪位高手有办法?

解决方案 »

  1.   

    System有个setOut(PrintStream out) 方法
    不知道是否满足LZ的要求
      

  2.   

    lz
    看看输入输出重定向的API
      

  3.   

    跟你说下java调用动态连接库的吧,或许有些帮助:(实现的是在java中点击按钮,会将结果显示到文本框中)
    java中的代码如下:package hello;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;import javax.swing.*;class test10{
    static {
    System.loadLibrary("TestDll");
    }
        public test10(){
         
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("显示");
        final JTextArea textarea = new JTextArea(10,20);
        frame.add(panel);
        panel.add(button);
        panel.add(textarea);     
        frame.setSize(300, 400);
        frame.setLocation(500, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      
        
        button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {test10 t=new test10();
    int a=1,b=2;
    int c=add(a,b);
    Integer.toString(c);
    textarea.setText(Integer.toString(c));
    }
    });
        }
        public native int add(int a,int b);
        public static void main(String args[]){ 
         new test10();
    }
    }
    在C++中的动态链接库中代码如下:/* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class hello_test10 */#ifndef _Included_hello_test10
    #define _Included_hello_test10
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     hello_test10
     * Method:    add
     * Signature: (II)I
     */
    JNIEXPORT jint JNICALL Java_hello_test10_add
      (JNIEnv *, jobject, jint, jint);#ifdef __cplusplus
    }
    #endif
    #endif
      

  4.   

    最简单的方法就是在输入的时候将标准输出流重新设置一下:然后再调用要输出的类
    代码可以参考一下:import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;class Test3{ public Test3() {
    System.out.println("Test3 constructor!");
    }

    public void add(){
    System.out.println("Test3 --> add()");
    }
    }
    public class Test2 {
    public static void main(String[] args) throws IOException {
    FileOutputStream fos = new FileOutputStream("C:/test.txt");
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);
    Test3 t3 = new Test3();
    t3.add();
    fos.close();
    ps.close();
    }
    }