Netbean Java Swing, JPanel/Jlabel Cant cover whole Frame

徘徊边缘 提交于 2019-11-27 09:54:03

Here's what I would do. Use a JPanel for the background and throw in some custom paint code.

  1. Drag a JPanel to the form and expand that to cover the whole frame, to be the background.

  2. Right click on the JPanel and select Customize Code from the context menu. You will see the following dialog. You can now edit the code.

  3. Make sure to select custom creation from the drop down and type this

    jPanel1 = new JPanel() {
        BufferedImage img;
        {
            try {
                img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png"));
            } catch (IOException ex) {  ex.printStackTrace(); }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    
        }
    };
    
  4. You probably will have to resolve imports. Just hit Ctrl + Shift + I

  5. Also you will have to change the path of the image to your path.

Here's my file structure

And here's the result

Alright guys, i have find the answer here. from the tutorial, when we are trying to make Jpanel and Jlabel to cover our whole Jframe we had to:

1) Right click at Jframe. 2) Chose Set Layout 3) Find and click Null Layout 4) Then you can edit your Jlabel to cover whole frame as i do below. thank for those who help.

p/s : i know thank is discourage in stack overflow but it hard to hold myself to do so. thanks again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!