将图像添加为jframe背景 - 其他jcomponents消失

时间:2013-12-02 12:33:20

标签: java image swing

我正在尝试添加图像作为jframe的背景。但是,当我运行下面的代码时,其余的面板不会显示,只显示图像。我需要将图像放在后面,其余的jcomponents显示在前面。此外,图像不会调整为jframes大小,但会保持不变。有没有什么办法解决这一问题? 为了便于阅读,我只使用jbutton init演示了一个jpanel。 使用的图像是这个:

  

http://wallpoper.com/wallpaper/black-background-metal-hole-444015

提前致谢

    public class bcquery extends JPanel implements ActionListener {



        public bcquery() {


setLayout(new BorderLayout());

        JPanel imagepnl = new JPanel(new BorderLayout());

        File file = new File(".jpg");           
        JLabel labelimg;
        try {
            labelimg = new JLabel(new ImageIcon(ImageIO.read(file)));
            imagepnl.add(labelimg, BorderLayout.CENTER);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

add(imagepnl, BorderLayout.CENTER);


     JPanel mainpanel = new JPanel();
            mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.PAGE_AXIS));

    JButton button1 = new JButton("OK");
    JPanel btnpanel = new JPanel(new FlowLayout());
    btnpanel.add(button1);

        btnpanel.setOpaque(false);
        mainpanel.setOpaque(false);

        mainpanel.add(btnpanel);
        imagepnl.add(mainpanel, BorderLayout.NORTH);
add(imagepnl, BorderLayout.CENTER);



    private static void createAndShowGUI() throws IOException {
                //Create and set up the window.

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setSize(800, 800);
                JFrame.setDefaultLookAndFeelDecorated(true);
                //Add content to the window.
                frame.add(new bcquery());
                frame.setResizable(true);           

                //Display the window.

                frame.pack();
                frame.setVisible(true);
            }

            public static void main(String[] args) {
                //Schedule a job for the event dispatch thread:
                //creating and showing this application's GUI.
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                //Turn off metal's use of bold fonts
                    UIManager.put("swing.boldMetal", Boolean.TRUE);


                try {
                    createAndShowGUI();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }
                });

        }
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

            }
    }

1 个答案:

答案 0 :(得分:1)

  

其余的面板不会出现,只有图像。

不正确,因为你将苹果与香蕉混合

  • JFrame已在API BorderLayoutJPanel实施了FlowLayout,最后添加JComponentJFrame为{ {1}}

  • (在此代码行之前)将同一区域frame.add(new bcquery());添加到JFrame.CENTERJLabel


  • 有两种方式

    1. frame.setContentPane(label);置于JPanelJFrame.CENTER)覆盖frame.add(myVariable),将paintComponent添加到Image,(因为{{ 1}}在这种情况下无法调整大小)你也可以停止为JPanel修复

    2. JFrame / JPanel置于Icon,为ImageIcon设置JFrame(似乎为LayoutManager)然后放置其余的JLabel

    3. 在这两种情况下,BorderLayout Swing JComponents setOpaque(false)添加JComponent JPanel / paintComponent JLabel Icon } / ImageIcon