java: überprüfen Sie, dass alle Textfelder in der GUI sind abgeschlossen
Ich versuche, erstellen Sie ein GUI, dass jemand erlaubt, ein Konto einzurichten. Ich möchte überprüfen, dass alle text-Felder komplett sind, wenn die Schaltfläche "Konto erstellen" gedrückt wird. Was ist der beste Weg, dies zu tun? Ich füge meinen code aber meine Bestätigung, dass die text-Felder komplett nicht funktioniert.
siehe code unten:
public class GUIaccounts extends JFrame{
private JLabel name;
private JLabel initDeposit;
private JLabel chooseAccount;
private JLabel empty;
private JTextField nameTextField;
private JTextField initDepositTextField;
private JPanel dataPanel;
private JPanel accountTypePanel;
private JRadioButton savingsAccount;
private JRadioButton premiereChecking;
private ButtonGroup radioButtonGroup;
private JButton createAccountButton;
private JPanel createAccountPanel;
private Bank myBank;
public GUIaccounts(){
setTitle("Create an Account");
setSize(1000,1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanels();
setVisible(true);
pack();
setLocationRelativeTo(null);
myBank=new Bank();
}
private void buildPanels(){
//create the panels
dataPanel= new JPanel();
dataPanel.setLayout(new GridLayout(2,2));
accountTypePanel=new JPanel();
accountTypePanel.setLayout(new GridLayout(3,1));
createAccountPanel=new JPanel();
createAccountPanel.setLayout(new FlowLayout());
//create labels
name=new JLabel("Enter Name:");
Border border = LineBorder.createBlackLineBorder();
name.setBorder(border);
initDeposit=new JLabel("Enter Initial Deposit:");
initDeposit.setBorder(border);
chooseAccount=new JLabel("Choose Account:");
empty=new JLabel(" ");
//create text fields
nameTextField=new JTextField();
initDepositTextField=new JTextField();
//create buttons
savingsAccount = new JRadioButton("Savings Account");
premiereChecking =new JRadioButton ("Premiere Checking Account");
createAccountButton=new JButton("Create Account");
//add labels and field to the panel
dataPanel.add(name);
dataPanel.add(nameTextField);
dataPanel.add(initDeposit);
dataPanel.add(initDepositTextField);
//add button to the panel
accountTypePanel.add(chooseAccount);
accountTypePanel.add(empty);
accountTypePanel.add(savingsAccount);
accountTypePanel.add(premiereChecking);
createAccountPanel.add(createAccountButton);
//add actionListeners to the buttons
savingsAccount.addActionListener(new RadioButtonListener());
premiereChecking.addActionListener(new RadioButtonListener());
createAccountButton.addActionListener(new createAccountListener());
//add focus to the text field
nameTextField.addFocusListener(new nameFieldListener());
initDepositTextField.addFocusListener(new initDepositFieldListener());
//add panels to the contentPane
add(dataPanel, BorderLayout.NORTH);
add(accountTypePanel, BorderLayout.CENTER);
add(createAccountPanel, BorderLayout.SOUTH);
//group radio buttons
radioButtonGroup= new ButtonGroup();
radioButtonGroup.add(savingsAccount);
radioButtonGroup.add(premiereChecking);
}
private class createAccountListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String name=null;
Double amount=null;
if (savingsAccount.isSelected()){
name=nameTextField.getText();
amount=Double.valueOf(initDepositTextField.getText());
if(name!=null && amount !=null){
try{
SavingsAccount account=new SavingsAccount(name,AccountIDs.getNextID(), amount);
myBank.addAccount(account);
JOptionPane.showMessageDialog(null,"Account setup was successful" + myBank.toString());
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"Unable to set up account. " +e1.getMessage());
}
}
else{
JOptionPane.showMessageDialog(null, "All fields must be completed in order to set up account");
}
}
else if(premiereChecking.isSelected()){
name=nameTextField.getText();
amount=Double.valueOf(initDepositTextField.getText());
if(name!=null && amount!=null){
try{
PremiereCheckingAccount account=new PremiereCheckingAccount(name, AccountIDs.getNextID(),amount);
myBank.addAccount(account);
JOptionPane.showMessageDialog(null,"Account setup was successful" +myBank.toString());
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"Unable to set up account. " + e1.getMessage());
}
}
else{
JOptionPane.showMessageDialog(null, "All fields must be completed in order to set up account");
}
}
else{
JOptionPane.showMessageDialog(null,"Please select the type of account you want to create.");
}
}
}
private class RadioButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
private class nameFieldListener implements FocusListener{
public void focusGained(FocusEvent e){
nameTextField.selectAll();//highlight contents
}
public void focusLost(FocusEvent e){
}
}
private class initDepositFieldListener implements FocusListener{
public void focusGained(FocusEvent e){
initDepositTextField.selectAll();
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String[]args){
GUIaccounts myAccount=new GUIaccounts();
}
}
tl;dr (und ein paar chars zu tun, die minimale Länge)
InformationsquelleAutor trs | 2011-06-19
Du musst angemeldet sein, um einen Kommentar abzugeben.
Die getText () - Methode keinen Wert zurück, null. Es wird ein leerer String zurückgegeben ("").
Ihre test sollte so etwas wie:
InformationsquelleAutor camickr
Alte Frage, aber es ist generell eine gute Idee, um eine "überprüfung" Helfer-Methode, die nicht alle die Validierung zuerst, dann, wenn die Kontrollen, die Sie laden können, werden alle Werte in das, was Sie tun (Variablen, Attribute der Klasse zu instanziieren, etwas, etc).
zB:
InformationsquelleAutor blah blah