【问题标题】:Fullscreensize & Frame in front of it全屏尺寸和它前面的框架
【发布时间】:2014-05-15 20:35:02
【问题描述】:

我(仍然)是 Java 的初学者,我创建了一个包含主框架的小软件。

需要覆盖我的软件后面的所有桌面,例如 Windows 98 安装屏幕:(我需要黑色和蓝色屏幕,覆盖所有任务栏等)。

为了做到这一点,我使用了全屏显示的 GraphicsDevice。这正是我所需要的:

public class Fond_noir extends JFrame {

    private boolean isFullScreen = false;
    private GraphicsDevice device;

    public Fond_noir(int etat) {


      GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
       this.device  = env.getDefaultScreenDevice();

        initFullScreen(etat);
    }

    private void initFullScreen(int etat) {
        isFullScreen = device.isFullScreenSupported();

        if (etat==0)
        {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        }
        if (etat==1)
        {
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        }
        setUndecorated(isFullScreen);
        setResizable(!isFullScreen);
        if (isFullScreen) {

            // Full-screen mode
            device.setFullScreenWindow(this);
            validate();
        } else {
            // Windowed mode
            this.setExtendedState(MAXIMIZED_BOTH);
            this.setVisible(true);
        }
    }


}

然后,我在其他地方的 main 中调用这个方法,(这没有问题):

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {


            new Fond_noir(0);
            Choix_Langue inst = new Choix_Langue(); // main frame
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);

        } }  );  }

但问题是,我的主框架无法显示,它隐藏在我的全屏后面。我想要相反的! 或者当我单击任务栏中的主框架时(使用键盘的窗口键之后..)我只能看到我的主框架,并且框架没有显示全屏

=> 有没有办法同时显示我的框架和我的 GraphicsDevice ?在它们之间使用“优先级”..?

感谢阅读!

【问题讨论】:

  • 嗯......仍然会有alt-tab。我猜你将无法完全屏蔽其他应用程序
  • 我同意,但我的问题是和windows 98的安装画面完全一样
  • 您可以编写代码来“计算”屏幕的“宽度和高度”,并且可以使用这些代码来创建背景 JFrame。在加载时,您打开第二个 JFrame 或 JDialog。你试过吗?如果它保持隐藏状态,则需要将焦点设置为较小的屏幕
  • 再次感谢您的回答!实际上,我试过这个,但我不能用 JFrame 隐藏任务栏,我真的需要覆盖整个屏幕。如果有办法用 JFrame 创建全屏,我会做你的技术
  • 这个帖子可能会帮助你:stackoverflow.com/questions/19435048/…

标签: java swing fullscreen


【解决方案1】:

使用这个:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setAlwaysOnTop(true)

Undecorated 将删除标题栏。也不是试图分别显示两个框架。把小的加到大的。

bigFrame.add(smallFrame);
bigFrame.setVisible(true);

证明它有效的例子:

【讨论】:

  • @XxRoboticaXx 编辑了一些东西。
  • @MadProgrammer 那么任务栏是什么意思呢?不过在这里工作。
  • 也感谢您的回答。我试过你刚才说的,但似乎不喜欢这一行:bigFrame.add(smallFrame); “线程“主”java.lang.IllegalArgumentException 中的异常:向容器添加窗口”
  • @XxRoboticaXx JFrame extends Frame 和 Frame extends Window 这使您的组件(JFrame)实例成为窗口,这就是您将窗口添加到容器异常的原因。
  • @crashystar 我的立场是正确的,它似乎覆盖了整个屏幕
【解决方案2】:

我不确定您是否需要进入全屏独占模式,例如,您可以调整无边框框架的大小以适应默认屏幕尺寸并使其始终位于顶部以帮助它覆盖系统中的所有其他窗口然后只需使用JDialog 作为与用户合作的主要界面,例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class FullScreenBackground {

    public static void main(String[] args) {
        new FullScreenBackground();
    }

    public FullScreenBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

                JFrame frame = new JFrame("Testing");
                frame.setAlwaysOnTop(true);
                frame.setUndecorated(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new BackgroundPane());
                frame.setLocation(0, 0);
                frame.setSize(dim);
                frame.setVisible(true);

                JDialog dialog = new JDialog(frame);
                dialog.setContentPane(new InstallPane());
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        });
    }

    public class InstallPane extends JPanel {

        public InstallPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("<html><h1>Welcome to my fancy pancy background screen<h1></html>"), gbc);
        }

    }

    public class BackgroundPane extends JPanel {

        private BufferedImage bg;

        public BackgroundPane() {
        }

        @Override
        public void invalidate() {
            super.invalidate(); 
            bg = null;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg == null) {
                bg = new BufferedImage(1, getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = bg.createGraphics();
                LinearGradientPaint lgp = new LinearGradientPaint(
                                new Point(0, 0),
                                new Point(0, getHeight()),
                                new float[]{0f, 1f},
                                new Color[]{Color.BLACK, Color.BLUE}
                );
                g2d.setPaint(lgp);
                g2d.fillRect(0, 0, 1, getHeight());
            }
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

    }

}

更新

如果更改所有“框架”并不难,您可以考虑对上述示例进行以下更改...

JFrame frame = new JFrame("Testing");
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BackgroundPane());
frame.setLocation(0, 0);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setSize(dim);
// This will stop the background window from become focused,
// potentially hiding the other windows
frame.setFocusableWindowState(false);
frame.setFocusable(false);
frame.setVisible(true);

JFrame dialog = new JFrame();
// Will need to add this to each frame...
dialog.setAlwaysOnTop(true);
dialog.setContentPane(new InstallPane());
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);

您可能面临的另一个问题是alwaysOnTop 依赖于平台,这意味着它在不同平台上的行为可能不同。

extends JFrame 更改为extends JDialog 确实会是一个更简单、更稳定的更改...

【讨论】:

  • 和peeskillet一样的问题。它工作正常,但我的软件快完成了,我无法更改框架的所有内容,并且使用 Jdialog.. 我的老板一周前刚刚告诉我,我必须覆盖全屏!如果我早点知道,我会做你的回答..
  • @crashystar 而不是使用全屏独占模式device.setFullScreenWindow(this); 我只是使用了一个非常大的无边框框架,它设置为始终位于顶部。全屏独占模式将阻止所有其他窗口出现在此窗口上方,并且还有一些与处理输入事件的方式有关的问题......
  • @XxRoboticaXx 您应该考虑使用基于CardLayoutJPanel,那么顶级容器将变得无关紧要;)
  • 感谢 mp 的信息。
  • @XxRoboticaXx 检查更新以了解使用JFrames 而不是JDialogs 的可能(混乱)解决方案...
【解决方案3】:

我找到了你所有答案的解决方案。

我没有使用另一个框架,而是使用 JWindow 作为背景:

public class Fond_noir extends JWindow{
    Panel panel = new Panel();


    public Fond_noir(int etat) {

       if (etat==0)

           {
           setSize(2300,4000);
           setLocationRelativeTo(null);
           setVisible(true);

           }

       if (etat==1)

           {
             dispose();
            }

        panel.setBackground(Color.black);
        add(panel);
        }

class Panel extends JPanel{

        public void paintComponent(Graphics g){
            super.paintComponent(g);

        }
    }
}

然后,当我试图将我的主框架的“扩展 JFrame”更改为“扩展 JDialog”时,它让我删除了代码中这个可怕的行:this.setState(Frame.ICONIFIED); !!!! 它解释了为什么我必须一直寻找我的图标..所以我保留了我的 JFrame.. 所以现在它同时打开了一个背景窗口和框架:)

谢谢大家!下次我不会用那么多框架了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多