How to add a class that extends JPanel to JFrame?

26,817

Solution 1

If I understand what you're asking right... In your assignment, you're being asked to extend JPanel for your own needs. Note how you would add a JPanel if it were not being extended:

JFrame myFrame = new JFrame();
JPanel myPanel = new JPanel();
myFrame.add(myPanel);
myFrame.pack();
myFrame.setVisible(true);

This adds the JPanel to the JFrame, packs it and sets it to be visible. Since your myFrame class extends JPanel, you should be able to do something very similar by creating a new instance of your panel class and adding it to your JFrame.

You won't want to be doing this part in paintComponent(), as paintComponent() can potentially be called multiple times. Check here to see what paintComponent() does.

Solution 2

@Hyper Anthony

So it would be something similar to this?:

MyPanel Mypanel= new MyPanel();
JFrame Myframe= new JFrame();
Myframe.setSize(500,400);
Myframe.add(Mypanel);
Myframe.setVisible(true);
Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Share:
26,817
stepup.stepout
Author by

stepup.stepout

Updated on March 07, 2020

Comments

  • stepup.stepout
    stepup.stepout about 4 years

    For my assignment, I am given this piece of code:

    // This class/method uses a  global variable that MUST be set before calling/using
    // note: You can not call the paint routine directly, it is called when frame/window is shown
    // look up the repaint() routine in the book
    // Review Listings 8.5 and 8.6
    //
    public static class MyPanel extends JPanel {
     public void paintComponent (Graphics g) {
        int xpos,ypos;
        super.paintComponent(g);
        // set the xpos and ypos before you display the image
        xpos = 10; // you pick the position
        ypos = 10; // you pick the position
        if (theimage != null) {
            g.drawImage(theimage,xpos,ypos,this);
            // note: theimage global variable must be set BEFORE paint is called
        }
     }
    }
    

    My professor also says: You will also need to to look up how to create AND add a JPanel to a JFrame. If you can create AND add a JPanel, then all you need to do is substitute 'MyPanel' for the class name 'JPanel' and this code will display an image on the window frame.

    What does he mean by "then all you need to do is substitute 'MyPanel' for the class name 'JPanel' and this code will display an image on the window frame"? I'm confused as to where I'm supposed to substitute MyPanel. Here's my code:

    public static class MyPanel extends JPanel {
     public void paintComponent (Graphics g) {
        int xpos,ypos;
        super.paintComponent(g);
        JPanel panel= new JPanel();
        JFrame frame= new JFrame();
        frame.setSize(500,400);
        frame.add(panel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set the xpos and ypos before you display the image
        xpos = 600; // you pick the position
        ypos = 600; // you pick the position
        if (theimage != null) {
            g.drawImage(theimage,xpos,ypos,this);
            // note: theimage global variable must be set BEFORE paint is called
        }
     }
    }
    
  • Anthony Neace
    Anthony Neace over 11 years
    Yeah. As some general advice it is good practice to do all of your work on your frame before you set it visible, and you'll also likely want to move it elsewhere... like your main function or a method called from main. May also want to name your panel something different, if only to not confuse yourself.
  • stepup.stepout
    stepup.stepout over 11 years
    I answered my own question if you can check my updated code (I don't know how to add code text in comments). And I have to use the code given to me by my professor, otherwise I get marked down points.
  • Anthony Neace
    Anthony Neace over 11 years
    For questions like this I would recommend just updating your question with new code, as comments are a bad format for posting new code. Anyway, your professor's initial code looks fine to me, just make sure you're not trying to set your frame within your custom panel class.