我正在尝试编写一些代码以退出程序上的全屏模式并进入窗口模式。目前,这是我的代码:
package main2;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class main2 extends JFrame{
private Screen screen;
public static void main(String args[]){
DisplayMode dm = new DisplayMode(1900,1080,16, DisplayMode.REFRESH_RATE_UNKNOWN);
main2 m = new main2();
m.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.CYAN);
setForeground(Color.ORANGE);
setFont(new Font("Arial", Font.PLAIN, 24));
screen = new Screen();
try{
screen.setFullScreen(dm, this);
}catch(Exception e){}
getContentPane().addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
screen.restoreScreen();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
screen.restoreScreen();
}
});
}
public void paint(Graphics g){
if(g instanceof Graphics2D){
Graphics2D graphics2D = (Graphics2D)g;
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
super.paint(g);
g.drawString("some text", 200, 200);
g.setColor(Color.GREEN);
g.drawRect( 300,300,100,100);
}
private class Screen {
private GraphicsDevice vc;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
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 e){
System.out.println(e.getStackTrace());
}
}
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
JFrame frame = (JFrame)w;
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
frame.setSize(800, 600);
frame.pack();
frame.setVisible(true);
frame.setLocation(10,10);
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
}
}
程序退出全屏模式,但是没有显示JFrame窗口。任务栏上会出现一个java图标,但不会显示实际窗口。任何帮助将非常感激。提前谢谢。