Jpanel form can not open a jframe form

0

Good to make the question less confusing I'll say that I have a jpanel form, two jframe form, jframe1 and jframe2.

In the jframe1 I use this code for flames or insert (I do not know how to express it) the jpanel form with button:

Panel panelD = new Panel();
    panelD.setSize(1240,465); 
    panelD.setLocation(0,0); 

    PanelLab.removeAll(); 
    PanelLab.add(panelD,BorderLayout.CENTER);
    PanelLab.revalidate();
    PanelLab.repaint();

This works and the jframe1 shows the jpanel, the problem is that this jpanel has a button with which I want to open the jframe2, but when pressing it nothing happens, I use this code:

private void btnAñadirActionPerformed(java.awt.event.ActionEvent evt) {                                          
    frame2 v = new frame2();
    v.setVisible(true);
}               

I do not know if I did something wrong (PS: I have some sleep).

Sorry for the molestask, I realized I was calling another Panel form that was not, I'm an idiot, sorry to waste your time.

    
asked by FerGuy 07.09.2018 в 07:21
source

1 answer

0

According to what I have understood you need to create a second frame, when you press the button in the first frame, I add this fragment of code that I hope will help you.

public static void main(String[] args) {
    //Creamos el primer frame
    Frame frame = new Frame();

    frame.setSize(500, 500);
    frame.setVisible(true);

    //Creamos el panel
    Panel panelD = new Panel();
    panelD.setSize(400, 400);
    panelD.setLocation(0, 0);

    panelD.setVisible(true);

    //Creamos el boton
    Button btn1 = new Button("Abre segundo frame");
    btn1.setSize(50, 50);
    btn1.setLocation(10 , 10);

    btn1.setVisible(true);

    //Añadimos boton al panel
    panelD.add(btn1);

    //Añadimos el panel al frame
    frame.add(panelD);

    //Creamos el Listener del boton
    btn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            //Creamos un segundo frame
            Frame frame2 = new Frame();

            frame2.setSize(300, 300);
            frame2.setLocation(200, 200);

            frame2.setVisible(true);
        }
    });
}
    
answered by 07.09.2018 / 11:29
source