搜尋
首頁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.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),