简体   繁体   English

如何将JPanel添加到另一个具有图形的JPanel?

[英]How do you add a JPanel onto another JPanel that has graphics?

Basically what im trying to do is to add a JPanel onto another JPanel that has painted graphics on it using the paintComponent() method. 基本上,我想做的就是使用paintComponent()方法将一个JPanel添加到另一个在其上绘制了图形的JPanel上。 But the JPanel I am trying to add is not shown because it is covered up by the JPanel w/ Graphics on it. 但是我试图添加的JPanel没有显示,因为它被带有图形的JPanel所掩盖。

How can I make it so that when I add the JPanel to the one w/ graphics it will show the JPanel in the front instead of being covered up by the Graphics? 如何做到这一点,以便在将JPanel添加到一个带有图形的JPanel时,它将在前面显示JPanel而不是被Graphics遮盖?

All answers are appreciated! 感谢所有答案! :) :)

If you need the code just tell me and I will gladly put it on here. 如果您需要代码,请告诉我,我很乐意将其放在此处。

Alright here is the code: 好了,这里是代码:

 package javavideogame; public class Game extends JPanel implements ActionListener, Runnable { public Game(MainCharacter character) { setLayout(null); setFocusable(true); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(ground, 0, 0, this); g.drawImage(character.getImage(), character.getX(), character.getY(), this); g.setColor(Color.RED); g.drawRect(10, 10, character.getMaxHealth(), 10); g.fillRect(10, 10, character.getHealth(), 10); g.dispose(); } public void getInventoryScreen() { Main.inv = new Inventory(); Main.game.add(Main.inv); } } 

And here is the code for the JPanel that I'm adding to the Game JPanel 这是我要添加到游戏JPanel中的JPanel的代码

 public class Inventory extends JPanel { public Inventory() { setLayout(null); setSize(400, 300); setBackground(Color.BLACK); addKeyListener(this); setFocusable(true); } } 

Custom painting is done by overriding the paintComponent(...) method. 通过重写paintComponent(...)方法来完成自定义绘制。 My guess is that you are overriding the paint() method. 我的猜测是您要重写paint()方法。

Read this section from the Swing tutorial on Custom Painting . 从Swing教程“ 自定义绘画”中阅读本节。 Because you are overriding the wrong method, you end up painting the children first and then the custom painting is done on top. 因为您覆盖了错误的方法,所以您首先要给子代绘画,然后在顶部进行自定义绘画。

If you need more help than post your SSCCE demonstrating the problem. 如果您需要更多的帮助,而不是发布SSCCE来演示此问题。

Just a reminder: java.awt and javax.swing don't mix well. 提醒一下:java.awt和javax.swing混合不好。 When you're painting stuff on one panel AND trying to add a JPanel to it, you are bound to run into problems somewhere. 当您在一个面板上绘画东西并尝试向其添加JPanel时,您肯定会在某个地方遇到问题。 A far better solution is to have 2 different JPanels, one for your graphics, and one for your inventory. 更好的解决方案是拥有2个不同的JPanel,一个用于图形,另一个用于库存。

Something like this: 像这样:

JPanel mainPanel = new JPanel(); //will hold BOTH panels
JPanel gamePanel = new Game(myCharacter); //declare game panel
JPanel inventoryPanel = new Inventory(); //declare inventory panel

//set up some layout
mainPanel.setLayout(new GridLayout(2, 1));

//add the graphics panel, then add the inventory
mainPanel.add(gamePanel);
mainPanel.add(inventoryPanel);

This will keep your swing and awt components from mixing and save you a lot of headaches. 这将使您的挥杆和awt组件不会混合,并为您节省很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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