Monday, 3 October 2016

java - Resizing JFrame to fit resized JPanel



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.


Answer



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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...