简体   繁体   中英

Adding image as jframe background - other jcomponents disappear

I am trying to add an image as background for the jframe. However when I run the code below, the rest of the panels wont show up, only the image. I need the image to be on the backwards, and the rest of the jcomponents showing in front. Plus the image wont be resized to the jframes size, but will remain as it is. Is there any way to fix this? To make it easier to read I only demonstrate one of the jpanels with a jbutton init. The image used is this one:

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

Thanks in advance

    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

            }
    }

the rest of the panels wont show up, only the image.

not true because you mixing apples with bananas

  • JFrame has BorderLayout implemented in API ( JPanel has FlowLayout ), then last added JComponent to JFrame is frame.add(new bcquery());

  • (before this code line) to the same area JFrame.CENTER is added JLabel to frame.setContentPane(label);


  • there are two ways

    1. put JPanel to JFrame.CENTER ( frame.add(myVariable) ) override paintComponent there to add Image to JPanel , (because JFrame isn't resizable in this case) you can to stop repaiting for JPanel too

    2. put Icon / ImageIcon to JFrame , set LayoutManager for JLabel (seems like as BorderLayout ) then there put rest of Swing JComponents

    3. in both cases you would have to play with setOpaque(false) for JComponent added to JPanel with paintComponent / JLabel with Icon / ImageIcon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM