Setting the maximum JFrame size at application startup

I want to set the maximum JFrame size at application startup time. The problem is that if the screen resolution is larger, my frame becomes larger, but at that time it should not cross the specified maximum range, but the same case works fine with low resolution.

How I want my frame to be maximum (500 500), so I wrote this piece of code:

JFrame frame = new JFrame("FRAME TRANSPARENT");
frame.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()-50), (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-150));
frame.setMaximizedBounds(new Rectangle(0,0 , 500, 500)); 
frame.setVisible(true);

Even I installed Bound, the JFrame considers the setSize method and it seems to ignore the setMaximizedBounds method. I already tried the setMaximumized method, but got the same result.

+5
source share
3 answers

, , setMaximumSize() .. !!! setResizable(false), ,

:

public class MaxSizeUI
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MaxSizeUI().makeUI();
            }
        });
    }

    public void makeUI() {
        final JFrame frame = new JFrame("Sample Fram") {

            @Override
            public void paint(Graphics g) {
                Dimension d = getSize();
                Dimension m = getMaximumSize();
                boolean resize = d.width > m.width || d.height > m.height;
                d.width = Math.min(m.width, d.width);
                d.height = Math.min(m.height, d.height);
                if (resize) {
                    Point p = getLocation();
                    setVisible(false);
                    setSize(d);
                    setLocation(p);
                    setVisible(true);
                }
                super.paint(g);
            }
        };
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 150);
        frame.setMaximumSize(new Dimension(400, 200));
        frame.setMinimumSize(new Dimension(200, 100));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
+6

:

Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
frame.setMaximumSize(DimMax);

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
+2

use size
Dimension d=getMaximumSize(); Frame.setSize(d.width, d.height);

0
source

All Articles