Java 全屏尺寸&;它前面的框架

Java 全屏尺寸&;它前面的框架,java,swing,fullscreen,Java,Swing,Fullscreen,我(仍然)是Java的初学者,我创建了一个包含主框架的小软件 我需要覆盖我软件后面的所有桌面,如windows 98安装屏幕:(我需要黑色和蓝色屏幕,覆盖所有任务栏等) 为了做到这一点,我使用了全屏显示的GraphicsDevice。这正是我所需要的: 然后,我在其他地方的main中调用此方法(这没有问题): 但问题是,我的主框架无法显示,而且它隐藏在我的全屏后面。。我想要相反的! 或者,当我在任务栏中单击我的主框架时(使用键盘ofc..的窗口键后),我只能看到我的主框架,并且全屏不会与框架一

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

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

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

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

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

=>是否有办法同时显示我的框架和图形设备?在他们之间使用“优先级”

谢谢你的阅读

使用以下命令:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setAlwaysOnTop(true)
未装饰的将删除标题栏。也不是试图分别显示两个帧。把小的加上大的

bigFrame.add(smallFrame);
bigFrame.setVisible(true);
显示其工作原理的示例:


我不确定您是否需要进入全屏独占模式,例如,您可以调整无边框框架的大小以适应默认屏幕大小,并使其始终位于顶部,以帮助它覆盖系统中的所有其他窗口,然后只需使用
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
依赖于平台,这意味着它在不同平台上的行为可能不同


extensedjframe
更改为
extensedjdialog
确实会是一个更简单、更稳定的更改…

我找到了一个解决方案

我没有使用另一帧,而是使用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);

        }
    }
}
然后,在尝试将我的主框架的“extends JFrame”更改为“extends JDialog”时,它让我删除了代码中的这一可怕的行:this.setState(frame.ICONIFIED)!!!! 这就解释了为什么我要一直寻找我的偶像。。所以我保留了我的JFrame。。 因此,现在它会同时打开一个背景窗口和帧:)


谢谢大家!下次我就不用那么多相框了。

好吧。。。仍然会有alt选项卡。我想我同意你的看法,你无法完全屏蔽其他应用程序,但我的问题是要做与windows 98安装屏幕完全相同的事情。你可以编写代码“计算”屏幕的“宽度和高度”,你可以使用这些来创建背景框架。加载时,打开第二个JFrame或JDialog。你试过了吗?如果它一直隐藏,你需要将焦点设置到较小的屏幕上再次感谢你的回答!事实上,我试过了,但我不能用JFrame隐藏任务栏,我真的需要覆盖所有屏幕。。如果有办法用JFrame创建全屏,我会用你的技术。这个线程可能会帮你:@xxrobotaxx编辑了一些东西。@MadProgrammer他用任务栏是什么意思?不过在这里工作。也谢谢你的回答。我尝试了你刚才所说的,但它似乎不喜欢这一行:bigFrame.add(smallFrame);“线程“main”java.lang.IllegalArgumentException中的异常:将窗口添加到容器“@xxRobotaxx JFrame扩展框架,框架扩展窗口使组件(JFrame)成为窗口的实例,这就是为什么将窗口添加到容器异常的原因。@crashystar I已更正,它似乎覆盖了整个屏幕,这和peeskillet的问题是一样的。它工作得很好,但我的软件几乎完成了,我无法更改框架的所有内容,并且使用Jdialog。。我的老板一周前刚刚告诉我,我必须全屏报道!如果我早知道,我会回答你的问题。@crashystar,而不是使用全屏独占模式
device.setFullScreenWindow(这个)我只是使用了一个非常大的、无边界的框架,该框架设置为始终位于顶部。全屏独占模式将阻止所有其他窗口出现在此窗口上方,并且还存在一些与输入事件处理方式相关的问题…@xxrobotaxx您应该考虑使用基于
JPanel
CardLayout
,那么顶级容器将变得不相关;)感谢您提供的信息。@xxrobotaxx检查更新,以获得使用
JFrame
s而不是
JDialog
s的可能(混乱)解决方案。。。
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);
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);

        }
    }
}