简体   繁体   中英

How to add a class that extends JPanel to JFrame?

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
    }
 }
}

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.

@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);

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