0

I have a little game, where I use an absolute size of JPanel, to fit the levels inside, which are made from graphic-tiles from a fixed size.

The JPanel must have exact 1088 pixel width and 704 pixels hight, to make the levels fit exactly.

How do I resize the JFrame to fit exactly around the JPanel?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Drachenbauer
  • 57
  • 1
  • 4
  • https://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#pack--, https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html – JB Nizet May 03 '19 at 21:32
  • 1
    You override `JPanel`'s `getPreferredSize()` method (see: https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), then you call `pack()` on the `JFrame`. – Frakcool May 03 '19 at 21:33
  • my class extends JPanel, and now i have this: this.getContentPane().setPreferredSize(new Dimension(board.WIDTH, board.HIGHT)); this.pack(); If i use undecorated, it fits perfectly, but if i don´t use this, the JFrame is 10 pixel to big, (lets a narrow line at the right and bottom.) – Drachenbauer May 03 '19 at 22:00
  • 1
    `The JPanel must have exact 1088 pixel width and 704 pixels` - why? My screen is only 1024 x 768. You should not be hardcoding values. Your panel size should be flexible and adjust based on the resolution of the screen being used. `If i use undecorated, it fits perfectly, but if i don´t use this, the JFrame is 10 pixel to big` - because the frame has a border. Check out: https://stackoverflow.com/questions/27474339/why-is-my-frame-larger-than-what-i-set-it-to-be/27474432#27474432 for a simple example to get information about your desktop size and frame border size. – camickr May 03 '19 at 22:16
  • it seams my screen has a bigger width than yours. and my panel is filled with a game-level, made from a fixed number of fixed sized graphic-tiles, and i want the window fit exactly arount the game-level. – Drachenbauer May 04 '19 at 17:07
  • if i use this.setUndecorated(true); , it fits exactly around my panel. But if i don´t use it, it leaves a gap of 10 pixels at the right and bottom between it´s border and the panel – Drachenbauer May 04 '19 at 17:14
  • maybe, it calculated some space for scrollbars. as i placed setResizable(false); above pack; , it fit´s perfectly. – Drachenbauer May 04 '19 at 18:53
  • It´s just a game making experiment for my own, because i like to create little java-applications, and test their functions, and on my screen this game-window fit´s good. i don´t think about selling it or something like that – Drachenbauer May 04 '19 at 19:02

0 Answers0