我是JAVA语言的初学者,正在尝试学习如何创建全屏应用程序。
我正在尝试创建一个简单的应用程序,在运行时,在屏幕中央显示带有蓝色背景和简单文本行的全屏(从位置400x300开始)。应用程序的分辨率设置为800x600。
我在运行OSX Lion的MacbookAir上运行代码,屏幕分辨率为1440x900。问题是,尽管获得了我预期的蓝色背景,但文本仅显示在屏幕的左上角。当我增加它的位置时,文本的位置将继续向下移动,直到它消失时超过1440x900。我猜测屏幕分辨率仍然设置为1440x900,而不是800x600。
这是我的主要课程:
import java.awt.*;
import javax.swing.JFrame;
public class bucky extends JFrame {
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
bucky b = new bucky();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.BLUE); // Setting background color
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
//The Screen variable is going to be a screen object
Screen s = new Screen();
try{
s.setFullScreen(dm, this); //this refers to whatever object we are working on, ie 's'
try{
//Once it is set to full screen, and wait for 5 second
Thread.sleep(5000);
}catch(Exception ex){};
}
finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("Test", 400, 300);
}
}
以下是Screen类的Screen类和方法的构造函数,它将应用程序设置为全屏
private GraphicsDevice vc; // Gives an interface to graphics card/video card.
public Screen(){
//env is environment variable, containing all graphics manipulation objects
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
//When we get environment and getDegaultScreen Device, we get access to the entire monitor, not just a window
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if(dm != null && vc.isDisplayChangeSupported()) {
try{
vc.setDisplayMode(dm);
}catch(Exception ex){}
}
}
如果有人能指出我为什么没有正确设置决议,我们将非常感激。
答案 0 :(得分:2)
显示模式必须是返回的显示模式之一 getDisplayModes(),但有一个例外:传递显示模式 DisplayMode.REFRESH_RATE_UNKNOWN刷新率将导致选择 具有匹配功能的可用显示模式列表中的显示模式 宽度,高度和钻头深度。
因此,您需要确保将显示模式设置为其中之一。
答案 1 :(得分:0)
我认为答案是GraphicsDevice.setDisplayMode(...)的javadoc。
您应首先检查if (gd.isFullScreenSupported())
,然后致电setFullScreenWindow(frame)
,或使用frame.setSize(...)
方法模拟全屏模式。