John Smith
John Smith

Reputation: 719

How to Resize the window of my GUI?

I normally resize my window using setSize() method but this time round it is not working? I want to resize the window to fit all my components in. I have taken out the majority of my code as it is irrelevant for my question but have left in what is needed. I have found questions with this similar problem but the solutions seem too complicated and I can't replicate them in my code. Here is my class:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

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

    public Display() {
        super("Mandelbrot Set Display");

        JPanel jp = new JPanel();
        //setSize(1000, 1000); this doesnt resize window
        setBounds(0, 0, 800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jp);

    }

    public static void main(String[] args) {
        new Display().setVisible(true);
    }
}

Thanks

Upvotes: 1

Views: 6620

Answers (3)

Mehdi
Mehdi

Reputation: 1477

resize after adding components and containers to the current JFrame.

Upvotes: 2

Yusuf K.
Yusuf K.

Reputation: 4250

Did you try call setSize after setBounds;

 setBounds(0, 0, 800, 600);
 setSize(1000, 1000); //this will resize window

public void setBounds(int x, int y, int width, int height)

If you call setBounds after setSize, width and height is setted by setBounds

import javax.swing.JFrame;
import javax.swing.JPanel;

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

    public Display() {
        super("Mandelbrot Set Display");

        JPanel jp = new JPanel();
        setBounds(0, 0, 800, 600); //800 and 600 not effect because of next line setSize method
        setSize(1000, 1000);// this doesnt resize window
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jp);

    }

    public static void main(String[] args) {
        new Display().setVisible(true);
    }
}

Upvotes: 2

Francesco Sollazzi
Francesco Sollazzi

Reputation: 434

Try calling this.setSize(x, y) :

this.setSize(WIDTH, HEIGHT);

You need to put this before the method call because your class is extended to JFrame and you need to attribute the method setSize at your JFrame using this.

Upvotes: 1

Related Questions