简体   繁体   中英

Trying to set a background image to my Login frame

I wanted to create a Login Frame like this image( Login Frame ). I used the setBounds() method to set these components by using setBound I set the setLayout() to null. Now I want to add a background image to my login frame but as I used the setLayout() to null so I can't set the image .

`public LoginFrame()
    {

    JFrame frame=new JFrame("Login");

            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);


        frame.setLayout(null);

            uid=new JLabel("Email Id");
    uid.setBounds(60,50,120,25);
            frame.add(uid);

            tid=new JTextField(20);
        tid.setBounds(120, 50,150,24);
            frame.add(tid);

            upass=new JLabel("Password");
    upass.setBounds(53,80,120,25);
            frame.add(upass);

            tpass=new JPasswordField(20);
            tpass.setBounds(120, 80,150,24);
                frame.add(tpass);

                Login=new JButton("Login");
                Login.setBounds(150,110,80,25);
                frame.add(Login);

                    frame.setSize(370,216);
            frame.setResizable(false);
            frame.setVisible(true);

    frame.setLocationRelativeTo(null);
    JLabel background=new JLabel(new ImageIcon("C:\\Users\\Tousif\\Desktop\\Login.jpg"));
    add(background);
}

public static void main(String []arg)
{
    new LoginFrame();
}

}`

Try this, You need to use setContentPane to add background image. and you need to add the background image first then you need to add other objects like labels,textfields etc..

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginFrame{
    public LoginFrame() {
        JLabel uid, upass;
        JTextField tid;
        JPasswordField tpass;
        JButton Login;

        JFrame frame = new JFrame("Login");

        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        frame.setLayout(null);
        JLabel background = new JLabel(new ImageIcon(
                "C:\\Users\\Tousif\\Desktop\\Login.jpg"));
        frame.setContentPane(background);

        uid = new JLabel("Email Id");
        uid.setBounds(60, 50, 120, 25);
        frame.add(uid);

        tid = new JTextField(20);
        tid.setBounds(120, 50, 150, 24);
        frame.add(tid);

        upass = new JLabel("Password");
        upass.setBounds(53, 80, 120, 25);
        frame.add(upass);

        tpass = new JPasswordField(20);
        tpass.setBounds(120, 80, 150, 24);
        frame.add(tpass);

        Login = new JButton("Login");
        Login.setBounds(150, 110, 80, 25);
        frame.add(Login);

        frame.setSize(370, 216);
        frame.setResizable(false);


        frame.setLocationRelativeTo(null);

        frame.setVisible(true);
    }

    public static void main(String[] arg) {
        new LoginFrame();
    }
}

but as I used the setLayout() to null so I can't set the image .

Exactly. Don't use a null layout. Swing was designed to be used with layout managers.

As a simple example you can do something like:

JLabel background = new JLabel(...);
background.setLayout( new GridLayout(0, 2) );
background.add(label1);
background.add(textField1);
background.add(label2);
background.add(textField2);
frame.add(background);

If you don't like the layout, then user a different Layout Manager .

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