JCash/src/me/teridax/jcash/gui/login/LoginView.java

91 lines
2.7 KiB
Java

package me.teridax.jcash.gui.login;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.text.NumberFormat;
public class LoginView extends JPanel {
private final JFormattedTextField blz;
private final JFormattedTextField iban;
private final JPasswordField pin;
private final JButton login;
public LoginView() {
this.blz = new JFormattedTextField("MA2424");
this.iban = new JFormattedTextField(getNumberFormat());
this.iban.setText("4711");
this.pin = new JPasswordField("1234");
this.login = new JButton("Login");
setLayout(new BorderLayout(16, 16));
var content = new JPanel();
this.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
add(new JScrollPane(content), BorderLayout.CENTER);
add(new JLabel("Bankautomat"), BorderLayout.NORTH);
var layout = new GridBagLayout();
var constraints = new GridBagConstraints();
content.setLayout(layout);
constraints.gridwidth = 4;
constraints.insets = new Insets(12,12,12,12);
addInputRow(constraints, content, blz, 1, "BLZ");
addInputRow(constraints, content, iban, 2, "Kontonummer");
addInputRow(constraints, content, pin, 3, "Passwort");
constraints.gridy = 4;
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.weightx = 0;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = new Insets(12,12,12,12);
content.add(login, constraints);
}
private NumberFormatter getNumberFormat() {
var format = NumberFormat.getIntegerInstance();
format.setGroupingUsed(false);
var formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);
return formatter;
}
private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, String name) {
constraints.gridwidth = 1;
constraints.gridx = 1;
constraints.gridy = row;
constraints.weightx = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
target.add(new JLabel(name, SwingConstants.RIGHT), constraints);
constraints.gridx = 2;
constraints.gridy = row;
constraints.weightx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
target.add(comp, constraints);
}
public JTextField getBlz() {
return blz;
}
public JTextField getIban() {
return iban;
}
public JPasswordField getPin() {
return pin;
}
public JButton getLogin() {
return login;
}
}