开发者

Java custom JComponent: Why is getMinimumSize() ignored?

I've overridden getPreferredSize() and getMinimumSize() in my custom JComponent. While getPreferredSize() is working as expected, getMinimumSize() instead is ignored as I am able to resize the JFrame until my custom component disappears. Why? How to avoid it?

Here is a complete runnable program that shows my point. What am I doing wrong?

Main.java

    package swing.minimumsize;

    import javax.swing.JFrame;

    @SuppressWarnings("serial")
    public class Main extends JFrame {

        public Main() {
         开发者_开发技巧   setTitle("Custom Component Test");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public void display() {
            add(new CustomComponent());
            pack();
            setVisible(true);
        }

        /**
        * @param args
        */
        public static void main(String[] args) {
            Main main = new Main();
            main.display();
        }

    }

CustomComponent.java

    package swing.minimumsize;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;

    import javax.swing.JComponent;

    @SuppressWarnings("serial")
    public class CustomComponent extends JComponent {

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

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 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);
        }

    }


..getMinimumSize() instead is ignored as I am able to resize the JFrame until my custom component disappears. Why? How to avoid it?

Check the 1st comment.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Main extends JFrame {

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

    public void display() {
        add(new CustomComponent());
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getSize());
        setVisible(true);
    }

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

class CustomComponent extends JComponent {

    CustomComponent() {
        setBackground(Color.YELLOW);
    }

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

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // paint the BG.
        g.setColor(getBackground());
        g.fillRect(0,0,getWidth(),getHeight());

        int margin = 10;
        Dimension dim = getSize();

        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width-margin*2, dim.height-margin*2);
    }
}


What is ignored or listened to will depend on the layout of the container that holds the JComponent. Since you're adding your comopnent to a JFrame BorderLayout.CENTER, it makes sense that setMinimumSize would be ignored.


The preferred, minimum and maximum sizes are just "suggestions" that the layout manager can use or ignore when laying out components.

Edit:

In addition to Andrew's suggestion you can try using a layout manager that respects the minimum size:

    Container contentPane = getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    add(new CustomComponent());
    pack();
    setMinimumSize(getMinimumSize());
    setVisible(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