如何使java swing JFrame正确调整大小 [英] how to make a java swing JFrame resize correctly

查看:60
本文介绍了如何使java swing JFrame正确调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我不能在下面调整我的框架的大小,并且随着我调整框架的大小,所有组件的大小都会越来越小.谢谢!!!

I'm wondering why I can't resize my Frame below and have all of the components resize smaller and bigger as I resize frame. Thank you!!!

public class TPASimulatorGUI extends JFrame{
    JPanel mainPanel = new JPanel();
    BoxLayout layout = new BoxLayout(mainPanel,BoxLayout.Y_AXIS);       
    mainPanel.setLayout(layout);

                // add things to main panel

    JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));

    it.add(mainPanel);

    this.getContentPane().add(it);
    this.setSize(new Dimension(1190,770));
    this.setVisible(true);

}

推荐答案

JPanel it = new JPanel(new FlowLayout(FlowLayout.LEADING));
it.add(mainPanel);

FlowLayout 始终遵循添加到其中的组件的首选大小.

A FlowLayout always respects the preferred size of the components added to it.

摆脱它"面板并尝试使用

Get rid of the "it" panel and try just using

//this.getContentPane().add(it);
add(mainPanel);

框架的默认布局是 BorderLayout,它将尝试增加/减少添加到其中的所有组件的大小.

The default layout for a frame is a BorderLayout which will try to increase/decrease the size of all components added to it.

这篇关于如何使java swing JFrame正确调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