Question

I got a background on C drive but its not showing up even though the code looks fine. It can run but there are a couple of problems.

1: Background doesnt show (You can download a random bg.jpg file to see if it works)

2: Text boxes are not centered, sadly...

3: I cant get texts to show beside the text boxes, like "User / PW" or "Welcome" or something.

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

public class Log extends JFrame {
JButton b1;
JLabel l1;

public static void main(String[] args) {
Log frameTabel = new Log();
}

JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);

Log(){
super("Login Autentification");
Toolkit tk = Toolkit.getDefaultToolkit();  
int xSize = ((int) tk.getScreenSize().getWidth());  
int ySize = ((int) tk.getScreenSize().getHeight());  


setSize(xSize,ySize);
setLocationRelativeTo(null);
panel.setLayout (null); 
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\bg.jpg"));
add(background);
background.setLayout(new FlowLayout());


txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
blogin.setBounds(110,100,80,20);

panel.add(blogin);
panel.add(txuser);
panel.add(pass);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}

public void actionlogin(){

    blogin.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent ae) {

String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("test") && ppaswd.equals("12345")) {
newframe regFace =new newframe();
regFace.setVisible(true);
dispose();
} else {

JOptionPane.showMessageDialog(null,"Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}

}
});
}
}
Was it helpful?

Solution

This should be your code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;
import java.awt.Graphics;

public class Log extends JFrame {
JButton b1;
JLabel l1;
Image bgImage;
JLabel user = new JLabel("User");

JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
public static void main(String[] args) {
    Log frameTabel = new Log("bg.jpg");
}

Log(String bgImg){
    super("Login Autentification");
    Toolkit tk = Toolkit.getDefaultToolkit();  
    int xSize = ((int) tk.getScreenSize().getWidth());  
    int ySize = ((int) tk.getScreenSize().getHeight());  
    try{
        bgImage = ImageIO.read(new File(bgImg));
    }
    catch(IOException e){}

    setSize(xSize,ySize);
    setLocationRelativeTo(null);
    panel.setLayout (null); 
    setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("C:\bg.jpg"));
    add(background);
    background.setLayout(new FlowLayout());


    txuser.setBounds(Math.round(xSize/2) - 75,30,150,20);
    pass.setBounds(Math.round(xSize/2) - 75,65,150,20);
    blogin.setBounds(Math.round(xSize/2) - 40,100,80,20);

    user.setBounds(Math.round(xSize/2) - 150, 30, 50, 20);

    panel.add(user);
    panel.add(blogin);
    panel.add(txuser);
    panel.add(pass);

    getContentPane().add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
public void paint(Graphics g){
    super.paint(g);
    g.drawImage(bgImage, 0, 0, panel);
}
}

Maybe you're asking yourself "why did he used xSize and substracted 75 or 40?" well, I divided it in two so we're in half of the window, now we substract half of the width of Textboxes so we have half width to the left and half to the right.

For text of User, I just added JLabel and added it to the panel.

Image background I tooked it from here

I suggest you to think a bit more what you want and what you have, search in the web, 'cause there's mostly of times an answer to your questions.

Anyway you'll get something like this:

enter image description here

(Label of "user" is hidden under the image, comment void paint method to get it to show, well, you get background image and labels, all missing is fixing that bug)

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top