Home > Java > JFrame maintains same window aspect ratio when resized

JFrame maintains same window aspect ratio when resized

PHPz
Release: 2024-02-22 12:55:06
forward
1184 people have browsed it

php editor Zimo brings you this issue of java Q&A. We will discuss how to maintain the same window aspect ratio when resizing a JFrame. JFrame is a commonly used window component in Java, but maintaining the aspect ratio when resizing has always been a concern for developers. In this article, we will share some practical methods and tips to help you solve this dilemma. Let’s dive in!

Question content

I am studying linux gnome.

I want the window to maintain a 16:9 ratio. For example, if you scale the width, the code only modifies the height, so user input is not touched.

With this approach, the window resizes itself beyond what it should be. For example, I scaled the width slightly, but the height is much larger than it should be.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main
{
    public static void main(String[] args)
    {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(1280, 720));
        panel.setBackground(new Color(89, 108, 171, 255));

        JFrame frame = new JFrame("Test");
        frame.setLocation(100, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        panel.addComponentListener(new ComponentAdapter()
        {
            @Override
            public void componentResized(ComponentEvent e)
            {
                int targetWidth = frame.getSize().width;
                int targetHeight = frame.getSize().width * 9 / 16;

                if (targetHeight > frame.getSize().height)
                {
                    targetHeight = frame.getSize().height;
                    targetWidth = frame.getSize().height * 16 / 9;
                }

                frame.setSize(targetWidth, targetHeight);
            }
        }); 
    }
}
Copy after login

Solution

This is my idea.

JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(1280, 720));
    panel.setBackground(new Color(89, 108, 171, 255));

    JFrame frame = new JFrame("Test");
    frame.setLocation(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p2 = new JPanel();
    p2.add(panel);
    frame.setContentPane(p2);
    frame.pack();
    frame.setVisible(true);

    p2.addComponentListener(new ComponentAdapter()
    {
        @Override
        public void componentResized(ComponentEvent e)
        {
            int fw = frame.getSize().width;
            int fh = frame.getSize().height;
            if( 16*fh > 9*fw ){
                panel.setPreferredSize( new Dimension(fw, 9*fw/16) );
            } else{
                Dimension d = new Dimension(16*fh/9, fh);
                panel.setPreferredSize( d );
            }

            frame.validate();
        }
    });
Copy after login

This way you don't change the value that the user is adjusting. (size of the frame) You are responding to changes and making sure the panel is still the correct size.

The above is the detailed content of JFrame maintains same window aspect ratio when resized. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template