JPanel with setvisible = true is not displayed when I call it from the Main ()

0

I'm making a graphical interface to a program that I have. It asks for several data to the user, he / she fills them, and with that data uses a function that registers the data in a file. I have no problem to see how the JPanel is from the Designer:

I have already done the actionlistener and everything, but when from the main I call the JPanel, nothing comes out. My JPanel class is:

public class Menu extends JPanel implements ActionListener

What you use:

public Menu() {

And there is everything. But when from the main I do:

Menu algo = new Menu();
    algo.setVisible(true);

Compile without problems, start, but nothing comes out at all ... and in the console it says "application finished" ...

Ideas?

    
asked by Keka Bron 22.04.2017 в 11:56
source

2 answers

1

Good we go from the beginning it seems that you designed your panel from the designer that gives netbeans or eclipse but still it is still a classic jpanel therefore it needs a JFrame to be able to visualize itself.

In your case you can create the JFrame from the designer or from code, in this case I will do it from code and I will add the panel that you have previously created.

public class WMain {
    public static void main(String[] args) {

        Menu menu=new Menu();
        JFrame mainWindow=new JFrame("Menu sample");

        mainWindow.setLayout(new BorderLayout());
        mainWindow.add(menu);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
        mainWindow.pack();
    }
}
    
answered by 22.04.2017 / 17:56
source
0

The size of your component needs to be added, because without size, nothing will be displayed.

ejemplo
JFrame frame=new JFrame();
frame.setbounds(poX,posY,tamX,tamY);

note: you may also need to use a layout for the organization of the elements

    
answered by 22.04.2017 в 15:06