1

I am having some serious trouble on JButtons. My problem is that the JButton just won't show up, no matter what I try. I've been trying everything for over an hour, and nothing has worked, so I think it is time to ask here. Here is my code. It's just the constructor because the whole class is really big and you don't need to see it.

public Game() {

    frame = new JFrame(NAME);
    canvas = new Canvas();

    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setSize(width, height);

    frame.add(canvas, BorderLayout.CENTER);

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
    frame.setFocusable(true);
    frame.requestFocus();

    JPanel panel = new JPanel();
    JButton play = new JButton("Hi");

    panel.add(play);
    frame.getContentPane().add(panel);
    panel.setPreferredSize(new Dimension(width, height));

}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • try `frame.add(panel, BorderLayout.SOUTH);` – Sanjeev Aug 10 '16 at 06:47
  • You have to give the `JPanel` a layout. Either `BorderLayout`, `BoxLayout`, ... Take a look here [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – Nico T Aug 10 '16 at 06:49
  • @NicoT *"You have to give the `JPanel` a layout."* By default, panels have a `FlowLayout` so ..no you don't. – Andrew Thompson Aug 10 '16 at 06:52
  • 1
    BTW - please give your class a more unique name than `Game` even if it takes adding your own name like `ElijahEeedaritaGame`. Do you realise just how many broken `Game` classes I've dealt with in my time? My IDE is baulking at deleting all parts of the previous broken `Game` .. – Andrew Thompson Aug 10 '16 at 06:55

2 Answers2

1

Main problems:

  1. frame.pack(); should be done after all components are added.
  2. frame.setVisible(true); is best put after all adding, packing & configuration.

import java.awt.*;
import javax.swing.*;

public class Game {

    private JFrame frame = null;
    private Canvas canvas = null;
    private String NAME = "Moronically Named Game";
    int width = 500;
    int height = 200;

    public Game() {

        frame = new JFrame(NAME);
        canvas = new Canvas();

        canvas.setMinimumSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setFocusable(false);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setSize(width, height);

        frame.add(canvas, BorderLayout.CENTER);

        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setFocusable(true);
        frame.requestFocus();

        JPanel panel = new JPanel();
        JButton play = new JButton("Hi");

        panel.add(play);
        frame.getContentPane().add(panel);
        panel.setPreferredSize(new Dimension(width, height));

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                Game o = new Game();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Other tips:

  1. For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. E.G. as seen in my answer. You posted an uncompilable code snippet in the question, I turned it into an MCVE for the answer.
  2. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
  3. As to layout out the GUI with padding as mentioned in tip (2). Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • N.B. After looking more closely at your code, I realised this will not produce what you expect either. I'll give the problem a further look once you've done tips 1 & 3. The ball is in your court.. – Andrew Thompson Aug 10 '16 at 07:18
  • This actually worked. However, in the end I decided to add my own GUI system to fit better with the program. I still put in the frame.pack() and frame.setVisible(true) at the end. Also, one more question, is it okay to use setPreferredSize on a canvas, since it doesn't need to change dimensions? – Elijah Seed Arita Aug 10 '16 at 17:48
  • The advice in that linked thread is to override the `getPreferredSize()` method, rather than call `setPReferredSize()` .. – Andrew Thompson Aug 10 '16 at 18:03
0

Based on the code you have provided I believe the canvas = new Canvas() component covered the entire JFrame since you have set it's dimension same with the jframe which makes the other component looks like not showing.

Gigeh
  • 49
  • 5