搜索
首页JavaJFrame 在调整大小时保持相同的窗口宽高比

php小编子墨为您带来本期的java问答,我们将探讨如何在调整JFrame大小时保持相同的窗口宽高比。JFrame是Java中常用的窗口组件,但在调整大小时保持宽高比一直是开发者们关注的问题。在本文中,我们将分享一些实用的方法和技巧,帮助您解决这一难题。让我们一起来深入探讨吧!

问题内容

我正在研究 linux gnome。

我想让窗口保持 16:9 的比例。例如,如果缩放宽度,则代码仅修改高度,因此不会触及用户输入。

通过这种方法,窗口会自行调整大小,使其超出应有的范围。 例如,我稍微缩放了宽度,但高度却比应有的要大得多。

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

解决方法

这是我的想法。

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

这样,您就不会更改用户正在调整的值。 (框架的大小)您正在响应更改并确保面板的大小仍然正确。

以上是JFrame 在调整大小时保持相同的窗口宽高比的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:stackoverflow。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具