我正在嘗試製作21點遊戲,並且我想要設計我的程序的方式是使用圖形面板(圖像,繪圖卡等)和在該面板的頂部帶有按鈕的JPanel。我希望這個JPanel是透明的,以便下面的圖形面板是可見的,但JButtons也不會變成透明的。將JPanel添加到圖形面板並使JPanel透明(對象除外)
如果有人能把我送往正確的方向?
圖層: public class GraphicsBoard { String [] fileName = {「cards.png」,「BlackJackBoard.png」}; ClassLoader cl = GraphicsBoard.class.getClassLoader(); URL imgURL [] =新的URL [2]; Toolkit tk = Toolkit.getDefaultToolkit(); 圖片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的頂部。
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –