2

プログラムで背景画像を設定する必要があります。メイン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)も試してみましたが、すべてのコンポーネントの寸法を手動で指定する必要がありました(役に立ちません)。

4

3 に答える 3

4

getPreferredSize()それぞれのJPanelのメソッドをオーバーライドし、有効なディメンションオブジェクトを返すようにするだけです。私はこの例がその方向にあなたを助けるかもしれないと思います:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PaintingExample
{
    private CustomPanel contentPane;
    private JTextField userField;
    private JPasswordField passField;
    private JButton loginButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Painting Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new CustomPanel();        

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PaintingExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private BufferedImage image;

    public CustomPanel()
    {
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        try
        {
            /*
             * Since Images are Application Resources,
             * it's always best to access them in the
             * form of a URL, instead of File, as you are doing.
             * Uncomment this below line and watch this answer
             * of mine, as to HOW TO ADD IMAGES TO THE PROJECT
             * https://stackoverflow.com/a/9866659/1057230
             * In order to access images with getClass().getResource(path)
             * here your Directory structure has to be like this
             *                 Project
             *                    |
             *         ------------------------
             *         |                      |
             *        bin                    src
             *         |                      |
             *     ---------             .java files             
             *     |       |                   
             *  package   image(folder)
             *  ( or              |
             *   .class        404error.jpg
             *   files, if
             *   no package
             *   exists.)
             */
            //image = ImageIO.read(
            //      getClass().getResource(
            //              "/image/404error.jpg"));
            image = ImageIO.read(new URL(
                        "http://gagandeepbali.uk.to/" + 
                                "gaganisonline/images/404error.jpg"));
        }
        catch(IOException ioe)
        {
            System.out.println("Unable to fetch image.");
            ioe.printStackTrace();
        }
    }

    /*
     * Make this one customary habbit,
     * of overriding this method, when
     * you extends a JPanel/JComponent,
     * to define it's Preferred Size.
     * Now in this case we want it to be 
     * as big as the Image itself.
     */
    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(image.getWidth(), image.getHeight()));
    }

    /*
     * This is where the actual Painting
     * Code for the JPanel/JComponent
     * goes. Here we will draw the image.
     * Here the first line super.paintComponent(...),
     * means we want the JPanel to be drawn the usual 
     * Java way first, then later on we will
     * add our image to it, by writing the other line,
     * g.drawImage(...).
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
}

ただし、継承を使用したくない場合は、この例この例に示すように、画像をJLabelに追加してから、JLabelのレイアウトを設定してコンポーネントをJLabelに追加するだけです。

于 2012-10-03T14:08:38.670 に答える
3

OverlayLayoutを使用できます。コンポーネントを互いに積み重ねることができます。このように最も低い画像として画像を追加すると、機能するはずです。

JPanel backgroundPanel = new JPanel();
backgroundPanel.setLayout(new OverlayLayout(backgroundPanel));

backgroundPanel.add(/* your panel with controls */);
backgroundPanel.add(/* image component */);

コントロール付きのパネルは、背景画像を隠さないように透明にする必要があることに注意してください。これは、panel.setOpaque(false);を設定することで実現できます。

于 2012-10-03T12:55:27.103 に答える
0

あなたはこのようなことをすることができます:

Image image = new ImageIcon(path);     
JLabel label=new JLabel(image);

JLabelを背景画像として使用したい場合は、これでうまくいくと思います。

とにかく、ImageIconJavadocも確認することをお勧めします。

于 2012-10-03T12:42:40.303 に答える