Java实现全屏的四种方式(四个例子)

Posted on 2016-05-10 20:41  空余恨  阅读(2843)  评论(0编辑  收藏  举报

FullScreenDemo.java:

Java代码  收藏代码
  1. package FullScreenDemo;  
  2.   
  3. import java.awt.*;  
  4.   
  5. import javax.swing.*;  
  6.   
  7. /** 
  8.  * @author http://xp9802.iteye.com/ 
  9.  * 2011-11-19下午04:40:38 
  10.  */  
  11. public class FullScreenDemo {  
  12.   
  13.     public static void main(String[] args) {  
  14.         final JFrame jframe = new JFrame();  
  15.         JButton fullsButton = new JButton("全屏显示");  
  16.         JButton exitButton = new JButton("退出");  
  17.         exitButton.addActionListener(new java.awt.event.ActionListener() {  
  18.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  19.                 System.exit(1);  
  20.             }  
  21.         });  
  22.         fullsButton.addActionListener(new java.awt.event.ActionListener() {  
  23.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  24.                 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  25.                 //通过调用GraphicsEnvironment的getDefaultScreenDevice方法获得当前的屏幕设备了  
  26.                 GraphicsDevice gd = ge.getDefaultScreenDevice();  
  27.                 // 全屏设置  
  28.                 gd.setFullScreenWindow(jframe);  
  29.             }  
  30.         });  
  31.         jframe.add(fullsButton);  
  32.         jframe.add(exitButton);  
  33.         jframe.setLayout(new FlowLayout());  
  34.         jframe.setSize(400, 300);  
  35.         jframe.setVisible(true);  
  36.     }  
  37. }  

 

 

 

FullScreenDemo1.java:

 

 

Java代码  收藏代码
  1. package FullScreenDemo;  
  2.   
  3. import java.awt.FlowLayout;  
  4.   
  5. import javax.swing.JButton;  
  6. import javax.swing.JFrame;  
  7. import javax.swing.UIManager;  
  8.   
  9. /** 
  10.  * @author http://xp9802.iteye.com/ 
  11.  * 2011-11-19下午04:31:38 
  12.  */  
  13. public class FullScreenDemo1 {  
  14.   
  15.     public static void main(String[] args) {  
  16.         JFrame jframe = new JFrame();  
  17.         JButton exitButton = new JButton("退出");  
  18.         exitButton.addActionListener(new java.awt.event.ActionListener() {  
  19.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  20.                 System.exit(1);  
  21.             }  
  22.         });  
  23.         jframe.add(exitButton);  
  24.         jframe.setLayout(new FlowLayout());  
  25.         jframe.setUndecorated(false);  
  26.         jframe.getGraphicsConfiguration().getDevice()  
  27.                 .setFullScreenWindow(jframe);  
  28.         jframe.setVisible(true);  
  29.     }  
  30. }  

 

 

FullScreenDemo2.java:

 

Java代码  收藏代码
  1. package FullScreenDemo;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.Toolkit;  
  6.   
  7. import javax.swing.JButton;  
  8. import javax.swing.JFrame;  
  9.   
  10.   
  11. /** 
  12.  * @author http://xp9802.iteye.com/ 
  13.  * 2011-11-19下午04:32:38 
  14.  */  
  15. public class FullScreenDemo2 {  
  16.     public static void main(String[] args) {  
  17.         JFrame jframe = new JFrame();  
  18.         JButton exitButton = new JButton("退出");  
  19.         exitButton.addActionListener(new java.awt.event.ActionListener() {  
  20.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  21.                 System.exit(1);  
  22.             }  
  23.         });  
  24.         jframe.add(exitButton);  
  25.         jframe.setLayout(new FlowLayout());  
  26.         /** 
  27.          * true无边框 全屏显示 
  28.          * false有边框 全屏显示 
  29.          */  
  30.         jframe.setUndecorated(false);  
  31.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
  32.         jframe.setSize(d.width, d.height);  
  33.         jframe.setVisible(true);  
  34.     }  
  35. }  

 

 

 

FullScreenDemo3.java:

 

 

Java代码  收藏代码
  1. package FullScreenDemo;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.Rectangle;  
  6. import java.awt.Toolkit;  
  7.   
  8. import javax.swing.JButton;  
  9. import javax.swing.JFrame;  
  10.   
  11. /** 
  12.  * @author http://xp9802.iteye.com/ 
  13.  * 2011-11-19下午04:39:38 
  14.  */  
  15. public class FullScreenDemo3 {  
  16.   
  17.     public static void main(String[] args) {  
  18.         JFrame jframe = new JFrame();  
  19.         JButton exitButton = new JButton("退出");  
  20.         exitButton.addActionListener(new java.awt.event.ActionListener() {  
  21.   
  22.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  23.                 System.exit(1);  
  24.             }  
  25.         });  
  26.         jframe.add(exitButton);  
  27.         jframe.setLayout(new FlowLayout());  
  28.         /** 
  29.          * true无边框 全屏显示 
  30.          * false有边框 全屏显示 
  31.          */  
  32.         jframe.setUndecorated(false);  
  33.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  34.         Rectangle bounds = new Rectangle(screenSize);  
  35.         jframe.setBounds(bounds);  
  36.         jframe.setExtendedState(JFrame.MAXIMIZED_BOTH);  
  37.         jframe.setVisible(true);  
  38.     }  
  39. }  

Copyright © 2024 空余恨
Powered by .NET 8.0 on Kubernetes