如何在textPane中显示system.out.print()

时间:2017-05-09 18:48:10

标签: jtextpane

我的Game类中有一个printHelp(),我试图在textPane的GameGUI类中显示它。我收到一条错误消息,因为printHelp()返回void类型。任何人都可以提出建议吗?非常感谢!

JTextPane textPane = new JTextPane();
contentPane.add(textPane, BorderLayout.CENTER);
textPane.replaceSelection("whatever printed in printHelp() needs to be displayed here.");


protected void printHelp()
{
    System.out.println("You are lost. You are alone. You wander");
    System.out.println("around at the university.");
    System.out.println("");
    System.out.println("Your command words are:");
    parser.showCommands();
}

1 个答案:

答案 0 :(得分:0)

用这个替换你的printHelp方法:

protected String printHelp()
{
    String message = "You are lost. You are alone. You wander"
    +"\n"+"around at the university."
    +"\n"
    +"\n"+"Your command words are:";
    message += parser.showCommands();
    return message;
}

然后:

textPane.replaceSelection(printHelp());

或者,如果需要,请使用:

private void printBoth() {
    String message = printHelp();
    textPane.replaceSelection(message);
    System.out.println(message);
}