IT story

JFrame 최대화 창

hot-time 2020. 9. 18. 19:25
반응형

JFrame 최대화 창


스윙을 사용하여 빠르고 더러운 애니메이션을 구성하고 있습니다. 창을 최대화하고 싶습니다. 어떻게 할 수 있습니까?


JFrame을 확장하는 경우 :

public void run() {
    MyFrame myFrame = new MyFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.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);
    }
}

JFrame을 사용하는 경우 이것을 시도하십시오.

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

어때요 JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)?


나는이 버전을 좋아한다 :

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

JFrame을 전체 화면으로 설정하는 방법은를 MAXIMIZED_BOTH의미하는 옵션 을 설정하는 것입니다.이 옵션은 MAXIMIZED_VERT | MAXIMIZED_HORIZ각각 프레임을 세로 및 가로로 최대화하도록 설정합니다.

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

이 코드를 사용하여 끝났습니다.

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

이 옵션은 다중 모니터 지원을 포함하여 모든 옵션 중에서 가장 잘 작동했습니다. 유일한 결점은 작업 표시 줄 오프셋이 모든 모니터에서 사용되는 일부 구성이라는 것입니다.


I've tried the solutions in this thread and the ones here, but simply calling setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH); right after calling setVisible(true); apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). 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:

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

참고URL : https://stackoverflow.com/questions/479523/jframe-maximize-window

반응형