rendered paste bodyimport java.awt.*;
import javax.swing.*;
/* This is the class that creates the Login panel with all the components to be able to login
*/
public class LoginPanel extends JPanel {
//Two fields for username and password
private JTextField loginField = new JTextField();
private JPasswordField passField = new JPasswordField();
public LoginPanel() {
//Creates username and password labels and a login button
JButton loginB = new JButton("Login");
JLabel user = new JLabel("Användarnamn:");
JLabel pass = new JLabel("Lösenord:");
//Creates gridbagconstraints for the componentes
GridBagConstraints loginBCon = new GridBagConstraints();
GridBagConstraints passFieldCon = new GridBagConstraints();
GridBagConstraints loginCon = new GridBagConstraints();
GridBagConstraints userCon = new GridBagConstraints();
GridBagConstraints passCon = new GridBagConstraints();
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
//Sets the preferences for the components
userCon.gridx = 0;
userCon.gridy = 1;
loginCon.gridx = 0;
loginCon.gridy = 2;
loginCon.weightx = 1;
passCon.gridx = 0;
passCon.gridy = 3;
passFieldCon.gridx = 0;
passFieldCon.gridy = 4;
passFieldCon.weightx = 1;
loginBCon.gridx = 0;
loginBCon.gridy = 5;
loginBCon.weightx = 1;
passFieldCon.anchor = GridBagConstraints.CENTER;
loginCon.anchor = GridBagConstraints.CENTER;
loginBCon.anchor = GridBagConstraints.CENTER;
loginCon.insets = new Insets(0, 0, 20, 0);
loginBCon.insets = new Insets(60, 0, 0, 0);
loginField.setPreferredSize(new Dimension (100, 25));
passField.setPreferredSize(new Dimension (100, 25));
//Adds key and action listener and requests focus for usernamefield.
loginField.requestFocusInWindow();
loginField.addKeyListener(new LoginKeyListener(this));
passField.addKeyListener(new LoginKeyListener(this));
loginB.addKeyListener(new LoginKeyListener(this));
loginB.addActionListener(new LoginPanelListener(this));
loginB.setActionCommand("login");
//Adds all components to the panel
public void paintComponent(Graphics g) {
g.drawImage(image,0,0,logga.png);
super.paintComponent(g);
}
add(pass, passCon);
add(user, userCon);
add(loginB, loginBCon);
add(passField, passFieldCon);
add(loginField, loginCon);
}
public JTextField getLoginField() {
return loginField;
}
public JPasswordField getPassField() {
return passField;
}
}