#Problem with the overlap between the background and a panel

1 messages · Page 1 of 1 (latest)

woven oriole
#

in this code, "loginPanel" dont respect the dimensions i gave to it(300x450), but it fits in all the screen like the backgroundLabel.
so then the loginPanel goes to cover the background.
this is the code:

package gui;

import javax.swing.*;
import java.awt.*;

public class LoginGUI extends JFrame {

    private static JLabel backgroundLabel;
    public LoginGUI() {

        super("LoginApp2");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        backgroundImage();
        loginPanel();
    }

    public void backgroundImage() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();

        ImageIcon backgroundImage = new ImageIcon("C://Users//***//Downloads//Applicazione4BK.png");
        Image img = backgroundImage.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
        backgroundLabel = new JLabel(new ImageIcon(img));
        add(backgroundLabel, BorderLayout.CENTER);
    }

    public void loginPanel(){
        JPanel blurPanel = new JPanel();
        blurPanel.setPreferredSize(new Dimension(30, 50));
        blurPanel.setBackground(new Color(11, 192, 239, 0));
        add(blurPanel, BorderLayout.CENTER);
        
    }
}```
fringe peakBOT
#

<@&987246487241105418> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

issue is that the "loginPanel" is not respecting the dimensions you have set for it (300x450) and instead it is fitting to the entire screen like the "backgroundLabel".

There could be a few reasons why this is happening. Here are some possible solutions:

  1. Check if there are any constraints or autoresizing masks applied to the "loginPanel" that might be causing it to resize automatically. Make sure to remove any conflicting constraints or adjust the autoresizing masks accordingly.

  2. Verify that you are setting the correct frame or bounds for the "loginPanel". Double-check your code to ensure that you are setting the dimensions correctly.

  3. If you are using Auto Layout, make sure that you have set up the constraints properly for the "loginPanel". Check if there are any conflicting or ambiguous constraints that might be causing it to resize incorrectly.

  4. If you are using a container view controller or embedding the "loginPanel" within another view, make sure that the container view's dimensions are also set correctly and not conflicting with the "loginPanel".

  5. If none of these solutions work, try debugging by printing out the frame or bounds of both the "backgroundLabel" and "loginPanel" at different stages of your code execution. This can help identify any unexpected changes in their dimensions.

By troubleshooting these potential issues, you should be able to fix the problem with the overlap between the background and panel in your code.

eternal pagoda
#

Do you want the dimensions of it to be fixed?

woven oriole
#

ye, i suppose that the dimensions i gave to loginPanel dont work coz it interfere with setExtendedState(JFrame.MAXIMIZED_BOTH)

eternal pagoda
#

you can use setMaximumSize() then i would think

#

if you are ok with itt being fixed

#

or just setSize i guess

woven oriole
#

i tried add that blurPanel.setMaximumSize(new Dimension(30, 50)); as u suggested. but it still does not work.

#

it still covers the entire screen, covering the background

eternal pagoda
#

also with setSize()?

woven oriole
#

as i said i think setExtendedState(JFrame.MAXIMIZED_BOTH) interfere with this loginPanel and create this problem. but i am not 100% sure

eternal pagoda
#

i am not sure what that does

woven oriole
eternal pagoda
#

Just looked it up in the documentation, setExtendedState(JFrame.MAXIMIZED_BOTH) just maximizes your frames size

#

so im not sure what the problem is

woven oriole
#

ok.. hope some helper will find it

woven oriole
#

.

woven oriole
#

import javax.swing.*;
import java.awt.*;

public class LoginGUI extends JFrame {

    private static JLabel backgroundLabel;
    public LoginGUI() {
        super("LoginApp2");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        setExtendedState(MAXIMIZED_BOTH);
        loginPanel();
        backgroundImage();
    }
    public void backgroundImage() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();
        ImageIcon backgroundImage = new ImageIcon("C://Users//***//Downloads//Applicazione4BK.png");
        Image img = backgroundImage.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
        backgroundLabel = new JLabel(new ImageIcon(img));
        add(backgroundLabel, BorderLayout.CENTER);
    }
    public void loginPanel(){
        //add(loginPanel(), BorderLayout.CENTER);
        JPanel blurPanel = new JPanel();
        blurPanel.setSize(new Dimension(300, 450));
        blurPanel.setBackground(new Color(11, 192, 239, 203));
       // blurPanel.setPreferredSize(new Dimension(300, 450));
        add(blurPanel, BorderLayout.CENTER);
    }
}```
fringe peakBOT
woven oriole
#

now the blurPanel appear but in the up-left side of the screen(it seems a default position). also if i wrote in center

woven oriole
#

@eternal pagoda hey i found the solution

eternal pagoda
#

nice

woven oriole
#

basically in my code i wasnt giving the position of blurPanel in a own line, i just was doing a "add(blurPanel, BorderLayout.CENTER);"
and that made an infinitive recursion. so i added java blurPanel.setLocation( 480,100); with the "x" of of the number /2 of my pc screen and for the Y was a different thing

woven oriole
eternal pagoda
#

looks good

woven oriole
#

thanks

#

im going to make a log in/sign up

#

and then maybe a calculator or idk what else uk

eternal pagoda
#

cool