A
A
Alexey R2015-03-01 23:43:03
Java
Alexey R, 2015-03-01 23:43:03

Tell me why the maximum window size is not set?

Good day. I'm just starting to learn Java, and at every step I meet a lot of pitfalls. Sometimes mistakes are stupid and banal. I can sit all day trying to solve some nonsense. Maybe it's wrong to ask some stupid questions, but already understood, it's better to ask and move on. In this connection, I ask you to help me in solving this problem. I need to set the size of the form so that its minimum size is 400 by 400 and the maximum is 600 by 600.

JFrame frame = new JFrame("Новая форма");
    frame.setVisible(true);
    frame.setSize(400, 400);
    frame.setMinimumSize(new Dimension(400, 400));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setMaximumSize(new Dimension(600, 600));

If you restrict the user by reducing the window, everything worked out, then the setMaximumSize method does not work at all. Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2015-03-02
@GavriKos

Superficial googling shows that this is a known bug , and that one must either wait for a fix or use alternatives.

A
Alexey Bykov, 2015-03-02
@jusio

This is an ancient Swing bug on Windows that will most likely not be fixed. You can fix this manually with a ComponentListener, use it to catch resize events and manually resize the form. Example:

final JFrame frame = new JFrame();
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                int width = e.getComponent().getWidth();
                int height = e.getComponent().getHeight();
                int newWidth = width;
                int newHeight = height;
                if (width > 600) {
                    newWidth = 600;
                }
                if (width < 400) {
                    newWidth = 400;
                }
                if (height > 600) {
                    newHeight = 600;
                }
                if (height < 400) {
                    newHeight = 400;
                }
                if (newHeight != height || newWidth != width) {
                    frame.setSize(newWidth, newHeight);
                }
            }
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question