简体   繁体   中英

Adding a JPanel to a JFrame in Netbeans

I created JFrame class in Netbeans and using generator I have add jPanel . I have also class, which extends JPanel . Now i want to create object of this class, and add him on the place where is my Panel in JFrame , but I can't find the right way, because all what I'm trying give no results

public static void main(String args[]) {
    
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            Frame f  = new Frame();
            jPanel1  = new MyPanel();
            f.pack();
            f.setVisible(true);
        }
    });

Ok, so maybe I will show more precise example what am I talking about:

first File:

class MyPanel extends javax.swing.JPanel {
public MyPanel() {
        initComponents();
        
    }
}

Another file, with Frame:

public class Frame extends javax.swing.JFrame {
public Frame() {
        initComponents();
}
public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Frame f  = new Frame();
                MyPanel p = new MyPanel();
                jPanel1 = p;
            }
        });
    }
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;

And I want to set on the place of jPanel1 - object MyPanel

Your problem: Your MyPanel instance is not added to the main frame.

Solution: Add your MyPanel instance to the main frame's content pane.

Something like:

JFrame f  = new JFrame();
MyPanel jPanel1  = new MyPanel();
f.getContentPane().add(jPanel1  );
f.pack();
f.setVisible(true);

Above answers are correct - you need to actually add this MyPanel to the contentPane of the Frame.

Additionally you should probably use Swing instead of plain AWT. The code would look like this:

SwingUtilities.invokeLater(new Runnable() {

        @Override //annotation if you are using Java >= 1.5
        public void run() {
            JFrame f = new JFrame();
            MyPanel jPanel1 = new MyPanel();
            f.getContentPane().add(jPanel1);
            f.pack();
            f.setVisible(true);
        }
    });

You need to add the panel to the frame:

f.add(jPanel1)

The following standalone code works for me :

    public static void main(String[] args)
    {

        Frame f  = new Frame();
        Panel jPanel1  = new Panel();
        jPanel1.add(new Button("Hello"));
        f.add(jPanel1  );
        f.pack();
        f.setVisible(true);
    }

JFrame jframe = new JFrame();

JPanel jpanel = new JPanel();

jframe.add(jpanel);

Eg:

   public Myclass extends JFrame{

   public Myclass(){

    this.setSize(300,300);

    this.add(new Mypanel());

     }

        class Mypanel extends JPanel{

                 // Add what u want to add in panel
              }


   }

Maybe the problem is that you have an empty JPanel, I mean, you have to use frame.add(MyPanel instance) but also you have to use (ie in MyPanel initComponents) add(jButton) or something like that..

you can also try seting the layout

setLayout(new GridLayout(1, 1)); //also in the initComponents method.

Can you show both initComponents(); methods?


EDIT:

if you're using the Netbeans editor

Right click the Palette of components

go to Palette Manager

Add from project (if it's an open project) or jar, whatever you want. You can browse your class there and look for your MyPanel file.

Then you will also select in which folder or menu must this component appear,

and then you should be able to drag it like a common JPanel.

Here is a great tutorial which teaches every thing about panel. It teaches you how to create a JFrame which contains JPanel.

Go to the Video tutorial

If you want to find out interesting videos visit this channel

Full list of Video tutorials

None of you people even understood this question. The person has their own JPanel, created previously, not in the netbeans widget designer. They are asking how you add your own custom JPanel that you already built some other time ... into a JFrame, or Frame, or whatever it is the netbeans designer uses when you create a "Frame" in the designer.

The question is, how do you add your own previously created JPanel that extends JPanel into the designer environment. And no, it's not straightforward at all. You can insert your own "custom creation code" and assign one of the designers JPanels with your own statement "= new MyExistingCustomJPanel();" and the constructor of your MyExistingCustomJPanel will indeed run, but it never paints the gui when you run it, because your subclass has been downcast to a regular old JPanel in the assignment.

So, how does one insert their own JPanel into a project that wants to use it as an object in the Netbeans Form designer environment.

Not so easy. Never have found a clear way to get this going with my own JPanels. Just some cheats that are really ugly.

All the answers here provided, excluded that the OP was trying to use their own JPanel in the Netbeans designer's "Frame" that it provides. Not straight forward at all, and nobody even understood the question.

I did, because I've struggled with this repeatedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM