简体   繁体   中英

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

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);
    }
}

在将组件和容器添加到当前JFrame之后调整大小。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM