Netbean Java Swing, JPanel/Jlabel Cant cover whole Frame
Asked Answered
S

2

0

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.

Shoot answered 6/3, 2014 at 3:24 Comment(3)
Set the layout manager of the JLabel add the other components to it...except the form editor won't let, you'll need to do it by hand...Rubberneck
im sorry for i am new to netbean and java, but where can i set the layout manager. thank MadProgrammerShoot
What you're trying to do is not easily achieved in the form editor...Rubberneck
W
4

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

Woodworm answered 6/3, 2014 at 3:49 Comment(1)
+1 and i vote your answer as my believe is getting things done by code instead of depending on platform editor is much beneficial to us for learning development.Shoot
S
0

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.

Shoot answered 6/3, 2014 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.