سؤال

I am (still) a beginner in Java, and I created a little software which contains a main frame.

I need to cover all the Desktop behind my software such as a windows 98 installing screen : (I need the black and blue screen behing, covering all the task bar etc).

enter image description here

In order to do this, I used GraphicsDevice which goes full screen. It is exactly what I needed :

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);
        }
    }


}

Then, I call this method in a main somewhere else, (there's no problem with this) :

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);

        } }  );  }

But the problem is, that my main frame can't show-up, and it's hidden behind my fullscreen.. I'd like the opposite ! Or when I click on my main frame in my task bar (aften using the window key of my keyboard ofc..) I can only see my main frame, and the fullscreen is not showing-up with the frame

=> Is there a way to show both my frame and my GraphicsDevice ? Using "priorities" between them..?

Thanks for reading !

هل كانت مفيدة؟

المحلول 2

I'm not sure you need to go full screen exclusive mode, for example, you can size a border-less frame to fit the default screen size and make it always on top to help it cover all other windows in the system and then simply use a JDialog as the primary interface to work with the user, for example...

Desktop

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);
        }

    }

}

Updated

If changing all the "frames" is not hard, you could consider making the following changes to the above example...

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);

The other problem you might face is the fact that alwaysOnTop is platform dependent, meaning that it might behaviour differently on different platforms.

Changing extends JFrame to extends JDialog really would be a simpler and more stable change...

نصائح أخرى

Use this:

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

Undecorated will remove the titlebars. Also instead of trying to show both the frames seperately. Add the small one to the bigger one.

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

Example to show that it works:

example

I found a solution with all your anwers.

Instead of using another frame I used a JWindow for the background :

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);

        }
    }
}

Then while trying to change the "extends JFrame" of my main frame to "extends JDialog", it made me delete this horrible line in the code : this.setState(Frame.ICONIFIED); !!!! It explains why I had to look for my icon all the time.. So I kept my JFrame.. So now it opens a background window AND the frame at the same time :)

Thank you everyone ! Next time I won't use that much frames.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top