1

我试图在另一个 JPanel 的顶部显示一个 Jpanel,而底部面板处于非活动状态,而我可以在顶部面板上放置图像和绘制东西。我努力了

setOpaque(false);

我试过了

Color transparent = new Color(0,0,0,0);

我有一个绘制二维面板数组的 Board 类。然后我有一个 PlayerBoard 类,它绘制一个二维数组的面板,这也是一个图形的表示。我想要透明的 PlayerBoard 以便我可以将 Player 添加到 PlayerBoard 类而无需覆盖/编辑 Board 类。

这是 Board 类的代码:

package project;

import javax.swing.*;

import java.awt.*;

@SuppressWarnings("serial")
public class Board extends JPanel {

private JPanel boardPanel; 
private Grid[][] grid;
public Board(int x, int y){



    //Create a new Player


    //Create a Panel to hold the board
    boardPanel = new JPanel();
    boardPanel.setLayout(new GridLayout(x,y,2,2));

    //Create a grid to hold each panel square
    grid = new Grid[x][y];
    for(int row = 0; row < 6; row++){

        for(int column = 0; column < 6; column++){

            grid[row][column] = new Grid(x,y,true, Color.BLACK);

            if((row==5 && column==5) || (row==4 && column==4) || (row==4 && column==5) || (row==5 && column==4) || (row==1 && column==2) || (row==1 && column==3) || (row==0 && column==2) || (row==0 && column==3) || (row==4 && column==0) || (row==5 && column==0) || (row==5 && column==1) || (row==4 && column==1)){
                grid[row][column].changeColor(Color.YELLOW);
            }
            boardPanel.add(grid[row][column]);
        }
    }

    //add boardPanel to Board
    add(boardPanel);


}



}

这是我的 PlayerBoard 课程

package project;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

@SuppressWarnings("serial")

public class PlayerBoard extends JPanel{

private JPanel boardPanel;
private Grid[][] board;
//private Player steve;

public PlayerBoard(int x, int y){

    UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);


    //Vertex of Graph
    String vertex = "";

    //Create a 2-d array of strings to hold the grid of the borad
    board = new Grid[x][y];

    //Create the boardPanel
    boardPanel = new JPanel();
    boardPanel.setLayout(new GridLayout(x,y,2,2));


    for(int i = 0; i < x; i++){

        for(int j = 0; j < y; j++){

            if(i==0){
                vertex = Integer.toString(j+1);
                board[i][j] = new Grid(i,j,true, new   Color(0,0,0,0));
                //board[i][j].changeColor(new Color(0,0,0,0));
                g.addVertex(vertex);
            }
            else{
                vertex = Integer.toString(((x*i)+(j+1)));
                board[i][j] = new Grid(i,j,true,new Color(0,0,0,0));
                //board[i][j].changeColor(new Color(0,0,0,0));
                g.addVertex(vertex);
            }

            boardPanel.add(board[i][j]);

        }
    }


    add(boardPanel);


    //Add the edges for the board graph
    g.addEdge("1", "2");
    g.addEdge("1", "7");
    g.addEdge("2", "8");
    g.addEdge("5", "11");
    g.addEdge("5", "6");
    g.addEdge("6", "12");

    g.addEdge("7", "13");
    g.addEdge("7", "8");
    g.addEdge("8", "14");
    g.addEdge("11", "17");
    g.addEdge("11", "12");
    g.addEdge("12", "18");

    g.addEdge("13", "14");
    g.addEdge("13", "19");
    g.addEdge("14", "20");
    g.addEdge("14", "15");
    g.addEdge("15", "21");
    g.addEdge("15", "16");
    g.addEdge("16", "17");
    g.addEdge("16", "22");
    g.addEdge("17", "23");
    g.addEdge("17", "18");
    g.addEdge("18", "24");

    g.addEdge("19", "20");
    g.addEdge("20", "21");
    g.addEdge("21", "22");
    g.addEdge("21", "27");
    g.addEdge("22", "28");
    g.addEdge("22", "23");
    g.addEdge("23", "24");

    g.addEdge("27", "28");
    g.addEdge("27", "33");
    g.addEdge("28", "34");



    /*
     * Print out the board
     */
    for(int i1 = 0; i1 < 6; i1++){

        for(int j = 0; j < 6; j++){

            System.out.print(board[i1][j]);
        }
        System.out.println();
    }

    System.out.println(g.toString());

    //steve = new Player("1", "bin/stuff/images/player.png");
    //add(steve);

    setOpaque(false);

}



public UndirectedGraph<String, DefaultEdge> getGraph(){

    return null;

}



}

这是我的游戏类,我希望 PlayerBoard 位于 Board 类之上。这样我就可以在不破坏棋盘类的情况下将图像放在 PlayerBoard 类上

package project;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class Game extends JFrame implements MouseListener{

private PlayerBoard playerboard;
private Board board;
private Side side;
private JPanel boardPanel, playerPanel;

public Game(){

    Color transparent = new Color (0, 0, 0, 0); 

    //create a boardPanel
    boardPanel = new JPanel();
    boardPanel.setLayout(new BorderLayout());

    playerPanel = new JPanel();
    playerPanel.setLayout(new BorderLayout());

    //Create a board that is x,y
    board = new Board(6,6);

    playerboard = new PlayerBoard(6,6);
    playerboard.setOpaque(false);

    playerPanel.add(playerboard);
    playerPanel.setBackground(transparent);

    //Create a Side Panel
    side = new Side();

    //add the side and board to Game

    boardPanel.add(board, BorderLayout.CENTER);
    playerPanel.add(playerboard, BorderLayout.CENTER);
    playerPanel.setOpaque(false);




    getContentPane().setLayout(new BorderLayout());
    getContentPane().setBackground(Color.BLUE);
    getContentPane().add(side, BorderLayout.EAST);
    getContentPane().add(boardPanel,BorderLayout.CENTER);
    getContentPane().add(playerPanel, BorderLayout.CENTER);
    pack();

    //Important
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    setVisible(true);

}




}

我在这里先向您的帮助表示感谢

4

2 回答 2

3

将顶部设置JPanel为底部的玻璃窗格,这应该覆盖整个底部:
board.setGlassPane(playerBoard);

于 2012-10-25T02:18:43.997 回答
2

这个问题还有更好的解决方案——JXLayer。如果您使用 Java 7,它已经是 Swing 的一部分,称为JLayer

于 2012-10-25T03:00:54.610 回答