-2

In the code I tried to move "Hotelz.jpg" to the bottom of the layer, but it does not work How do I do that?

I tried this code to add a background image, but it does not work I'm really new to Java so please explain why it isn't working

There isn't much detail I can add, As I really don't know what to do

This was closed, but the other answers isn't an answer for me

Because their code is very different from mine and the answers they received doesn't help me

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Objects;

class Login extends JFrame {
    private JTextField txtUEmail;
    private JPasswordField txtPass;

    private JButton btnLogin;
    private JButton btnReg;
            
    private JLabel lblUEmail;
    private JLabel lblPass;
    private JLabel lblAcc;
    private JLabel lblConn;
    private JLabel lblCheck; 
    private JLabel lblPic1;
    private JLabel lblPic2;
    ImageIcon icon1, icon2;
       
    private JComboBox<String> accountType;

    private Database database;

    /**
     * Constructor for objects of class Login
     */
    public Login()
    {
        
        database = new Database();         
        setLayout(new FlowLayout());         
        String[] accountTypes = { "Customer", "Manager", };
        //Creating the Combo Box and passing an array into it to display account type
        accountType = new JComboBox(accountTypes);        
        
        //Creating new JLabel objects and label values
        lblUEmail = new JLabel("Customer Email:");
        lblUEmail.setForeground(Color.white);
        lblPass = new JLabel("Password:");
        lblPass.setForeground(Color.white);
        lblAcc = new JLabel("Account Type:");
        lblAcc.setForeground(Color.white);
        lblConn = new JLabel("");
        lblConn.setForeground(Color.white);
        
        lblCheck = new JLabel("");  
        lblCheck.setForeground(Color.white);
        
        icon1 = new ImageIcon("hotelz.jpg");        
        lblPic1 = new JLabel();        
        
        lblPic1.setIcon(icon1);        
        
        //Creating new JTextField object and value
        txtUEmail = new JTextField(15);
        
        //Creating new JPasswordField object and value
        txtPass = new JPasswordField(15);     
        
        //Creating the JButton objects
        btnReg = new JButton("Register");
        btnLogin = new JButton("Login");
        //Declaring a label that we can use to provide information
        //ie. it could say "Incorrect Login Details"   
        
        //Creating a container for the components 
        Container con = getContentPane();
        //Making the container a Flow Layout
        con.setBackground(Color.gray);
        con.setLayout(new FlowLayout());
                     
        //Adding the components to the container
        con.add(lblUEmail);
        con.add(txtUEmail);
        con.add(lblPass);
        con.add(txtPass);
        con.add(lblAcc);
        con.add(accountType);
        con.add(btnReg);
        con.add(btnLogin);
        con.add(lblCheck);
        con.add(lblConn);
        con.add(lblPic1);        
        lblPic1.setOpaque(false);

        
        
        if(database.conn != null)
        {
            lblConn.setText("Successful connection to database");
        }
        else
        {
            lblConn.setText("Cannot connect to database.  Please see your Systems Administrator.");
        }           
        con.setComponentZOrder(lblPic1, 0);
        
        //Calling the buttonActions() method
        buttonActions();
        
        setVisible(true);       
        setSize(400,600);        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }
    
    //buttonActions method
    public void buttonActions() {
        //Event Handler for btnLogin
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userType = Objects.requireNonNull(accountType.getSelectedItem()).toString();
                switch (userType) {
                    case "Customer":
                        try {
                            database.statement = database.conn.createStatement();
                            database.resultSet = database.statement.executeQuery("SELECT * FROM customer");
                            
                            while (database.resultSet.next()) {
                                int customerId = database.resultSet.getInt(1);
                                String user = database.resultSet.getString(5);
                                String pass = database.resultSet.getString(7);


                                if ((user.equals(txtUEmail.getText()))&&(pass.equals(txtPass.getText()))) {
                                    JOptionPane.showMessageDialog(null, "Correct login details!");
                                    dispose();
                                    new CustomerMain(customerId);
                                    return;
                                } 
                            }                                
                               JOptionPane.showMessageDialog(null, "Incorrect login details!");
                                   
                               } catch (SQLException s) {
                                   s.printStackTrace();
                        }
                        break;
                    
                    case "Manager":
                        try {
                            database.statement = database.conn.createStatement();
                            database.resultSet = database.statement.executeQuery("SELECT * FROM manager");

                            while (database.resultSet.next()) {
                                String user = database.resultSet.getString(5);
                                String pass = database.resultSet.getString(7);

                                if ((user.equals(txtUEmail.getText()))&&(pass.equals(txtPass.getText()))) {
                                    JOptionPane.showMessageDialog(null, "Correct login details!");
                                    dispose();
                                    //new AdminMain(txtUEmail.getText());
                                    return;
                                }
                            }

                            JOptionPane.showMessageDialog(null, "Incorrect login details!");
                        } catch (SQLException s) {
                            s.printStackTrace();
                        }
                        break;
                    default:
                        break;
                }
            }
        });
        //Event handler for btnReg
        btnReg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Register();
            }
        });
    }
}


Result

  • Do you get an error? – Lajos Arpad May 03 '23 at 20:15
  • 1
    Swing components have a parent/child relationship. So you need to create a panel with the background image and add it to the frame. Then you need to add all your other components to that panel. If you add panels they need to use setOpaque(false)` .*but the other answers isn't an answer for me* - yes the other answers will help because they show the proper way to solve this problem. The problem is that you code is NOT structured properly so you need to change your code to follow the structure of the suggested answers. – camickr May 04 '23 at 02:03

1 Answers1

-1

You have to set a new container in your frame and override "paintComponent".

 JPanel container = new JPanel() {
        protected void paintComponent(Graphics g) {
            g.drawImage(icon1.getImage(), 0, 0, null);
        };
 };

 f.setContentPane(container);
BluEOS
  • 576
  • 6
  • 13
  • Your intent is correct, but your implementation is wrong on a number of levels – MadProgrammer May 03 '23 at 21:43
  • "on a number of levels". Thanks for your informative comment. – BluEOS May 04 '23 at 10:16
  • At least I left a comment - to "fix" the issues I'd have to create an entire answer just to address your issues - why not take the time look over the linked duplicates and see where your mistakes are – MadProgrammer May 04 '23 at 11:15