標準の Java ユーティリティを使用して、背景画像の上に画像を重ねようとしています。下の写真を見てください...
背景画像を作成するように見えるコードがあります (実際に動作することを確認できますか?)。また、画像を表示するために使用する JPanel の拡張機能を作成しました (このクラスは ImagePanel と呼ばれます)。
ただし、プログラムを起動すると、JFrame には 2 番目の画像しか表示されず、ウィンドウのサイズが変更されると画像が移動します。
背景画像がウィンドウのスペース全体を占める状態で、最初にウィンドウを開きたいと思います。次に、指定した場所に 2 番目の画像を一番上に表示したいと思います。
import javax.swing.*;
import java.awt.*;
public class ImageTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
JPanel backgroundPanel = new JPanel();
ImageIcon backgroundImage = new ImageIcon("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
JLabel background = new JLabel(backgroundImage);
background.setBounds(0, 0, backgroundImage.getIconWidth(), backgroundImage.getIconHeight());
frame.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
backgroundPanel.setOpaque(false);
frame.setContentPane(backgroundPanel);
ImagePanel button = new ImagePanel("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\button.png");
JPanel cardContainer = new JPanel(new FlowLayout());
frame.getContentPane().add(cardContainer);
cardContainer.add(button);
cardContainer.setBounds(100, 600, 200, 200);
frame.pack();
frame.setVisible(true);
}
}