带有双显示器的Java全屏jframe

时间:2012-02-07 15:48:09

标签: java windows jframe fullscreen multiple-monitors

这是我的问题,我有2个Jframe,一个用于控制,一个用于向公众显示内容。 我需要全屏显示框以避免公众看到它不需要的东西,我需要控制框可以在另一个屏幕上使用来控制显示框。

这是我的问题,今天,我在Ubuntu和双屏幕模式下工作没有问题,但是,在Windows 7 buisness 64(不要尝试其他版本)显示框架实际上是全屏,但是当我单击显示框外的任何位置(控制框包括),显示框自动最小化。我是客人,这是一个Windows最小化最大化行为的问题,但我真的很想让它工作。 我知道一个非常脏的解决方法,包括使显示框架与我的屏幕大小相同,并将显示框架定位在良好的坐标上。

以下是我所做的示例代码 要将显示框全屏显示,请对焦控制框并按下F11键。或使用菜单(法语)

GameFrame.java 相当于显示框架

package ihm;
import java.awt.Graphics;  
import java.awt.Image;  
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class GameFrame extends javax.swing.JFrame {

    public GameFrame() {
        initComponents();
    }


    @SuppressWarnings("unchecked")              
    private void initComponents() {

        mainPanel = new javax.swing.JTabbedPane();
        gamePanel = new javax.swing.JPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                if(backgroundImage != null){
                    g.drawImage(backgroundImage, 0, 0, this.getWidth(),this.getHeight(),null);
                }
            }
        };
        votePanel = new javax.swing.JPanel();
        countdownPanel = new javax.swing.JPanel();
        bienvenuePanel = new javax.swing.JPanel();
        adsPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ImproAffichage");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(255, 179, 0));
        setForeground(java.awt.Color.white);
        setUndecorated(true);
        getContentPane().setLayout(new java.awt.GridLayout(1, 1));

        mainPanel.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);

        gamePanel.setBackground(new java.awt.Color(255, 168, 0));
        gamePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(40, 40, 40, 40));
        gamePanel.setMinimumSize(new java.awt.Dimension(200, 200));
        gamePanel.setLayout(new java.awt.GridLayout(2, 2, 70, 70));
        mainPanel.addTab("score", gamePanel);

        votePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        votePanel.setLayout(new java.awt.GridLayout(1, 0));
        mainPanel.addTab("vote", votePanel);

        countdownPanel.setLayout(new java.awt.GridLayout(1, 0));
        mainPanel.addTab("decompte", countdownPanel);

        bienvenuePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        bienvenuePanel.setLayout(new java.awt.GridLayout(1, 0));
        mainPanel.addTab("Bienvenue", bienvenuePanel);

        adsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        adsPanel.setLayout(new java.awt.GridLayout(1, 0));
        mainPanel.addTab("publicité", adsPanel);

        getContentPane().add(mainPanel);
        mainPanel.setUI(new BasicTabbedPaneUI() {

            protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {
                return 0;
            }
        });

        pack();
     }                      


    public static void main(String args[]) {
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GameFrame().setVisible(true);
            }
        });
    }
    private static GameFrame instance;
    private Image backgroundImage;
    private Image decompteImage;
    private Image bienvenueImage;
    // Variables declaration - do not modify                     
    private javax.swing.JPanel adsPanel;
    private javax.swing.JPanel bienvenuePanel;
    private javax.swing.JPanel countdownPanel;
    private javax.swing.JPanel gamePanel;
    private javax.swing.JTabbedPane mainPanel;
    private javax.swing.JPanel votePanel;
    // End of variables declaration                   


    public static GameFrame getInstance() {
        if (instance == null) {
            instance = new GameFrame();
        }
        return instance;
    }

    public void refresh() {
        this.validate();
        this.repaint();
        this.pack();
    }
}  



ControlFrame.java 相当于控制框架

package ihm;
import java.awt.Dimension;
import java.awt.GraphicsDevice; 
public class ControlFrame extends javax.swing.JFrame {

