JFrame created is not displayed

1

I am starting to program in Java and I have encountered this problem, I am using Eclipse , the thing is that at the time of executing the program the JFrame container of the others JPanel is not displayed, and no I see the error

 public class AddClient extends JFrame implements ActionListener
  {


    private static final long serialVersionUID = 1L;
        //ATTR'S
        private JFrame Frame;
        private JPanel RPanel, LPanel, UPanel, CPanel;
        private JButton LButton1, LButton2, LButton3;
        private JButton RButton1, RButton2, RButton3;
        private JButton UButton1, UButton2, UButton3;
        private JButton CButton1, CButton2, CButton3;
        private Container Cont;


    //Constructor 
    public AddClient()
    {
        this.CPanel = null;
        this.Frame = null;
        this.RPanel = null;
        this.LPanel = null;
        this.UPanel = null;
        this.CPanel = null;
        this.RButton1 = null;
        this.RButton2 = null;
        this.RButton3 = null;
        this.LButton1 = null;
        this.LButton2 = null;
        this.LButton3 = null;
        this.UButton1 = null;
        this.UButton2 = null;
        this.UButton3 = null;
        this.CButton1 = null;
        this.CButton2 = null;
        this.CButton3 = null;
        this.Cont = null;


         makeLPanel();
         makeRPanel();
         makeUPanel();
         makeCPanel();
         makeWindow();

    }

    // funcion para hacer el panes izquierdo
    public void makeLPanel()
    {
        this.LPanel = new JPanel();
        this.LButton1 = new JButton("LBoton1");
        this.LButton2 = new JButton("LBoton2");
        this.LButton3 = new JButton("LBoton3");
        this.LPanel.setLayout(new BoxLayout(LPanel, BoxLayout.Y_AXIS));
        this.LPanel.add(this.LButton1);
        this.LPanel.add(this.LButton2);
        this.LPanel.add(this.LButton3);

    }

    // funcion para hacer el panel Derecho
    public void makeRPanel()
    {
        this.RPanel = new JPanel();
        this.RButton1 = new JButton("RBoton1");
        this.RButton2 = new JButton("RBoton2");
        this.RButton3 = new JButton("RBoton3");
        this.RPanel.setLayout(new BoxLayout(RPanel, BoxLayout.Y_AXIS));
        this.RPanel.add(this.RButton1);
        this.RPanel.add(this.RButton2);
        this.RPanel.add(this.RButton3);
    }

    // FUNCION creacion de Panel Superior

    public void makeUPanel()
    {
        this.UPanel = new JPanel();
        this.UButton1 = new JButton("UBoton1");
        this.UButton2 = new JButton("UBoton2");
        this.UButton3 = new JButton("UBoton3");
        this.UPanel.setLayout(new FlowLayout());
        this.UPanel.add(this.UButton1);
        this.UPanel.add(this.UButton2);
        this.UPanel.add(this.UButton3);
    }

    //Panel Central
        public void makeCPanel()
        {
            this.CPanel = new JPanel();
            this.CButton1 = new JButton("LBoton1");
            this.CButton2 = new JButton("LBoton2");
            this.CButton3 = new JButton("LBoton3");
            this.CPanel.setLayout(new BoxLayout(CPanel, BoxLayout.Y_AXIS));
            this.CPanel.add(this.CButton1);
            this.CPanel.add(this.CButton2);
            this.CPanel.add(this.CButton3);
        }

    //Creamos Ventana principal

    public void makeWindow()
    {
        this.Frame = new JFrame();
        this.Frame.setTitle("Gestion de Bienes Raices");
        this.Frame.setSize(1000, 900);
        this.Frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.Frame.setLocationRelativeTo(null);
        this.Frame.setResizable(false);
        Cont = Frame.getContentPane();
        Cont.setLayout(new BorderLayout());
        Cont.add(this.UPanel, BorderLayout.NORTH);
        Cont.add(this.CPanel, BorderLayout.CENTER);
        Cont.add(this.RPanel, BorderLayout.EAST);
        Cont.add(this.LPanel, BorderLayout.WEST);


    }


    /* esto va en la clase main de nombre
      RealState y pretendo llamar la clase addClient 
       desde acá y cargar el JFrame. */

    public class RealState {

    public static void main(String[] args) {
        // Inicializamos 
        AddClient pf = new AddClient();
        pf.setVisible(true);

    }

}
}
    
asked by Alvaro Santafe 08.04.2017 в 00:27
source

2 answers

0

You did not include the class declaration of AddClient , but I suppose if it allows you to call setVisible() to the instance that is a:

public class AddClient extends JFrame{

If that's the case, it's obvious why you do not see anything:

in makeWindow() you are prompting a new JFrame that you never make visible.

public void makeWindow()
{
    this.Frame = new JFrame();
    this.Frame.setTitle("Gestion de Bienes Raices");
    this.Frame.setSize(1000, 900);
    this.Frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.Frame.setLocationRelativeTo(null);
    this.Frame.setResizable(false);
    Cont = Frame.getContentPane();
    Cont.setLayout(new BorderLayout());
    Cont.add(this.UPanel, BorderLayout.NORTH);
    Cont.add(this.CPanel, BorderLayout.CENTER);
    Cont.add(this.RPanel, BorderLayout.EAST);
    Cont.add(this.LPanel, BorderLayout.WEST);
    // eso funciona:
    this.Frame.setVisible(true);
}

In any case I recommend you re-invoice your code to a class that extends JFrame and then only build a container of your taste and add it as contentPane . in this case, your code would be:

public void makeWindow()
{
    this.setTitle("Gestion de Bienes Raices");
    this.setSize(1000, 900);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    Cont = getContentPane();
    Cont.setLayout(new BorderLayout());
    Cont.add(this.UPanel, BorderLayout.NORTH);
    Cont.add(this.CPanel, BorderLayout.CENTER);
    Cont.add(this.RPanel, BorderLayout.EAST);
    Cont.add(this.LPanel, BorderLayout.WEST);
}

That also works with the code in your main .

Finally let me advise you: if you want to make the life of your peers easier in finding your mistakes, consider following the common practices of formatting variable names, classes and constants:

  • lowercase methods with camelCase, starting with a verb (axis getContentPane() )
  • lowercase variables with camelCase: (axis fichaTecnica )
  • classes starting with capital letters, then camelCase (axis AddClient )
  • capital constants (axis EXIT_ON_CLOSE )
answered by 08.04.2017 / 00:40
source
0

When you add the components to your container, you add it to a variable of type Container that contains the ContentPane of your JFrame created in the method makeWindow , and in the method main creas and instances your class AddClient that behaves like a JFrame but does not contain your components. possible solution (not the best) is from class main access to your variable Frame

 public  static void main(String[] args) {
    AddClient pf = new AddClient();
    pf.Frame.setVisible(true);
 }

Note that if the class extends from JFrame You do not need to create a new instance of JFrame since the class will behave as a JFrame , in addition you can add components without calling getContentPane , if not directly with the method add

public void makeWindow()
{
    setTitle("Gestion de Bienes Raices");
    setSize(300, 300);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());
    add(this.UPanel, BorderLayout.NORTH);
    add(this.CPanel, BorderLayout.CENTER);
    add(this.RPanel, BorderLayout.EAST);
    add(this.LPanel, BorderLayout.WEST);
}
    
answered by 08.04.2017 в 00:45