0

I know that there are similar questions on SO like here, but none of them have seemed to fix my problem.

So far, I have two classes: a Game class that extends Canvas, and a Window class. All the Window class really does is create the JFrame and the JPanel's and adds all the necessary components.

One of the components I've been trying to add to the JPanel is the Game class itself. I've passed in a Game (which is a child class of Canvas) to the Window constructor:

public Window(int width, int height, String title, Game game) {
    // ...
}

And, inside the Window constructor, apparently adding the Game to the JFrame itself works:

public Window(int width, int height, String title, Game game) {
    JFrame frame = new JFrame();

    // frame.setSize(new Dimension(500,500)); frame.setVisible(true); etc.
    frame.add(game);
}

However, when I try to add the Game to the panel, it won't show!

 public Window(int width, int height, String title, Game game) {
     JFrame frame = new JFrame();
     JPanel panel = new JPanel(new GridBagLayout());

     panel.setSize(new Dimension(400,500));
     panel.setLocation(0,0);
     panel.requestFocus();
     panel.setVisible(true);

     frame.setSize(500, 500);
     frame.setResizable(false);
     frame.setLocationRelativeTo(null);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     frame.setTitle(title);

     frame.setVisible(true);

     GridBagConstraints gbc = new GridBagConstraints();
     panel.add(game, gbc); // doesn't work
     frame.add(panel);
 }

If you're wondering why I don't just add Game (which again extends Canvas) to the JFrame itself, it's because I want to add more panels to the frame - and I don't want the panel to be the full size of the window. Adding Game to the panel would make life a whole lot easier because then I can have other panels within the window.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MattGotJava
  • 185
  • 3
  • 14
  • Likely you're likely not changing the preferred size of the game component and leaving the container JPanel's layout to its default FlowLayout. Consider giving the JPanel a BorderLayout so that the game fills its center position, and then you can add other components to other positions. Also it's usually a bad idea to mix heavy weight and light weight components in a GUI as you're doing. – Hovercraft Full Of Eels Aug 11 '18 at 17:04
  • Thank you! I tried adding a BorderLayout rather than a GridBagLayout, and it seemed to work. However, I'm not sure why - – MattGotJava Aug 11 '18 at 17:17
  • BorderLayout will add whatever component is added in a default manner (without a second parameter) to the `BorderLayout.CENTER` position,and this expand that added component to fill the center position. Please read the tutorials for the details. You can find the layout manager tutorial here: [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), and you can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/questions/tagged/swing). – Hovercraft Full Of Eels Aug 11 '18 at 17:19
  • @MattGotJava, Changing the layout manager is not really a solution. It does not fix the problem, only mask the problem. The BorderLayout ignores the size of any component added to the CENTER and makes the component fill the space available in the panel. Read the linked duplicate answer. The proper solution is to override the `getPreferredSize()` method of the component so all layout managers can use this information. Read the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and working examples to get you started. – camickr Aug 11 '18 at 17:44

0 Answers0