JFrame maintains same window aspect ratio when resized
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); } }); } }
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(); } });
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

