Java窗口内容调整大小,但不超过最小大小 [英] Java window contents resize, but not beyond a minimum size

查看:321
本文介绍了Java窗口内容调整大小,但不超过最小大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NetBeans编写的Java应用程序(是的,我在这里读到了很多关于NetBeans的抱怨)。当我调整窗口大小时,内容会随窗口大小调整,直到我将窗口(实际高度)缩小到一定大小以下。然后窗口调整大小,但内容会被剪切。

I have a Java apps I've written using NetBeans (yes, I've read plenty of complaints here about NetBeans). When I resize the window, the contents resize with the window, until I shrink the window (the height, actually) below a certain size. Then the window resizes, but the contents just get clipped.

我已经检查了所有组件小部件。它们都具有零或非常小的最小尺寸。唯一具有非零首选大小的是那些不与父母一起调整大小的人,而且他们也很小。在缩小时,我不会碰到任何小部件。在我无法收缩的地方,面板底部与底部内部的小部件之间存在相当大的差距。

I've checked all of the component widgets. They all have min sizes of zero or very small numbers. The only ones with nonzero preferred sizes are those that do not resize with their parents, and they are also small. When shrinking, I'm not bumping into any of the widgets. At the point where I cannot shrink anymore, there is a sizable gap between the bottom of the panel and the widgets inside towards the bottom.

我有什么方法可以分辨什么限制这种方式的大小?是否还有其他可能涉及的房产?

Is there any way I can tell what is limiting the size in this way? Is there another property I should be looking for that might be involved?

谢谢。

推荐答案

是和非常简单的解决方案只是覆盖并返回 getMinimumSize()

yes and very simple solution just override and return getMinimumSize()

例如

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents(), BorderLayout.NORTH);
        add(new CustomComponents(), BorderLayout.CENTER);
        add(new CustomComponents(), BorderLayout.SOUTH);
        add(new CustomComponents(), BorderLayout.EAST);
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getMinimumSize());
        setPreferredSize(getPreferredSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

这篇关于Java窗口内容调整大小,但不超过最小大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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