简体   繁体   中英

How do I check if the JFrame is fullscreen?

I'm trying to code an app using JFrame . But I have encountered an issue when trying to check if the window is full screen or not. I want my button to be in the same position when it's full and half screen. Here's my code.

package App.Gui;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import App.Gui.Event.ExitEvent;

public class WindowSettings {
    private final static ImageIcon imageIcon = new ImageIcon("C:\\Users\\zeesh\\FirstApp\\src\\image\\Untitled.png");
 public static void setWindow() {

     JFrame frame = new JFrame("Debug Configurations");
     JPanel panel = new JPanel();
     frame.getContentPane();
     JButton button = new JButton("X");
     Dimension size = button.getPreferredSize();
     button.setBounds(1125, 20, 50, 50);
     button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ExitEvent.exit();
            }
     });
     panel.setLayout(null);
     panel.add(button);
     panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     frame.add(panel);
     frame.setIconImage(imageIcon.getImage());
     frame.setSize(1200, 750);
     frame.setVisible(true);
 }
}

Make use of appropriate layouts, for example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JButton button = new SquareButton("X");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Hello");
                }
            });

            JPanel topPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.EAST;
            gbc.weightx = 1;
            gbc.insets = new Insets(20, 50, 20, 50);
            topPane.add(button, gbc);

            add(topPane, BorderLayout.NORTH);

            JPanel contentPane = new JPanel(new GridBagLayout());
            contentPane.add(new JLabel("Stuff goes here"));
            add(contentPane);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1200, 750);
        }
    }

    class SquareButton extends JButton {
        SquareButton(String s) {
            super(s);
        }

        @Override
        public Dimension getPreferredSize() {
            FontMetrics fm = getFontMetrics(getFont());
            int stringWidth = fm.stringWidth(getText());
            int stringHeight = fm.getHeight();

            int size = Math.max(stringHeight, stringWidth);

            return new Dimension(size + 22, size + 22);
        }
    }
}

See Laying Out Components Within a Container for more details

But, wait, you want to overlap the button over the top of the content? Well, that's not that hard...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton button = new SquareButton("X");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Hello");
                }
            });

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTHEAST;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.insets = new Insets(20, 50, 20, 50);
            add(button, gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;

            JPanel contentPane = new JPanel(new GridBagLayout());
            contentPane.setBackground(Color.MAGENTA);
            contentPane.add(new JLabel("Stuff goes here"));
            add(contentPane, gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1200, 750);
        }
    }

    class SquareButton extends JButton {

        SquareButton(String s) {
            super(s);
        }

        @Override
        public Dimension getPreferredSize() {
            FontMetrics fm = getFontMetrics(getFont());
            int stringWidth = fm.stringWidth(getText());
            int stringHeight = fm.getHeight();

            int size = Math.max(stringHeight, stringWidth);

            return new Dimension(size + 22, size + 22);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM