以下内容对我有用,但我使用的是 Mac,所以我不能肯定它在 Linux 上也能工作:
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
//Ignore this block, it was simply to give me a chance to change my resolution
Scanner readUserInput=new Scanner(System.in);
System.out.println("Waiting on input, go change your resolution");
String myName=readUserInput.nextLine();
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
Run Code Online (Sandbox Code Playgroud)
输出(blah实际上是输入)如下:
1440x900
Waiting on input, go change your resolution
blah
1280x960
Run Code Online (Sandbox Code Playgroud)
显然,如果您有多个屏幕设备,则必须更加小心。