gpt4 book ai didi

java - 将 JPanel 添加到图形面板并使 JPanel 透明(对象除外)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:32 26 4
gpt4 key购买 nike

我正在尝试制作一个 Blackjack 游戏,我想设计我的程序的方式是使用图形面板(图像、卡片绘图等),并在该面板的顶部有一个带按钮的 JPanel。我希望这个 JPanel 是透明的,这样下面的图形面板是可见的,但 JButton 也不会变成透明的。

如果有人可以向我发送正确的方向?

图形层:

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);
}
}
}

按钮层:

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

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

我希望 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.

OverlayLayout JPanel will do what you descibe .

关于java - 将 JPanel 添加到图形面板并使 JPanel 透明(对象除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com