Hey R-S,
I'm learning GUI - Jframes etc and i'm struggling to set the Text & Password & Button above the image so the users can interact with the screen.
How do I get the actual JTextField & JPasswordField to overlay on the top of the image?
Here is what I have so far,
Spoiler for Open Me:
As you can see I have the image loading up - But no interaction with the Login & Password + Button selection
Here is the code
EDIT: Is it to do withCode:import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginScreen extends JPanel implements ActionListener{
private static JTextField userName;
private static JLabel user;
private static JLabel pass;
private static JPasswordField passWord;
private static JButton Login;
private static JLabel success;
public static JFrame frame = new JFrame("SOTF : Login Screen");
public static void main(String[] args) {
JPanel panel = new JPanel();
frame.setSize(450, 400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
user = new JLabel("Username:");
pass = new JLabel("Password:");
user.setBounds(10, 20, 150, 25);
pass.setBounds(10, 70, 150, 25);
panel.add(user);
panel.add(pass);
userName = new JTextField(20);
userName.setBounds(80, 20, 165, 25);
panel.add(userName);
passWord = new JPasswordField(20);
passWord.setBounds(80, 70, 165, 25);
panel.add(passWord);
Login = new JButton("Login");
Login.setBounds(10, 80, 80, 25);
Login.addActionListener(new LoginScreen());
panel.add(Login);
success = new JLabel(" ");
success.setBounds(10, 110, 300, 25);
panel.add(success);
frame.setContentPane(new JLabel(new ImageIcon(Utils.getImage("LoginBGTest.png").getImage())));
//new JLabel(new ImageIcon(Utils.getImage("LoginBGTest.png").getImage()));
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String user = userName.getText();
String password = passWord.getText();
System.out.println(user +", " + password);
if (user.equals("Administrator") && password.equals("Test123")) {
success.setText("Logged in Successfully.");
//Loads up the Game.
GameLoader.main(new String[1]);
//Closes the LoginScreen
//frame.setVisible(false);
frame.dispose();
} else {
success.setText("Incorrect credentials - Please enter valid details.");
}
}
}
Quote:
frame.setContentPane(new JLabel(new ImageIcon(Utils.getImage("LoginBGTest.png").getIma ge())));
