【问题标题】:How to clear console in Java - Eclipse SDK [duplicate]如何在 Java 中清除控制台 - Eclipse SDK [重复]
【发布时间】:2012-05-09 03:31:58
【问题描述】:

我已经搜索了很多地方,但还没有找到有用的东西...... 一定有办法!!!所以,我需要的是在 Eclipse 中清除控制台的代码(使其空白)。不,不打印 50 个空白行,清除它!

我发现了这个:

Import import java.io.Console;

public void ClearConsole() {
            Console console = System.console();        
            if (console == null)
                    System.out.println("Couldn't get Console object !");
            console.clear();
    }

但它给了我一个错误:“方法 clear() 没有为 Console 类型定义

【问题讨论】:

标签: java


【解决方案1】:

在 Eclipse 工具中,您可以通过 右键单击 + clear 来清除控制台面板,但在 Java 中则不行。

控制台是一个日志工具,为了管理安全,它不能被清除。

【讨论】:

  • OP 正在寻找一种以编程方式执行此操作的方法。你也可以点击 Eclipse 控制台右上角的灰色小十字来清除它:)
  • @Jerome 正如 OP 所说的 Eclipse,你给出了最懒惰的解决方案。
【解决方案2】:

我的回答可能会迟到,但这是我设法做到的(它对我有用):

我根据本教程 http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F 创建了我的控制台,并将 findConsole 方法修改为如下所示:

private MessageConsole findConsole(String name) {

      ConsolePlugin plugin = ConsolePlugin.getDefault();
      IConsoleManager conMan = plugin.getConsoleManager();

      IConsole[] existing = conMan.getConsoles();
      //if console exists, clear it 
      for (int i = 0; i < existing.length; i++)
          if (name.equals(existing[i].getName())){
              ((MessageConsole) existing[i]).clearConsole(); //this is the important part
              return myConsole;
          }

      myConsole = new MessageConsole(name, null);
      conMan.addConsoles(new IConsole[]{myConsole});
      return myConsole;
   }

所以,在其他按钮/控件/其他的监听器中,我有:

myConsole = findConsole(ASIO_RECORD_OUTPUT);
myConsoleOut = myConsole.newMessageStream();

每当执行那段代码时,我的控制台就会被清除。希望对您有所帮助。

编辑:忘了提,我在创建 RCP 应用程序时这样做了!

【讨论】:

  • 您需要显示与此问题相关的导入语句,否则会产生很多不可能的猜测。
猜你喜欢
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多