Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

swing - 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). 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

yes and very simple solution just override and return 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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...