Java – save JPanel graphics
•
Java
I'm not sure how graphics work in Java, so I can't debug my problem
This is a quick code that will demonstrate my problem
public class MyFrame extends JPanel{ private int property; private int x; private int y; public MyFrame(int xp,int yp){ x = xp; y = yp; } @override public void paintComponent(Graphics g){ super.paintComponent(g); g.fillRect(x,y,5,5); } public void setProperty(int p){ property = p; } } public class MainClass() extends JPanel{ private ArrayList<MyFrame> frames = new ArrayList<MyFrame>; private MyFrame currentFrame = new MyFrame(); public void addFrame(int x,int y){ this.remove(currentFrame); currentFrame = new MyFrame(); this.add(currentFrame); frames.add(currentFrame); } public void setFrame(int frame){ this.remove(currentFrame); currentFrame = frames.get(frame); this.add(currentFrame); } }
Absrtact: I want the panel to display the correct frame when calling setframe Currently, when I do this, the setup framework will be blank
Solution
You seem to be exchanging JPanel into another JPanel, and when you do this, you must consider the layout used by the container -JPanel, and you need to call revalidate after reworking and remap the container -JPanel..
But why not save images by using CardLayout to swap jpanels or jlabel and imageicons instead of screwing up all this?
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码