简体   繁体   English

Java窗口内容调整大小,但不超过最小大小

[英]Java window contents resize, but not beyond a minimum size

I have a Java apps I've written using NetBeans (yes, I've read plenty of complaints here about NetBeans). 我有一个使用NetBeans编写的Java应用程序(是的,我在这里阅读过很多有关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? 我是否应该寻找其他可能涉及的财产?

Thanks. 谢谢。

yes and very simple solution just override and return getMinimumSize() 是的,非常简单的解决方案只是重写并返回getMinimumSize()

for example 例如

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

Can we see some example code? 我们可以看到一些示例代码吗? I believe that its not the window but the content inside the window that's not resizing beyond a certain point. 我相信它不是窗口,而是窗口中内容的大小不会超出特定点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM