Java JFrame实现全屏的四种方式

JFrame实现全屏的四种方式,方式一:

 
 
  1. import java.awt.*; 
  2.  
  3. import javax.swing.*; 
  4.  
  5. public class FullScreenDemo1 { 
  6.  
  7.     public static void main(String[] args) { 
  8.         final JFrame jframe = new JFrame(); 
  9.         JButton fullsButton = new JButton("全屏显示"); 
  10.         JButton exitButton = new JButton("退出"); 
  11.         exitButton.addActionListener(new java.awt.event.ActionListener() { 
  12.             public void actionPerformed(java.awt.event.ActionEvent evt) { 
  13.                 System.exit(1); 
  14.             } 
  15.         }); 
  16.         fullsButton.addActionListener(new java.awt.event.ActionListener() { 
  17.             public void actionPerformed(java.awt.event.ActionEvent evt) { 
  18.                 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
  19.                 //通过调用GraphicsEnvironment的getDefaultScreenDevice方法获得当前的屏幕设备了 
  20.                 GraphicsDevice gd = ge.getDefaultScreenDevice(); 
  21.                 // 全屏设置 
  22.                 gd.setFullScreenWindow(jframe); 
  23.             } 
  24.         }); 
  25.         jframe.add(fullsButton); 
  26.         jframe.add(exitButton); 
  27.         jframe.setLayout(new FlowLayout()); 
  28.         jframe.setSize(400300); 
  29.         jframe.setVisible(true); 
  30.     } 

方式二:

 
 
  1. import java.awt.FlowLayout; 
  2.  
  3. import javax.swing.JButton; 
  4. import javax.swing.JFrame; 
  5. import javax.swing.UIManager; 
  6.  
  7. public class FullScreenDemo2 { 
  8.  
  9.     public static void main(String[] args) { 
  10.         JFrame jframe = new JFrame(); 
  11.         JButton exitButton = new JButton("退出"); 
  12.         exitButton.addActionListener(new java.awt.event.ActionListener() { 
  13.             public void actionPerformed(java.awt.event.ActionEvent evt) { 
  14.                 System.exit(1); 
  15.             } 
  16.         }); 
  17.         jframe.add(exitButton); 
  18.         jframe.setLayout(new FlowLayout()); 
  19.         jframe.setUndecorated(false); 
  20.         jframe.getGraphicsConfiguration().getDevice() 
  21.                 .setFullScreenWindow(jframe); 
  22.         jframe.setVisible(true); 
  23.     } 

方式三:

 
 
  1. import java.awt.Dimension; 
  2. import java.awt.FlowLayout; 
  3. import java.awt.Toolkit; 
  4.  
  5. import javax.swing.JButton; 
  6. import javax.swing.JFrame; 
  7.  
  8. public class FullScreenDemo3 { 
  9.     public static void main(String[] args) { 
  10.         JFrame jframe = new JFrame(); 
  11.         JButton exitButton = new JButton("退出"); 
  12.         exitButton.addActionListener(new java.awt.event.ActionListener() { 
  13.             public void actionPerformed(java.awt.event.ActionEvent evt) { 
  14.                 System.exit(1); 
  15.             } 
  16.         }); 
  17.         jframe.add(exitButton); 
  18.         jframe.setLayout(new FlowLayout()); 
  19.         /** 
  20.          * true无边框 全屏显示 
  21.          * false有边框 全屏显示 
  22.          */ 
  23.         jframe.setUndecorated(false); 
  24.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
  25.         jframe.setSize(d.width, d.height); 
  26.         jframe.setVisible(true); 
  27.     } 

方式四:

 
 
  1. import java.awt.Dimension; 
  2. import java.awt.FlowLayout; 
  3. import java.awt.Rectangle; 
  4. import java.awt.Toolkit; 
  5.  
  6. import javax.swing.JButton; 
  7. import javax.swing.JFrame; 
  8. public class FullScreenDemo3 { 
  9.  
  10.     public static void main(String[] args) { 
  11.         JFrame jframe = new JFrame(); 
  12.         JButton exitButton = new JButton("退出"); 
  13.         exitButton.addActionListener(new java.awt.event.ActionListener() { 
  14.  
  15.             public void actionPerformed(java.awt.event.ActionEvent evt) { 
  16.                 System.exit(1); 
  17.             } 
  18.         }); 
  19.         jframe.add(exitButton); 
  20.         jframe.setLayout(new FlowLayout()); 
  21.         /** 
  22.          * true无边框 全屏显示 
  23.          * false有边框 全屏显示 
  24.          */ 
  25.         jframe.setUndecorated(false); 
  26.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
  27.         Rectangle bounds = new Rectangle(screenSize); 
  28.         jframe.setBounds(bounds); 
  29.         jframe.setExtendedState(JFrame.MAXIMIZED_BOTH); 
  30.         jframe.setVisible(true); 
  31.     } 
  32. }
  • 12
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值