I have a Jpanel and I have to call this Jpanel from a class which have the main method. I have called the jpanel method inside my main method. But it not displaying. So how to do it in netbeans?
public class A
{
public static void main(String[] args)
{
// TODO code application logic here
B b=new B();
b.caller();
}
}
Jpanel Class:
class B extends Jpanel{
public void caller()
{
initComponents();
}
}
where init component method is private.So I called this from another method inside the class which is public but nothing working. Help to solve this in netbeans
解决方案
I have called the jpanel method inside my main method. But it not
displaying.
JPanel is a generic lightweight container. It cannot be visible by itslef, you need to add it into a Window (tipically JFrame or JDialog) in order to make it visible:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new B());
frame.pack();
frame.setVisible(true);
}
});
}
class B extends JPanel {
public B() {
initComponents();
}
private void initComponents(){...}
}