是否可以在运行期间使用Java清除Intellij中的控制台选项卡? [英] Is it possible to clear the console tab during runtime in Intellij with Java?
问题描述
我试图在运行时使用Java清除Intellij中的控制台选项卡。
Im trying to clear the console tab in Intellij during run time with Java.
任何想法?
这就是我的尝试:
System.out.print("\033\143");
public static void clrscr(){
//Clears Screen in java
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}
public final static void clearConsole()
{
try
{
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
{
Runtime.getRuntime().exec("cls");
}
else
{
Runtime.getRuntime().exec("clear");
}
}
catch (final Exception e)
{
// Handle any exceptions.
}
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
推荐答案
您可以指定快捷方式控制台上的全部清除
来自首选项>键映射
。在下面的屏幕截图中,我分配了快捷方式 ALT SHIFT 1
来清除控制台。
You could assign a shortcut to Clear All
on the Console from Preferences > Keymap
. In the following screenshot I am assigning the shortcut ALT SHIFT 1
to clear the console.
要测试这个地方将光标放在控制台中并使用该按键组合。
To test this just place your cursor in the console and use that keystroke combination.
这是最简单的部分。接下来,您可以使用 java.awt.Robot
来执行该击键组合。例如:
That's the easy part. Next you can use java.awt.Robot
to exeucte that keystroke combination. For example:
import java.awt.Robot;
import java.awt.event.KeyEvent;
public void clearConsole() {
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_1);
} catch (AWTException ex) {
ex.printStackTrace(System.err);
}
}
使用机器人
可能有点棘手,所以你可能不得不玩这个,包括在执行 ALT SHIFT 1
之前添加按键组合以将焦点切换到控制台
Use of Robot
can be a bit tricky so you'll likely have to play around with that including adding a keystroke combination to switch focus to the console before executing ALT SHIFT 1
这篇关于是否可以在运行期间使用Java清除Intellij中的控制台选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!