以下代码使用java.awt.GraphicsDevice
和javafx.stage.Screen
来获取屏幕尺寸(以像素为单位)。 JavaFX似乎在Linux下出错了,如下所示(但在Windows 7下是正确的)。我很想知道是否有其他人遇到过同样明显的错误。
import javafx.application.Application;
import javafx.stage.*;
import java.awt.*;
public class DisplayCheck extends Application {
static int getScreenWidthViaAWT(int screenNum) {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[screenNum];
return gd.getDisplayMode().getWidth();
}
static int getScreenHeightViaAWT(int screenNum) {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[screenNum];
return gd.getDisplayMode().getHeight();
}
static int getScreenWidthViaJavaFX(int screenNum) {
return (int)Screen.getScreens().get(screenNum).getVisualBounds().getWidth();
}
static int getScreenHeightViaJavaFX(int screenNum) {
return (int)Screen.getScreens().get(screenNum).getVisualBounds().getHeight();
}
public static void main(String[] args) {
int w0 = getScreenWidthViaAWT(0);
int h0 = getScreenHeightViaAWT(0);
int w1 = getScreenWidthViaAWT(1);
int h1 = getScreenHeightViaAWT(1);
System.out.println ("\n\nScreen sizes from java.awt.GraphicsDevice:\n\nScreen 0: " + w0 + " x " + h0 + "\nScreen 1: " + w1 + " x " + h1);
w0 = getScreenWidthViaJavaFX(0);
h0 = getScreenHeightViaJavaFX(0);
w1 = getScreenWidthViaJavaFX(1);
h1 = getScreenHeightViaJavaFX(1);
System.out.println ("\n\nScreen sizes from javafx.stage.Screen:\n\nScreen 0: " + w0 + " x " + h0 + "\nScreen 1: " + w1 + " x " + h1);
launch(args);
}
public void start (Stage stage) {
System.exit(0);
}
}
在我的系统(Ubuntu MATE)上,JavaFX将我的笔记本电脑屏幕和外接显示器的高度误报为718像素。 AWT做对了。结果如下:
john@jlaptop2:/java$ javac DisplayCheck.java
john@jlaptop2:/java$ java DisplayCheck
Screen sizes from java.awt.GraphicsDevice:
Screen 0: 1024 x 768
Screen 1: 1920 x 1080
Screen sizes from javafx.stage.Screen:
Screen 0: 1920 x 718
Screen 1: 1024 x 718
john@jlaptop2:/java$
答案 0 :(得分:1)
您使用Screen.getVisualBounds
代替Screen.getBounds
。视觉边界不包括任务栏占据的区域,等等。
来自Screen.getVisualBounds
的javadoc:
这些边界考虑了本机窗口系统中的对象,例如任务栏和菜单栏。