【Gloomyfish】Java Swing 实现实际大小到全屏切换

简介: 【Gloomyfish】Java Swing 实现实际大小到全屏切换

Summary:


just provide any JPanel which you want to make it diaplay as full screen mode, it's very helpful for you


to use this utility class.

package com.gloomyfish.smtp.client;
 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
 
/**
 * @author gloomy fish
 * @date 2011-03-03
 */
public class FullScreenViewer {
  private Dimension size;
  private JPanel sourcePanel;
  private JPanel buttonsPanel;
  private JButton exitBtn;
 
  /**
   * 
   display any JComponent as full screen, need to pass in root Container
   * 
   * invoke swing graphic utility tool, it's a bug!! when double buffer image
   * JFreechart * @param JFrame rootComponent
   */
  public void showFullScreenOne(Frame rootComponent) {
    final JDialog dialog = new JDialog(rootComponent, true);
    dialog.getContentPane().setLayout(new BorderLayout());
    final Container parent = sourcePanel.getParent();
    dialog.getContentPane().add(sourcePanel, BorderLayout.CENTER);
    dialog.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
    dialog.setUndecorated(true);
    exitBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
      }
    });
    GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    if (gd.isFullScreenSupported()) {
      try {
        gd.setFullScreenWindow(dialog);
      } finally {
        gd.setFullScreenWindow(null);
        dialog.dispose();
        if (parent != null) {
          sourcePanel.setSize(size);
          parent.add(sourcePanel);
        }
      }
    }
  }
 
  /**
   * * it's a bug!! when double buffer image JFreechart
   * 
   * @param owner
   */
  public void showFullScreenTwo(Frame owner) {
    final JDialog dialog = new JDialog(owner, true);
    dialog.getContentPane().setLayout(new BorderLayout());
    final Container parent = sourcePanel.getParent();
    dialog.getContentPane().add(sourcePanel, BorderLayout.CENTER);
    dialog.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
    exitBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
        dialog.dispose();
        if (parent != null) {
          sourcePanel.setSize(size);
          parent.add(sourcePanel);
        }
      }
    });
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());
    dialog.setUndecorated(true);
    dialog.setSize(xSize, ySize);
    dialog.setResizable(false);
    dialog.setVisible(true);
    dialog.toFront();
  }
 
  public FullScreenViewer(JPanel sourcePanel) {
    this.sourcePanel = sourcePanel;
    initComponents();
  }
 
  private void initComponents() {
    exitBtn = new JButton("Exit");
    buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    buttonsPanel.add(exitBtn);
    size = new Dimension(sourcePanel.getSize().width,
        sourcePanel.getSize().height);
  }
}

PS:

There is an potential problem in above code implementation, just contact me

if you found this issue as well...

相关文章
|
3月前
|
安全 Java API
|
3月前
|
SQL Java 数据库连接
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
130 0
|
3月前
|
存储 数据可视化 Java
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
|
5月前
|
Java 数据安全/隐私保护
利用Java图形化界面组件Swing写一个简易的登录界面
利用Java图形化界面组件Swing写一个简易的登录界面
61 1
|
5月前
|
Java Windows
基于java Swing编写扫雷游戏设计实现(已调试)
基于java Swing编写扫雷游戏设计实现(已调试)
62 1
|
5月前
|
Java
Java Swing模拟水波纹扩散效果动画
Java Swing模拟水波纹扩散效果动画
53 6
|
5月前
|
Java
JAVA Swing自定义JScorllPanel
JAVA Swing自定义JScorllPanel
35 1
|
5月前
|
前端开发 Java UED
Java中的图形用户界面编程:Swing与JavaFX的比较与应用
Java中的图形用户界面编程:Swing与JavaFX的比较与应用
|
5月前
|
存储 设计模式 前端开发
[笔记] 疯狂JAVA讲义(第3版)第12章 Swing编程
[笔记] 疯狂JAVA讲义(第3版)第12章 Swing编程
|
5月前
|
Java
Java Swing 层次面板(JLayeredPanel) Demo
Java Swing 层次面板(JLayeredPanel) Demo
36 0