How you should structure your JPanels
inside a JFrame
is an an architectural question that is beyond the scope of this site.
To help wrap your head around what the difference between these two sorts of containers:
Change public class Gato extends javax.swing.JFrame implements ActionListener {
to public class Gato extends javax.swing.JPanel implements ActionListener {
Then, create a new "driver" class that does something like this:
public static void main(String[] args){
JFrame frame = new JFrame();
Gato gato = new Gato();
frame.setContentPane(gato);
frame.setVisible(true);
}
The only difference between a JFrame and a JPanel is that a JFrame is a top-level container - it exists as its own window - while a JPanel is something that has to be loaded inside a top-level container. Here is a great discussion about the difference between them conceptually.
In practice, once you have your frame created and your panel embedded into it (there are some quirks to this process, and I'll admit they tripped me up quite a few times when I was first wrapping my head around Swing - be sure to read the documentation very carefully, paying particular attention to JFrame.pack()
and JPanel.validate()
), you can treat that panel exactly as you would if it was a frame.
In fact, I often test JPanel elements by changing them to JFrames in order to test them independent of any container.