プログラムで背景画像を設定する必要があります。メインGUIの構造は次のとおりです。
含むJFrame-含むBoxLayouを含むJPanel...など
最初のJPanelの後ろに画像を配置する必要がありますが、方法がわかりません。
私はこのコードを書きました:
JPanel background = new JPanel();
JLabel labl = new JLabel("This is a dummy label this is a dummy label");
background.add(labl);
// TODO insert an image in background.
Component VERT_RA = Box.createRigidArea(new Dimension(0, 10));
Component HORI_RA = Box.createRigidArea(new Dimension(10, 0));
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
add(background);
add(main);
main.setOpaque(false);
main.add(VERT_RA);
JPanel a = new JPanel();
a.setLayout(new BoxLayout(a, BoxLayout.LINE_AXIS));
main.add(a);
main.add(VERT_RA);
a.add(HORI_RA);
JPanel services = new JPanel();
services.setLayout(new BoxLayout(services, BoxLayout.PAGE_AXIS));
a.add(services);
a.add(HORI_RA);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
a.add(right);
a.add(HORI_RA);
JLabel lbl = new JLabel("SERVIZI");
lbl.setFont(new Font("SansSerif", Font.BOLD, 30));
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
lbl.setPreferredSize(new Dimension(100, 100));
services.add(lbl);
しかし、実行すると、「メイン」のJPanel(「SERVIZI」ラベル)しか表示されません。setSize(x、y)メソッドを指定した場合にのみ、バックグラウンドJPanelを表示できます。
寸法を指定せずに、レイアウトに背景画像を追加する方法はありますか?
setLayou(null)も試してみましたが、すべてのコンポーネントの寸法を手動で指定する必要がありました(役に立ちません)。