Table of Contents
Question content
Solution
Home Java JFrame maintains same window aspect ratio when resized

JFrame maintains same window aspect ratio when resized

Feb 22, 2024 pm 12:55 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)