如何在JFrame中实现图像重叠?多种尝试均未解决求助
问题:JFrame中图像重叠显示失败及解决办法
我之前卡在了JFrame里实现图像重叠显示的问题上——试了单个JPanel、两个JPanel的方案都没成功,甚至用JLayeredPane也没达到预期效果。虽然涉及多个交互类,不确定代码能不能帮上忙,但还是把未完成的代码贴出来了:
import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLayeredPane; import javax.swing.ImageIcon; import java.awt.Graphics2D; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; public class MazeDisplay implements KeyListener { static JFrame window; static JPanel backPanel, userPanel; JLabel user, cpu, maze1, maze2; User u; public MazeDisplay(String x, Cell[][] maize1, Cell[][] maize2) throws IOException { maze1 = new JLabel(new ImageIcon(createMazeImage(maize1))); maze2 = new JLabel(new ImageIcon(createMazeImage(maize2))); user = new JLabel(new ImageIcon("user.png")); cpu = new JLabel(new ImageIcon("cpu.png")); u = new User(maize1); window = new JFrame(x); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setBounds(400, 400, 986, 509); backPanel = (JPanel) window.getContentPane(); backPanel.setLayout(null); maze1.setBounds(0, 0, 480, 480); maze2.setBounds(500, 0, 480, 480); backPanel.add(maze1); backPanel.add(maze2); userPanel = new JPanel(null); user.setBounds(0, 0, 30, 30); cpu.setBounds(500, 0, 30, 30); userPanel.add(user); userPanel.add(cpu); window.add(userPanel); window.setResizable(false); window.setVisible(true); } public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public boolean isLegal(int dir) { boolean ret = false; Cell[][] maze = u.getMaize(); if(0 <= u.xPos() && u.xPos() < maze.length && 0 <= u.yPos() && u.yPos() < maze.length) { if(dir == 0) ret = (maze[u.xPos()][u.yPos()].isOpen(dir) && maze[u.xPos()][u.yPos()-1].isOpenO(dir)); else if(dir == 1) ret = (maze[u.xPos()][u.yPos()].isOpen(dir) && maze[u.xPos()+1][u.yPos()].isOpenO(dir)); else if(dir == 2) ret = (maze[u.xPos()][u.yPos()].isOpen(dir) && maze[u.xPos()][u.yPos()+1].isOpenO(dir)); else if(dir == 3) ret = (maze[u.xPos()][u.yPos()].isOpen(dir) && maze[u.xPos()-1][u.yPos()].isOpenO(dir)); } return ret; } public static BufferedImage createMazeImage(Cell[][] maze) throws IOException { BufferedImage[][] iMaze = new BufferedImage[maze.length][maze.length]; for(int i = 0; i < iMaze.length; i++) for(int j = 0; j < iMaze.length; j++) { String cellDir = maze[i][j] + ".png"; iMaze[i][j] = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB); Graphics2D icon = iMaze[i][j].createGraphics(); icon.drawImage(ImageIO.read(new File(cellDir)), 0, 0, null); } int xOff = 0, yOff = 0; BufferedImage mazeImage = new BufferedImage(maze.length * 30, maze.length * 30, BufferedImage.TYPE_INT_RGB); Graphics2D g = mazeImage.createGraphics(); for(int r = 0; r < iMaze.length; r++) { for(int c = 0; c < iMaze.length; c++) { g.drawImage(iMaze[r][c], xOff, yOff, null); yOff += 30; } yOff = 0; xOff += 30; } return mazeImage; } public static void main(String[] args) throws IOException { MazeGen g = new MazeGen(16); MazeGen h = new MazeGen(16); MazeDisplay m = new MazeDisplay("Maze Game", g.getMaze(), h.getMaze()); } }
编辑:抱歉之前表述得有点模糊,当时状态很差又累又烦躁。不过还是特别感谢大家的帮助!最后我按照MadProgrammer的建议,改用GridBagLayout就顺利解决了图像重叠显示的问题。
内容的提问来源于stack exchange,提问作者androo
相关产品推荐
相关产品推荐

