该程序是JSch的一部分,之前用于读取输出内容然后将其打印到控制台,但我已经制作了具有textPanel的UI,我想将提取的内容写入文本区域。但我得到Null指针异常。我调用textPanel的方式有问题。
JumpHosts - http://www.jcraft.com/jsch/examples/JumpHosts.java.html(完整计划)
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
//System.out.print(new String(tmp, 0, i));
textPanel.appendText(new String(tmp, 0, i));
}
if (channel.isClosed()) {
//System.out.println("exit-status: " + channel.getExitStatus());
textPanel.appendText(new String("exit-status: " + channel.getExitStatus()));
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee);
}
}`
文字面板代码:
public class TextPanel extends JPanel {
private JTextArea textArea;
public TextPanel(){
setLayout(new BorderLayout());
textArea = new JTextArea();
add(new JScrollPane(textArea),BorderLayout.CENTER);
}
public void appendText(String tmp){
textArea.append(tmp);
}
文本面板在MainFrame类中初始化:
public class MainFrame extends JFrame{
private TextPanel textPanel;
private Toolbars toolbar;
private FormPanel formPanel;;
public MainFrame() {
super("ABC");
setLayout(new BorderLayout());
//setJMenuBar(createMenuBar());
textPanel = new TextPanel();
toolbar = new Toolbars();
formPanel = new FormPanel();
toolbar.setStringListener(new StringListeners() {
public void textEmitter(String text) {
textPanel.appendText(text);
}
});
formPanel.setFormListener(new FormListener(){
public void formEventOccured(FormEvent e){
String name = e.getName();
String occupation = e.getOccupation();
textPanel.appendText(name + " : " + occupation + "\n");
}
});
add(textPanel,BorderLayout.CENTER);
add(toolbar,BorderLayout.NORTH);
add(formPanel,BorderLayout.WEST);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(500, 400));
setSize(600, 500);
}