Java中JFrame实现全屏的四种方式

2019-04-29 15:44 阅读 151 次 评论 0 条
JFrame实现全屏的四种方式,方式一:

import java.awt.*; 
 
import javax.swing.*; 
 
public class FullScreenDemo1 { 
 
    public static void main(String[] args) { 
        final JFrame jframe = new JFrame(); 
        JButton fullsButton = new JButton("全屏显示"); 
        JButton exitButton = new JButton("退出"); 
        exitButton.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                System.exit(1); 
            } 
        }); 
        fullsButton.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
                //通过调用GraphicsEnvironment的getDefaultScreenDevice方法获得当前的屏幕设备了 
                GraphicsDevice gd = ge.getDefaultScreenDevice(); 
                // 全屏设置 
                gd.setFullScreenWindow(jframe); 
            } 
        }); 
        jframe.add(fullsButton); 
        jframe.add(exitButton); 
        jframe.setLayout(new FlowLayout()); 
        jframe.setSize(400, 300); 
        jframe.setVisible(true); 
    } 
} 
方式二:

import java.awt.FlowLayout; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.UIManager; 
 
public class FullScreenDemo2 { 
 
    public static void main(String[] args) { 
        JFrame jframe = new JFrame(); 
        JButton exitButton = new JButton("退出"); 
        exitButton.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                System.exit(1); 
            } 
        }); 
        jframe.add(exitButton); 
        jframe.setLayout(new FlowLayout()); 
        jframe.setUndecorated(false); 
        jframe.getGraphicsConfiguration().getDevice() 
                .setFullScreenWindow(jframe); 
        jframe.setVisible(true); 
    } 
} 
方式三:

import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Toolkit; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
 
public class FullScreenDemo3 { 
    public static void main(String[] args) { 
        JFrame jframe = new JFrame(); 
        JButton exitButton = new JButton("退出"); 
        exitButton.addActionListener(new java.awt.event.ActionListener() { 
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                System.exit(1); 
            } 
        }); 
        jframe.add(exitButton); 
        jframe.setLayout(new FlowLayout()); 
        /** 
         * true无边框 全屏显示 
         * false有边框 全屏显示 
         */ 
        jframe.setUndecorated(false); 
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
        jframe.setSize(d.width, d.height); 
        jframe.setVisible(true); 
    } 
} 
方式四:

import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
 
import javax.swing.JButton; 
import javax.swing.JFrame; 
public class FullScreenDemo3 { 
 
    public static void main(String[] args) { 
        JFrame jframe = new JFrame(); 
        JButton exitButton = new JButton("退出"); 
        exitButton.addActionListener(new java.awt.event.ActionListener() { 
 
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                System.exit(1); 
            } 
        }); 
        jframe.add(exitButton); 
        jframe.setLayout(new FlowLayout()); 
        /** 
         * true无边框 全屏显示 
         * false有边框 全屏显示 
         */ 
        jframe.setUndecorated(false); 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
        Rectangle bounds = new Rectangle(screenSize); 
        jframe.setBounds(bounds); 
        jframe.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        jframe.setVisible(true); 
    } 
}

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:Java中JFrame实现全屏的四种方式 | 猿笔记
分类:JAVA, 程序笔记 标签:,

发表评论


表情