    public static ControlFrame getInstance() {
        if (instance == null) {
            instance = new ControlFrame();
        }
        return instance;
    }

    private ControlFrame() {
        this.gameFrame = GameFrame.getInstance();
        this.gameFrame.setBounds(100, 100, 400, 300);
        this.gameFrame.pack();
        this.gameFrame.setVisible(true);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {
        transitionPanel = new javax.swing.JPanel();
        jbBienvenue = new javax.swing.JButton();
        jbScore = new javax.swing.JButton();
        jbVote = new javax.swing.JButton();
        jbDecompte = new javax.swing.JButton();
        jbPublicite = new javax.swing.JButton();
        jpPlayerControl = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mainMenu = new javax.swing.JMenu();
        jmScore = new javax.swing.JMenu();
        jmiAddTeam = new javax.swing.JMenuItem();
        jmiFullscreen = new javax.swing.JMenuItem();
        jmiBackground = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jmiPointPath = new javax.swing.JMenuItem();
        jmOtherImage = new javax.swing.JMenu();
        jmiVote = new javax.swing.JMenuItem();
        jmiDecompte = new javax.swing.JMenuItem();
        jmiBienvenue = new javax.swing.JMenuItem();
        jmiAds = new javax.swing.JMenuItem();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ImproContro");
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(0, 0, 200, 300));
        setForeground(java.awt.Color.white);
        setPreferredSize(new java.awt.Dimension(400, 500));
        setResizable(false);
        getContentPane().setLayout(new java.awt.BorderLayout(25, 25));
        transitionPanel.setLayout(new java.awt.GridLayout(1, 6));
        jbBienvenue.setText("Bienvenue");
        transitionPanel.add(jbBienvenue);
        jbScore.setText("Score");
        transitionPanel.add(jbScore);
        jbVote.setText("Aux votes");
        transitionPanel.add(jbVote);
        jbDecompte.setText("Decompte");
        transitionPanel.add(jbDecompte);
        jbPublicite.setText("Publicité");
        transitionPanel.add(jbPublicite);
        getContentPane().add(transitionPanel, java.awt.BorderLayout.PAGE_START);
        jpPlayerControl.setBackground(new java.awt.Color(255, 220, 0));
        jpPlayerControl.setLayout(new java.awt.GridLayout(2, 2, 10, 10));
        getContentPane().add(jpPlayerControl, java.awt.BorderLayout.CENTER);
        mainMenu.setText("File");
        jMenuBar1.add(mainMenu);
        jmScore.setText("Score");
        jmiAddTeam.setText("ajouter equipe");
        jmScore.add(jmiAddTeam);
        jmiFullscreen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
        jmiFullscreen.setText("Plein Ecran");
        jmScore.add(jmiFullscreen);
        jmiBackground.setText("image \"fond score\"");
        jmScore.add(jmiBackground);
        jMenuItem1.setText("couleur \"fond score\"");
        jmScore.add(jMenuItem1);
        jmiPointPath.setText("Icone de point");
        jmScore.add(jmiPointPath);
        jMenuBar1.add(jmScore);
        jmOtherImage.setText("Autre images");
        jmiVote.setText("image \"aux votes\"");
        jmOtherImage.add(jmiVote);
        jmiDecompte.setText("image \"fond Decompte\"");
        jmOtherImage.add(jmiDecompte);
        jmiBienvenue.setText("image \"Bienvenue\"");
        jmOtherImage.add(jmiBienvenue);
        jmiAds.setText("Ajouter image annonce");
        jmOtherImage.add(jmiAds);
        jMenuBar1.add(jmOtherImage);
        setJMenuBar(jMenuBar1);
        jmiFullscreen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));

        jmiFullscreen.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiFullscreenActionPerformed(evt);
            }
        });
        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ControlFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        /*
         * * Create and display the form
         */        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ControlFrame.getInstance().setVisible(true);
            }
        });
    }

    private void jmiFullscreenActionPerformed(java.awt.event.ActionEvent evt) {
        GraphicsDevice gd = this.gameFrame.getGraphicsConfiguration().getDevice();

        if (gd.getFullScreenWindow() != null) {
            gd.setFullScreenWindow(null);
            this.gameFrame.setResizable(true);
            this.gameFrame.setPreferredSize(new Dimension(400, 300));
        } else {
            this.gameFrame.setResizable(false);
            this.gameFrame.setPreferredSize(new Dimension(gd.getDefaultConfiguration().getBounds().getSize()));
            this.gameFrame.enableInputMethods(false);
            gd.setFullScreenWindow(this.gameFrame);
        }
        refresh();
    }
    private static ControlFrame instance;
    private GameFrame gameFrame;
    // Variables declaration - do not modify
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JButton jbBienvenue;
    private javax.swing.JButton jbDecompte;
    private javax.swing.JButton jbPublicite;
    private javax.swing.JButton jbScore;
    private javax.swing.JButton jbVote;
    private javax.swing.JMenu jmOtherImage;
    private javax.swing.JMenu jmScore;
    private javax.swing.JMenuItem jmiAddTeam;
    private javax.swing.JMenuItem jmiAds;
    private javax.swing.JMenuItem jmiBackground;
    private javax.swing.JMenuItem jmiBienvenue;
    private javax.swing.JMenuItem jmiDecompte;
    private javax.swing.JMenuItem jmiFullscreen;
    private javax.swing.JMenuItem jmiPointPath;
    private javax.swing.JMenuItem jmiVote;
    private javax.swing.JPanel jpPlayerControl;
    private javax.swing.JMenu mainMenu;
    private javax.swing.JPanel transitionPanel;
    // End of variables declaration

    public void refresh() {
        System.out.println("refresh()");
        this.gameFrame.refresh();
        this.validate();
        this.repaint();
    }
}


