문제

I want to make my JLabel cover the whole frame but when i do so it wont cover the text field and ok button. how do i make my JLabel to cover whole frame content.

enter image description here

P/s I want to make this jlabel as a background so i can put my icon in jlabel as background picture.

도움이 되었습니까?

해결책

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.

    enter image description here

  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

enter image description here

And here's the result

enter image description here

다른 팁

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.

enter image description here

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top