你可以使用Toolkit類來獲取屏幕的長寬。以下是一個示例代碼:
import java.awt.Dimension;
import java.awt.Toolkit;
public class Main {
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) screenSize.getWidth();
int screenHeight = (int) screenSize.getHeight();
System.out.println("Screen Width: " + screenWidth);
System.out.println("Screen Height: " + screenHeight);
}
}
上述代碼中,我們使用Toolkit類的getDefaultToolkit()方法獲取Toolkit對象,然后使用getScreenSize()方法獲取屏幕的尺寸,最后通過getWidth()和getHeight()方法獲取屏幕的寬度和高度。