简体   繁体   English

JFrame 最大化窗口

[英]JFrame Maximize window

I'm putting together a quick and dirty animation using swing.我正在使用 Swing 制作一个快速而肮脏的动画。 I would like the window to be maximized.我希望窗口最大化。 How can I do that?我怎样才能做到这一点?

Provided that you are extending JFrame:假设您要扩展 JFrame:

public void run() {
    MyFrame myFrame = new MyFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}

Something like this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);类似this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{
    public Test()
    {
        GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
        this.setMaximizedBounds(env.getMaximumWindowBounds());
        this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
    }

    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);

        Test t = new Test();
        t.setVisible(true);
    }
}

If you're using a JFrame, try this如果你使用的是 JFrame,试试这个

JFrame frame = new JFrame();
//...
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

i like this version:我喜欢这个版本:

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Test
{
    public static void main(String [] args)
    {
        final JFrame frame = new JFrame();
        final GraphicsConfiguration config = frame.getGraphicsConfiguration();

        final int left = Toolkit.getDefaultToolkit().getScreenInsets(config).left;
        final int right = Toolkit.getDefaultToolkit().getScreenInsets(config).right;
        final int top = Toolkit.getDefaultToolkit().getScreenInsets(config).top;
        final int bottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom;

        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final int width = screenSize.width - left - right;
        final int height = screenSize.height - top - bottom;

        frame.setResizable(false);
        frame.setSize(width,height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setVisible(true);
    }
}

The way to set JFrame to full-screen, is to set MAXIMIZED_BOTH option which stands for MAXIMIZED_VERT | MAXIMIZED_HORIZ将 JFrame 设置为全屏的方法是设置MAXIMIZED_BOTH选项,它代表MAXIMIZED_VERT | MAXIMIZED_HORIZ MAXIMIZED_VERT | MAXIMIZED_HORIZ , which respectively set the frame to maximize vertically and horizontally MAXIMIZED_VERT | MAXIMIZED_HORIZ ,分别设置frame垂直和水平最大化

package Example;
import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;
import javax.swing.JButton;

public class JFrameExample
{
    static JFrame frame;
    static GraphicsConfiguration gc;
    public static void main(String[] args)
    {
        frame = new JFrame(gc);
        frame.setTitle("Full Screen Example");
        frame.setExtendedState(MAXIMIZED_BOTH);

        JButton button = new JButton("exit");
        b.addActionListener(new ActionListener(){@Override
        public void actionPerformed(ActionEvent arg0){
            JFrameExample.frame.dispose();
            System.exit(0);
        }});

        frame.add(button);
        frame.setVisible(true);
    }
}

I've tried the solutions in this thread and the ones here , but simply calling setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH);我已经尝试过这个线程和这里的解决方案,但只是调用setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH); right after calling setVisible(true);在调用setVisible(true); apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen).显然不适用于我的环境(Windows 10,JDK 1.8,我的任务栏位于屏幕右侧)。 Doing it this way still leaves a tiny space on the left, right and bottom .这样做仍然会在左侧、右侧和底部留下一个很小的空间。

What did work for me however, is calling setExtendedState(... when the window is activated, like so:然而,对我setExtendedState(...是调用setExtendedState(...当窗口被激活时,如下所示:

public class SomeFrame extends JFrame {

    public SomeFrame() {
        // ...
        setVisible(true);
        setResizable(true);
        // if you are calling setSize() for fallback size, do that here
        addWindowListener (
            new WindowAdapter() {
                private boolean shown = false;
                @Override
                public void windowActivated(WindowEvent we) {
                    if(shown) return;
                    shown = true;
                    setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
                }
            }
        );
    }
}

I ended up using this code:我最终使用了这段代码:

public void setMaximized(boolean maximized){
    if(maximized){
        DisplayMode mode = this.getGraphicsConfiguration().getDevice().getDisplayMode();
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
        this.setMaximizedBounds(new Rectangle(
                mode.getWidth() - insets.right - insets.left, 
                mode.getHeight() - insets.top - insets.bottom
        ));
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    }else{
        this.setExtendedState(JFrame.NORMAL);
    }
}

This options worked the best of all the options, including multiple monitor support.此选项在所有选项中效果最好,包括多显示器支持。 The only flaw this has is that the taskbar offset is used on all monitors is some configurations.唯一的缺陷是在所有显示器上使用任务栏偏移量是一些配置。

@kgiannakakis answer is fully correct, but if someone stuck into this problem and uses Java 6 on Linux (by example, Mint 19 Cinnamon), MAXIMIZED_BOTH state is sometimes not applied. @kgiannakakis 的回答是完全正确的,但如果有人陷入这个问题并在Linux上使用Java 6 (例如,Mint 19 Cinnamon),有时不会应用MAXIMIZED_BOTH状态。

You could try to call pack() method after setting this state.您可以尝试在设置此状态后调用pack()方法。

Code example:代码示例:

public MainFrame() {
    setContentPane(contentPanel); //some JPanel is here
    setPreferredSize(new Dimension(1200, 800));
    setMinimumSize(new Dimension(1200, 800));
    setSize(new Dimension(1200, 800));
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    pack();
}

This is not necessary if you are using Java 7+ or Java 6 on Windows.如果您在 Windows 上使用 Java 7+ 或 Java 6,则不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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