How to make my old JFrame to JPanel, and how to make it actually work as it worked while I used JFrame

StackOverflow https://stackoverflow.com/questions/23355447

  •  11-07-2023
  •  | 
  •  

Question

Im really new to swing lib, like maybe 5 days ago I started using it... so I have so many problems... first, I made a Tic Tac Toe game on a JFrame, it would be cool to leave it like that, but then I came with the idea to make a small menu to acces multiple little games (just for fun/practice so far). So I actually succed, then it happens... I made all my games in JFrames so, to make a menu its also another JFrame, so I have so many windows... all the time!

I made some research and people usually makes Jpanels for this kind of stuff.. But I dont know how to transfer my games (Tic Tac Toe) to a JPanel and make it work exactly as it was... here is my code of the Tic Tac Toe in JFrame... the IDE is netbeans.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;


public class Gato extends javax.swing.JFrame implements ActionListener {


Boton MAPA[][];


public static boolean turno=true;

 public Gato(){
    initComponents();

    MAPA= new Boton[3][3];

     for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
             MAPA [i][j]=new Boton();
             MAPA[i][j].u.setBounds((i*230)+10,(j*230)+10,230,230);
             MAPA[i][j].u.addActionListener(this);
             this.add(MAPA[i][j].u);
         }

     }
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JLabel o=new JLabel("Hola");
     o.setBounds(60, 60, 100, 100);
     this.add(o);

}
Was it helpful?

Solution

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.

OTHER TIPS

Instead of adding components to the JFrame, add them to a JPanel, then add the JPanel to the JFrame.

Well for your problem you just need one JDesktopPane and All your JFrame need JIternalFrame and call all InternalFrame in JDesktopPane.

Go to this link and check out examples

you don't need to convert them into panel and all. or you can also try netbeans inbuilt examples for JDesktopPane.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top