简体   繁体   English

Java未最大化JFrame

[英]Java un-maximize JFrame

I have a simple question that I have not found an answer two, possibly because I do not know how to title it. 我有一个简单的问题,我找不到答案二,可能是因为我不知道如何给它加上标题。 Lets say I have a JFrame with width X and height Y. Somewhere in my program I must maximize it to fit to take up the whole screen, and do myJFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 可以说我有一个宽度为X且高度为Y的JFrame。我必须在程序中的某个位置将其最大化以适合整个屏幕,并进行myJFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); to accomplish that. 做到这一点。 Now, somewhere later I must make the JFrame "un-maximize" and return to its original size. 现在,在以后的某个地方,我必须使JFrame“未最大化”并返回其原始大小。 There is probably a simple command for this but i have not found any. 为此可能有一个简单的命令,但我还没有找到任何命令。 I have in mind a command akin to something like myJFrame.setExtendedState(JFrame.MAXIMIZED_NONE); 我想到的命令类似于myJFrame.setExtendedState(JFrame.MAXIMIZED_NONE); So in short: after maximizing a JFrame, how do I "un-maximize" it? 简而言之:最大化JFrame之后,如何“取消最大化”呢?

Edit: It seems that the problem was not that i was not able to find the method i neaded, but that some of my code was preventing myJFrame.setExtendedState(JFrame.NORMAL); 编辑:似乎问题不在于我无法找到我所使用的方法,而是我的一些代码阻止了myJFrame.setExtendedState(JFrame.NORMAL); from working correctly. 不能正常工作。 Here is this buggy code: 这是错误的代码:

        frame.dispose();
        if (b) {
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setUndecorated(true);
        } else {
            frame.setExtendedState(JFrame.NORMAL);
            frame.setUndecorated(false);
        }
        frame.setVisible(true);

Rearranging the statements in the above code fixed the problem :) 重新排列以上代码中的语句可解决问题:)

If you have a look at the JavaDocs for Frame#setExtendedState , you will see a list of possible values 如果您查看Frame#setExtendedState的JavaDocs,将看到可能值的列表

Sets the state of this frame. 设置该框架的状态。 The state is represented as a bitwise mask. 该状态表示为按位掩码。
- NORMAL -正常
Indicates that no state bits are set. 指示未设置任何状态位。
- ICONIFIED -已确认
- MAXIMIZED_HORIZ -MAXIMIZED_HORIZ
- MAXIMIZED_VERT -MAXIMIZED_VERT
- MAXIMIZED_BOTH -MAXIMIZED_BOTH
Concatenates MAXIMIZED_HORIZ and MAXIMIZED_VERT. 连接MAXIMIZED_HORIZ和MAXIMIZED_VERT。

Then if we have a look at Frame#NORMAL 然后,如果我们看一下Frame#NORMAL

Frame is in the "normal" state. 框架处于“正常”状态。 This symbolic constant names a frame state with all state bits cleared 该符号常量为帧状态命名,并清除所有状态位

That seems promising 那看起来很有希望

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JButton unmax = new JButton("Switch");
            unmax.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFrame frame = (JFrame) SwingUtilities.windowForComponent(TestPane.this);
                    if (frame.getExtendedState() != JFrame.NORMAL) {
                        frame.setExtendedState(JFrame.NORMAL);
                    } else {
                        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    }
                }
            });
            setLayout(new GridBagLayout());
            add(unmax);

        }

    }

}

So, all this does, is checks the current windows extendedState , if it's not set to NORMAL , it sets it to NORMAL , otherwise it sets it to MAXIMIZED_BOTH 因此,所有这些操作就是检查当前窗口的extendedState ,如果未将其设置为NORMAL ,则将其设置为NORMAL ,否则将其设置为MAXIMIZED_BOTH

Updated based on example code... 根据示例代码进行了更新...

Okay, so based on your example code... 好的,因此根据您的示例代码...

frame.dispose();
if (b) {
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setUndecorated(true);
} else {
    frame.setExtendedState(JFrame.NORMAL);
    frame.setUndecorated(false);
}
frame.setVisible(true);

The problem "seems" to lie with either dispose or setUndecorated or a combination of both. 问题“似乎”在于disposesetUndecorated或两者结合。 What "seems" to happening is when you restore the frame to it's normal state, it's maintaining the size and position it was when it was maximized. 发生的“似乎”是当您将框架恢复到正常状态时,它保持了最大化时的大小和位置。

So, what I did, was grab a copy of the windows bounds before it was maximised and when it was restored, I reapplied it 因此,我要做的是在最大化之前获取窗口bounds的副本,并在还原时重新应用它

public class TestPane extends JPanel {

    private Rectangle previousBounds;

    public TestPane() {

        JButton unmax = new JButton("Switch");
        unmax.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = (JFrame) SwingUtilities.windowForComponent(TestPane.this);
                frame.dispose();
                if (frame.getExtendedState() != JFrame.NORMAL) {
                    frame.setExtendedState(JFrame.NORMAL);
                    frame.setUndecorated(false);
                    frame.setBounds(previousBounds);
                } else {
                    previousBounds = frame.getBounds();
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    frame.setUndecorated(true);
                }
                frame.setVisible(true);
            }
        });
        setLayout(new GridBagLayout());
        add(unmax);

    }

}

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

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