Hallo liebes Java-Forum,
das Programm funktioniert einwandfrei jedoch möchte ich mir Tipps einholen, wie ich den Quellcode verbessern kann.
mein Projekt sieht folgendermaßen aus:
ImageShack® - Online Photo and Video Hosting
und hier ist der Code. Es sind nicht alle Klassen nur die Gui Klasse damit ich das forum nich mit einer Vielzahl an Klassen erschlage^^
Vielen Dank im Vorraus.
das Programm funktioniert einwandfrei jedoch möchte ich mir Tipps einholen, wie ich den Quellcode verbessern kann.
mein Projekt sieht folgendermaßen aus:
ImageShack® - Online Photo and Video Hosting
und hier ist der Code. Es sind nicht alle Klassen nur die Gui Klasse damit ich das forum nich mit einer Vielzahl an Klassen erschlage^^
Java:
/* Listing3702.java */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ContactManager
extends JFrame implements ActionListener, KeyListener
{
//Labels
protected JLabel labLastName;
protected JLabel labForeName;
protected JLabel labAdress;
protected JLabel labLocation;
protected JLabel labLocationNumber;
protected JLabel labLand;
protected JLabel labTelephone;
//TextFields
private JTextField txtLastName;
private JTextField txtForeName;
private JTextField txtAdress;
private JTextField txtLocation;
private JTextField txtLocationNumber;
private JTextField txtTelephone;
//ComboBox
private JComboBox boxLand;
//Buttons
private JButton btnGenerate;
private JButton btnReset;
public ContactManager()
{
super("ContactManager v.1.1");
setSize(800,300);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
buildGui();
}
public void buildGui()
{
//Layout
setLayout( new GridLayout(/*3*/ 0, 2, 6, 3) );
//Labels
labLastName = new JLabel("Nachname: ");
labForeName = new JLabel("Vorname: ");
labAdress = new JLabel("Adresse: ");
labLocation = new JLabel("Ort: ");
labLocationNumber = new JLabel("PLZ: ");
labLand = new JLabel("Land: ");
labTelephone = new JLabel("Telefonnummer: ");
//Textfields
txtLastName = new JTextField();
txtLastName.addKeyListener(this);
txtForeName = new JTextField();
txtForeName.addKeyListener(this);
txtAdress = new JTextField();
txtAdress.addKeyListener(this);
txtLocation = new JTextField();
txtLocation.addKeyListener(this);
txtLocationNumber = new JTextField();
txtLocationNumber.addKeyListener(this);
txtTelephone = new JTextField();
txtTelephone.addKeyListener(this);
//ComboBox
boxLand = new JComboBox();
for(String country : EuropeCountries.countries)
boxLand.addItem(country);
//Buttons
btnGenerate = new JButton("Weiter");
btnGenerate.setEnabled(false);
btnGenerate.addActionListener(this);
btnReset = new JButton("Reset");
btnReset.addActionListener(this);
//Nachname:
add(labLastName);
add(txtLastName);
//Vorname:
add(labForeName);
add(txtForeName);
//Adresse:
add(labAdress);
add(txtAdress);
//Ort:
add(labLocation);
add(txtLocation);
//PLZ:
add(labLocationNumber);
add(txtLocationNumber);
//Telefonnummer:
add(labTelephone);
add(txtTelephone);
//Land:
add(labLand);
add(boxLand);
add(btnGenerate);
add(btnReset);
setVisible( true );
}
public void resetTextFields()
{
txtLastName.setText("");
txtForeName.setText("");
txtAdress.setText("");
txtLocation.setText("");
txtLocationNumber.setText("");
txtTelephone.setText("");
btnGenerate.setEnabled(false);
}
public void buttonControl()
{
boolean hasValue =!txtLastName.getText().equals("") &
!txtForeName.getText().equals("") &
!txtAdress.getText().equals("") &
!txtLocation.getText().equals("") &
!txtLocationNumber.getText().equals("") &
!txtTelephone.getText().equals("");
if(hasValue){
btnGenerate.setEnabled(true);
}
else{
btnGenerate.setEnabled(false);
}
}
public Contact generateContact()
{
return new Contact(
txtLastName.getText(),
txtForeName.getText(),
txtAdress.getText(),
txtLocation.getText(),
txtLocationNumber.getText(),
txtTelephone.getText(),
boxLand.getSelectedItem().toString()
);
}
public void keyPressed(KeyEvent arg0) {
buttonControl();
}
public void keyReleased(KeyEvent arg0) {
buttonControl();
}
public void keyTyped(KeyEvent arg0) {}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(btnGenerate) ){
if(checkForDigits(txtLocationNumber.getText() ) & checkForDigits(txtTelephone.getText()) ){
new ConfirmationFrame(generateContact());
}
else
{
JOptionPane.showMessageDialog(null,"PLZ bzw. Telefonnummer enthalten Buchstaben","False",JOptionPane.ERROR_MESSAGE);
}
}
if(ae.getSource().equals(btnReset) ){
resetTextFields();
}
}
public boolean checkForDigits(String value)
{
char[] valueArray = value.toCharArray();
for(int i=0; i<valueArray.length; i++)
{
if(!(Character.isDigit(valueArray[i])||valueArray[i]=='/')){
return false;
}
}
return true;
}
}
Vielen Dank im Vorraus.