1

I am pretty new with JPanel and JFrame and probably not working with them the way I should. I'm trying to create Pong game and after creating JFrame and adding JPanel to it, I'm able to resize my JPanel but I don't know how to resize my JFrame to fit it. Game class extends JPanel.

main:

 public static void main(String[] args) throws InterruptedException { int height = 500, width =(int) (height*1.56); //height = 500, width = 780; JFrame frame = new JFrame("Pong"); Game game = new Game(); frame.add(game); frame.setVisible(true); game.setSize(width, height); System.out.println(game.getHeight()); System.out.println(game.getWidth()); game.setBackground(Color.BLACK); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println(frame.getHeight()); System.out.println(frame.getWidth()); } 

output:

500 780 39 136 

The output should be somthing like:

500 780 *above* 500 *above* 780 

EDIT:

 public static void main(String[] args) throws InterruptedException { int height = 500, width =(int) (height*1.56); //height = 500, width = 780; JFrame frame = new JFrame("Pong"); Game game = new Game(); frame.add(game); frame.setVisible(true); game.setPreferredSize(new Dimension(width, height)); frame.pack(); System.out.println(game.getHeight()); System.out.println(game.getWidth()); game.setBackground(Color.BLACK); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println(frame.getHeight()); System.out.println(frame.getWidth()); } 

Changing the setSize to setPreferredSize and then call pack() for the JFrame fixed it all.

2
  • 1
    Call pack() on the JFrame after adding all components. Never set size either but instead deal with preferred size (preferredSize). Commented Jul 9, 2016 at 21:49
  • I got it, Thank you! There isn't any simple answer all over the internet and you had her. Commented Jul 9, 2016 at 22:01

1 Answer 1

2

Again, deal with preferred sizes and make sure to call pack() on your JFrame after adding components. Also, it's better to override getPreferredSize rather than calling setPreferredSize. For example:

import java.awt.Dimension; import javax.swing.*; public class Game extends JPanel { private static final int PREF_W = 400; private static final int PREF_H = 300; private int prefW; private int prefH; public Game(int prefW, int prefH) { this.prefW = prefW; this.prefH = prefH; } public Game() { this(PREF_W, PREF_H); } @Override public Dimension getPreferredSize() { if (isPreferredSizeSet()) { return super.getPreferredSize(); } return new Dimension(prefW, prefH); } private static void createAndShowGui() { int height = 500; int width =(int) (height*1.56); //height = 500, width = 780; Game game = new Game(width, height); JFrame frame = new JFrame("Game"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(game); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); System.out.println("Frame size: " + frame.getSize()); System.out.println("game size: " + game.getSize()); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> createAndShowGui()); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.