也许用到的机会很少 但JDK还是为我们提供了这个的功能 像许多软件中的打印预览功能 还有某些文本编辑器中为了获得更大的编辑画面 也用到了Swing全屏幕模式 如果你有兴趣写一个像ACDSee这样的软件 使用Swing全屏幕模式可以让用户看到更大的图片画面
如何使用Swing全屏幕模式?
关键是java awt *里面的两个与显示设备有关的类 GraphicsEnvironment和GraphicsDevice
GraphicsEnvironment为Java应用程序提供了特定平台的 GraphicsDevice对象和 Font 对象集合 这些GraphicsDevice可以是各种本机和远端机器的资源 如屏幕 打印机或者是Image Buffer 甚至是Graphics D绘图方法的目标对象
而GraphicsDevice就是指特定的图形环境了 如屏幕和打印设备等 这样 我们就可以用GraphicsDevice来 *** 纵屏幕了 GraphicsDevice提供的setFullScreenWindow()方法就是设置全屏幕用的
由于GraphicsEnvironment的构造器是受保护的(protected) 我们不能直接构造一个GraphicsEnvironment 对象来获得GraphicsDevice对象 幸好它提供了getLocalGraphicsEnvironment()方法 用来获得一个 GraphicsEnvironment实例
GraphicsEnvironment ge = GraphicsEnvironment getLocalGraphicsEnvironment()
有了GraphicsEnvironment可以调用getDefaultScreenDevice方法获得当前的Swing全屏幕模式设备了
GraphicsDevice gd = ge getDefaultScreenDevice()
自己动手体验一下
有了上面的简介 写一个实例来体验一下吧
importjava awt Color
importjava awt Font
importjava awt Graphics
importjava awt GraphicsDevice
importjava awt GraphicsEnvironment
importjava awt event MouseAdapter
importjava awt event MouseEvent
importjavax swing JWindow
publicclassFullScreenTest{
publicstaticvoidmain(String[]args){
GraphicsEnvironmentGraphicsEnvironmentge=
GraphicsEnvironment getLocalGraphicsEnvironment()
GraphicsDevicegd=ge getDefaultScreenDevice()
FullScreenWindowmyWindow=newFullScreenWindow()
if(gd isFullScreenSupported())
gd setFullScreenWindow(myWindow)
else
System out println( Unsupportedfullscreen )
}
staticclassFullScreenWindowextendsJWindow{
publicFullScreenWindow(){
this addMouseListener(newMouseAdapter(){
publicvoidmousePressed(MouseEventevt){
quit()
}
})
}
publicvoidquit(){
this dispose()
}
publicvoidpaint(Graphicsg){
g setFont(newFont( Arial Font BOLD ))
g setColor(Color RED)
g drawString( 这是全屏幕模式 )
}
}
lishixinzhi/Article/program/Java/hx/201311/27030public void this_windowLostFocus(WindowEvent e) {
this.requestFocus()
this.setLocation(0,0)
}
public void this_windowIconified(WindowEvent e) {
this.setExtendedState(JFrame.MAXIMIZED_BOTH)
}
Java代码
import java awt Color
import java awt DisplayMode
import java awt Font
import java awt Graphics
import javax swing JFrame
public class FullScreenTest extends JFrame {
public static void main(String[] args) {
DisplayMode displayMode
if (args length == ) {
displayMode = new DisplayMode(
Integer parseInt(args[ ])
Integer parseInt(args[ ])
Integer parseInt(args[ ])
DisplayMode REFRESH_RATE_UNKNOWN)
}
else {
displayMode = new DisplayMode(
DisplayMode REFRESH_RATE_UNKNOWN)
}
FullScreenTest test = new FullScreenTest()
test run(displayMode)
}
private static final long DEMO_TIME =
public void run(DisplayMode displayMode) {
setBackground(Color blue)
setForeground(Color white)
setFont(new Font( Dialog Font PLAIN ))
SimpleScreenManager screen = new SimpleScreenManager()
try {
screen setFullScreen(displayMode this)
try {
Thread sleep(DEMO_TIME)
}
catch (InterruptedException ex) { }
}
finally {
screen restoreScreen()
}
}
public void paint(Graphics g) {
g drawString( Hello World! )
}
}
Java代码
import java awt DisplayMode
import java awt GraphicsDevice
import java awt GraphicsEnvironment
import java awt Window
import javax swing JFrame
/**
* The SimpleScreenManager class manages initializing and displaying full screen
* graphics modes
*/
public class SimpleScreenManager {
private GraphicsDevice device
/**
* Creates a new SimpleScreenManager object
*/
public SimpleScreenManager() {
GraphicsEnvironment environment = GraphicsEnvironment
getLocalGraphicsEnvironment()
device = environment getDefaultScreenDevice()
}
/**
* Enters full screen mode and changes the display mode
*/
public void setFullScreen(DisplayMode displayMode JFrame window) {
window setUndecorated(true)
window setResizable(false)
device setFullScreenWindow(window)
if (displayMode != null &&device isDisplayChangeSupported()) {
try {
device setDisplayMode(displayMode)
} catch (IllegalArgumentException ex) {
// ignore illegal mode for this device
}
}
}
/**
* Returns the window currently used in full screen mode
*/
public Window getFullScreenWindow() {
return device getFullScreenWindow()
}
/**
* Restores the screen s display mode
*/
public void restoreScreen() {
Window window = device getFullScreenWindow()
if (window != null) {
window dispose()
}
device setFullScreenWindow(null)
}
lishixinzhi/Article/program/Java/hx/201311/26128
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)