-1

I am following this guy's video: https://www.youtube.com/watch?v=1gir2R7G9ws. Also, I'm using Netbeans. Each time I run it, like when I tried using preffered size instead of set size, it still outputs a small window.

public class APCSGame extends Canvas implements Runnable{

...

public final int WINWIDTH=1000,WINHEIGHT=500, SCALE=1;;
JFrame frame;

private BufferedImage backgroundMenu=null;

public APCSGame(){

    frame=new JFrame("Testing it out boiiiii");
    setSize(WINWIDTH*SCALE,WINHEIGHT*SCALE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);

    frame.add(this);
    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

...

public static void main(String[] args) {
    new APCSGame().start();

}

}
  • Don't post threads, please simplify your code into a [mcve] i.e. a simple program where you place an image inside the `JFrame`. Don't extend `Canvas` it belongs to AWT, instead use a `JPanel`. Also see [Should I avoid the use of setPreferred/Maximum/MinimumSize?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) (YES). Instead override the `getPreferredSize` of your canvas (or `JPanel`) – Frakcool Dec 21 '16 at 01:35
  • I'm talking to you. Haven't you read what [`pack()`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()) does? It respects the `preferredSize` of the components and resize your frame to the minimum size where all the components have their `preferredSize` respected – Frakcool Dec 21 '16 at 01:45
  • Uh... I just commented out the setLayout call and it worked fine. –  Dec 21 '16 at 02:28
  • 4 years later and I wanted to come back and say that yes sir, you were in fact incorrect in your response. Hopefully you've dealt with your pride runtime exception as it resulted in a mild annoyance when trying to familiarize myself with a new framework. –  Aug 06 '20 at 12:29
  • Are you talking to me? What pride are you talking about? I don't get it, and I don't know what response or why you say I was incorrect – Frakcool Aug 06 '20 at 12:47

3 Answers3

2

Why should you set the Frame's layout to null?

Please remove or comment this line : frame.setLayout(null);

And then it uses the default FlowLayoutlayout manager. Or you can specify some other layout manager.

Young Emil
  • 2,220
  • 2
  • 26
  • 37
  • I commented out the line, but it still outputs a small window. The reason why I used a null layout is because I wanted to be able to manually place elements on the frame. I would have made a custom layout, but I'm lazy –  Dec 21 '16 at 01:47
  • My answer should work!!! Let your code be as exactly what you have posted and then just comment the `frame.setLayout(null);`. – Young Emil Dec 21 '16 at 01:52
  • I think I understand what you want my code to do, but it still doesn't increase the size. Now I can't even scale it if I setResizable to true... –  Dec 21 '16 at 02:07
  • See I don't know what you are up to but I have tried this code myself and it worked. I only commented this `frame.setLayout(null);`, thats all. I could resize okay. Everything is working fine. If you think you have made too much changes to the code in your editor, you can copy what you posted here. – Young Emil Dec 21 '16 at 02:12
  • Also ensure that the image is in the location. – Young Emil Dec 21 '16 at 02:16
  • Thanks! I found out that I moved some methods around for some reason and had to move them back... but this worked! –  Dec 21 '16 at 02:18
0

If you want to create a container without a layout manager as you doing in your example;

    frame.setLayout(null);

It means, container children will be having absolute positions. You need to do the following;

  • Call the Components class setbounds method for each of the container's children
  • Call the Component class repaint method

Although it is possible to do without a layout manager, you should use a layout manager if at all possible.

fabfas
  • 2,200
  • 1
  • 21
  • 21
-1

use this code for setting the size

this.setSize(width,height);

or

frame.setSize(width,height);
Danh
  • 5,916
  • 7
  • 30
  • 45
nag
  • 19
  • 1
  • Neither of these work. for some reason frame.setSize didn't take the image away this time but the Frame itself is still minuscule. –  Dec 21 '16 at 01:30