简体   繁体   English

将JPanel添加到图形面板并使JPanel透明,除了对象

[英]Add a JPanel to a Graphics Panel and making the JPanel Transparent except for objects

I am trying to make a Blackjack Game and way I want to design my program is with a graphics panel (Images, drawing of cards, etc.) and on top of that panel a JPanel with buttons. 我正在尝试制作一个二十一点游戏,我希望设计我的程序是使用图形面板(图像,绘图卡等),并在该面板上设置一个带按钮的JPanel。 I want this JPanel to be transparent so that the Graphics Panel underneath is Visible but the JButtons do not turn transparent as well. 我希望这个JPanel是透明的,以便下面的图形面板是可见的,但JButtons也不会变透明。

If someone can send me in the right direction? 如果有人可以向我发送正确的方向?

Graphic Layer: 图形层:

public class GraphicsBoard {
    String[] fileName = { "cards.png", "BlackJackBoard.png" };
    ClassLoader cl = GraphicsBoard.class.getClassLoader();
    URL imgURL[] = new URL[2];
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image imgCards, imgBG;

    public GraphicsBoard() throws Exception {
        for (int x = 0; x < imgURL.length; x++)
            imgURL[x] = cl.getResource("pictures/" + fileName[x]);
        imgCards = tk.createImage(imgURL[0]);
        imgBG = tk.createImage(imgURL[1]);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(imgBG, 0, 0, 550, 450, 0, 0, 551, 412, this);

        Graphics2D g2 = (Graphics2D) g;
        for (int x = 0; x <= 550; x += 50) {
            g2.drawLine(x, 0, x, 450);
            g2.drawString("" + x, x + 5, 20);
        }
        for (int y = 5; y <= 450; y += 50) {
            g2.drawLine(0, y, 550, y);
            g2.drawString(" " + y, 0, y + 20);
        }
    }
}

Button Layer: 按钮层:

public class OverBoard extends JPanel implements ActionListener{
    JButton btnDeal = new JButton("Deal");

    public OverBoard(){
        btnDeal.addActionListener(this);
        add(btnDeal);
        setOpaque(false);
    }
}

I want the ButtonLayer to be on top of the GraphicLayer. 我希望ButtonLayer位于GraphicLayer之上。

I want this JPanel to be transparent so that the Graphics Panel underneath is Visible but the JButtons do not turn transparent as well. 我希望这个JPanel是透明的,以便下面的图形面板是可见的,但JButtons也不会变透明。

an OverlayLayout JPanel will do what you descibe . OverlayLayout JPanel 会做你想要的

there are a few ways, proper could be to 有几种方法,适当的可能

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

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