EDIT1:
我尝试全屏显示两个窗口,但是当调用一个filechooser时,由于全屏窗口始终处于最顶端的行为,焦点会丢失。

编辑2:
我还试图将一个监听器放在图标化的显示jframe上,但是如果我尝试将状态更改为NORMAL,则显示jframe请求焦点,而我的第二帧无法使用,因为它失去了焦点

编辑3:
如果显示框未完全屏蔽,则单击控制框时不会将其最小化。

2 个答案:

答案 0 :(得分:2)

所以这里我自己的问题,我最终使用的解决方法,我认为唯一不那么难看的解决方法,我可以找到使用此代码

private void jmiFullscreenActionPerformed(java.awt.event.ActionEvent evt) {                                              
     GraphicsDevice gd = this.gameFrame.getGraphicsConfiguration().getDevice();

    if (!this.gameFrame.isResizable() ) {
        //gd.setFullScreenWindow(null);
        this.gameFrame.setResizable(true);
        this.gameFrame.setPreferredSize(new Dimension(400, 300));
    } else {
        this.gameFrame.setResizable(false);
        this.gameFrame.setPreferredSize(new Dimension(gd.getDefaultConfiguration().getBounds().getSize()));
        this.gameFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    refresh();
}           

而不是我原来的例子

private void jmiFullscreenActionPerformed(java.awt.event.ActionEvent evt) {
    GraphicsDevice gd = this.gameFrame.getGraphicsConfiguration().getDevice();

    if (gd.getFullScreenWindow() != null) {
        gd.setFullScreenWindow(null);
        this.gameFrame.setResizable(true);
        this.gameFrame.setPreferredSize(new Dimension(400, 300));
    } else {
        this.gameFrame.setResizable(false);
        this.gameFrame.setPreferredSize(new Dimension(gd.getDefaultConfiguration().getBounds().getSize()));
        this.gameFrame.enableInputMethods(false);
        gd.setFullScreenWindow(this.gameFrame);
    }
    refresh();
}

所以最后我没有使用java全屏功能,我只是使用我的jframe的一些属性使它看起来像一个全屏帧。

希望对你们有些帮助!! 祝你有愉快的一天

答案 1 :(得分:0)

this可能会帮助您

我更喜欢你使用的自定义方式:)我想出了这个解决方案 当我面临同样的问题,但问题在于Java的Fullscreen API。