欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swing JFrame全屏

程序员文章站 2022-07-15 09:27:52
...
import java.awt.DisplayMode;
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FullScreenDemo {
	public static void main(String[] args) {
		JFrame window = new JFrame();
		window.setUndecorated(true);
		window.setResizable(false);
		window.setLayout(new FlowLayout());
		JButton button = new JButton("close window");
		button.setMargin(new Insets(0, 0, 0, 0));
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		window.add(button);
		
		DisplayMode displayMode = new DisplayMode(800,600,16,75);//分辨率800*600,16色深,75HZ
		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice device = environment.getDefaultScreenDevice();
		//use the Jframe as the full screen window
		device.setFullScreenWindow(window);
		//如果要退出全屏
		//device.setFullScreenWindow(null);
		//change the displaymode
		if(displayMode != null && device.isDisplayChangeSupported()){
			try {
				device.setDisplayMode(displayMode);				
			} catch (Exception e) {
			}
		}
		
	}
}

 

相关标签: Swing Java