Hallo!
Ich hab hier wegen dieses Programms schon öfter um Hilfe gebeten, nun ist es so weit fertig.
Bitte schaut´s euch an und probiert´s aus.
Wenn ihr Kritik zur Methodik oder zum Aufbau habt, bitte posten, dann kann ich das dementsprechend einfügen und sauber verbessern. Bitte beschränkt eure Kritik aber nicht darauf, anzugeben wo ich mir Code hätte sparen können oder was ich auch anders hätte machen können.
Wichtiger wären für mich Dinge, die ich unbedingt anders machen muß!!
Vielen Dank!
8)
Ich hab hier wegen dieses Programms schon öfter um Hilfe gebeten, nun ist es so weit fertig.
Bitte schaut´s euch an und probiert´s aus.
Wenn ihr Kritik zur Methodik oder zum Aufbau habt, bitte posten, dann kann ich das dementsprechend einfügen und sauber verbessern. Bitte beschränkt eure Kritik aber nicht darauf, anzugeben wo ich mir Code hätte sparen können oder was ich auch anders hätte machen können.
Wichtiger wären für mich Dinge, die ich unbedingt anders machen muß!!
Vielen Dank!
Code:
/** Die grafische Oberfläche des Handy Programms
Version 1.5
*/
// import the defined classes of java
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
// class extension of JFrame called Telefon with ActionListeners
public class Telefon extends JFrame implements ActionListener
{
// definition of the components used in the GUI
JTextField textfeld;
JButton eins;
JButton zwei;
JButton drei;
JButton vier;
JButton fuenf;
JButton sechs;
JButton sieben;
JButton acht;
JButton neun;
JButton stern;
JButton zero;
JButton raute;
JButton anrufen;
JButton delete;
JButton Kohle;
JButton mehrKohle;
JButton einschalten;
JButton ausschalten;
JButton telefonbuch;
JButton savemsn;
JButton enter;
JButton passwortFestlegen;
String nummer;
String zahl;
String test;
int löschen;
int laenge;
int anzahl;
int telefonnummer;
boolean eingeschaltet;
boolean ausgeschaltet;
String ausgabe;
String eingabe;
String Name;
String Telefonnummer;
// value of "guthaben" by starting the application
double guthaben = 0;
int ladezustand;
double neuguthaben;
private Container con;
String telefonbucheinträge;
String eingabeString;
JTable table;
JPasswordField Passwortfeld;
JLabel Passwortaufforderung;
String pinEingabe;
String vorPIN;
Vector telefonBuchV;
// constructor for the GUI
public Telefon()
{
super("D2-Vodafone");
eingeschaltet = false;
ladezustand = 0;
// constructing the used buttons
eins = new JButton("1");
zwei = new JButton("2");
drei = new JButton("3");
vier = new JButton("4");
fuenf = new JButton("5");
sechs = new JButton("6");
sieben = new JButton("7");
acht = new JButton("8");
neun = new JButton("9");
zero = new JButton("0");
raute = new JButton("#");
stern = new JButton("*");
anrufen = new JButton("call");
delete = new JButton("C");
Kohle = new JButton("€");
einschalten = new JButton("on");
ausschalten = new JButton("off");
ausschalten.setVisible(false);
mehrKohle = new JButton("+ 5 €");
telefonbuch = new JButton("telefonbuch");
savemsn = new JButton("save msn");
enter = new JButton("enter");
passwortFestlegen = new JButton("Set PIN!");
Passwortaufforderung = new JLabel("Bitte PIN festlegen:");
Passwortfeld = new JPasswordField("");
// constructing the Vector for "telefonbuch"
telefonBuchV = new Vector(15,2);
textfeld = new JTextField(15);
textfeld.setText("");
textfeld.setVisible(false);
// giving the buttons an explicit ActionCommand for the ActionListener
ausschalten.setActionCommand("off");
mehrKohle.setActionCommand("+ 5 €");
einschalten.setActionCommand("on");
anrufen.setActionCommand("call");
Kohle.setActionCommand("€");
delete.setActionCommand("C");
eins.setActionCommand("1");
zwei.setActionCommand("2");
drei.setActionCommand("3");
vier.setActionCommand("4");
fuenf.setActionCommand("5");
sechs.setActionCommand("6");
sieben.setActionCommand("7");
acht.setActionCommand("8");
neun.setActionCommand("9");
zero.setActionCommand("0");
stern.setActionCommand("*");
raute.setActionCommand("#");
telefonbuch.setActionCommand("t");
savemsn.setActionCommand("s");
enter.setActionCommand("enter");
passwortFestlegen.setActionCommand("set");
// using the GridBagLayout
GridBagLayout gbl = new GridBagLayout();
this.con = this.getContentPane();
this.con.setLayout( gbl );
// adding the diffrent Components to the GridBagLayout
addComponent(this.con, gbl ,this.textfeld, 0, 1, 3, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.savemsn, 0, 2, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.enter, 2, 2, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.telefonbuch,1, 2, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.ausschalten,0, 3, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.mehrKohle ,1, 3, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.einschalten,2, 3, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.anrufen, 0, 4, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.Kohle , 1, 4, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.delete , 2, 4, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.eins, 0, 5, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.zwei, 1, 5, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.drei, 2, 5, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.vier, 0, 6, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.fuenf, 1, 6, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.sechs, 2, 6, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.sieben, 0, 7, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.acht, 1, 7, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.neun, 2, 7, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.stern, 0, 8, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.zero, 1, 8, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.raute, 2, 8, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.NONE);
addComponent(this.con, gbl ,this.Passwortaufforderung, 1, 9, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.Passwortfeld, 1, 10, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
addComponent(this.con, gbl ,this.passwortFestlegen, 2, 10, 1, 1, 0.1, 0.1, GridBagConstraints.NORTH , GridBagConstraints.HORIZONTAL);
textfeld.addMouseListener( new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
textfeld.setText("");
}
});
// add the Listener for all buttons
eins.addActionListener(this);
zwei.addActionListener(this);
drei.addActionListener(this);
vier.addActionListener(this);
fuenf.addActionListener(this);
sechs.addActionListener(this);
sieben.addActionListener(this);
acht.addActionListener(this);
neun.addActionListener(this);
stern.addActionListener(this);
zero.addActionListener(this);
raute.addActionListener(this);
anrufen.addActionListener(this);
delete.addActionListener(this);
Kohle.addActionListener(this);
einschalten.addActionListener(this);
ausschalten.addActionListener(this);
mehrKohle.addActionListener(this);
telefonbuch.addActionListener(this);
savemsn.addActionListener(this);
enter.addActionListener(this);
passwortFestlegen.addActionListener(this);
}
public static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty, int anchor, int gbconst)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbconst;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.anchor = anchor;
gbl.setConstraints(c, gbc);
cont.add(c);
}
// a method to define the password for the handy
protected void handyPINdefinition()
{
vorPIN = String.valueOf(Passwortfeld.getPassword());
}
// a method to need a pin to use the Handy Object. The PIN is defined and not changeable
protected void handyPIN()
{
if ( guthaben == 0.0 )
{
pinEingabe = textfeld.getText();
if (pinEingabe.equals(vorPIN))
{ eingeschaltet = true;
ausschalten.setVisible(true);
textfeld.setVisible(true);
textfeld.setText("Korrekter PIN");
enter.setVisible( false );
einschalten.setVisible( false );
}
else
{
eingeschaltet = false;
textfeld.setText("Falscher PIN!!!");
}
}
}
// a method to dispaly the number which was called
public void nummerAnzeigen()
{
if (eingeschaltet == true)
{
eingabe = textfeld.getText();
anzahl = eingabe.length();
if (anzahl < 10)
{
textfeld.setText("Bitte Telefon-Nummer vollständig angeben!");
}
else
{
nummer = textfeld.getText();
textfeld.setText("You called: " + nummer);
}
}
else
textfeld.setText("Ihr Handy ist nicht eingeschaltet!");
}
// a method to realize a phonebook by using a vector
protected void telefonbuch()
{
String ausgabedVector;
ausgabedVector = telefonBuchV.toString();
String listData[] = { ausgabedVector };
JList jlist = new JList(listData);
JFrame frame = new JFrame();
frame.setTitle("Telefonbuch");
frame.setSize(200,200);
frame.add(jlist);
frame.setVisible(true);
}
// a method to add the numbers to the Vector
protected void TelefonBuchSpeichern()
{
String neueNummer;
if (anzahl >= 10)
{
neueNummer = textfeld.getText();
telefonBuchV.addElement(neueNummer);
textfeld.setText("");
}
else
textfeld.setText("Bitte komplette Rufnummer angeben!");
}
// a method to clear the textfield
public void nummerLöschen()
{
textfeld.setText("");
}
// a method that returns "guthaben" on textfield
private void guthaben()
{
if(eingeschaltet == true)
textfeld.setText("Ihr Guthaben beträgt: " + guthaben + " €");
else
textfeld.setText("Ihr Handy ist nicht eingeschaltet!");
}
// a method to realize "telefonieren" with reduction of "guthaben"
public void telefonieren()
{
if(guthaben >= 0.5)
{
if (anzahl > 10 && anzahl < 14)
{
if(eingeschaltet == true)
{
guthaben -= 0.5;
}
}
else textfeld.setText("Bitte die Rufnummer komplett eingeben!");
}
else textfeld.setText("Ihr Guthaben reicht nicht aus!");
}
// a method to switch the "handy" to "on"
public void einschalten()
{
if(vorPIN != null)
{
textfeld.setText("Bitte geben Sie ihren Pin ein! ");
}
else
{
textfeld.setText("Bitte erst PIN zuweisen!!!");
}
}
// a method to switch the "handy" to "off"
public void ausschalten()
{
eingeschaltet = false;
textfeld.setText("Ihr Handy ist jetzt ausgeschaltet!");
enter.setEnabled( true );
einschalten.setEnabled( true );
}
// a method to expanse "guthaben"
private void erhoeheGuthaben()
{
neuguthaben = 5.0;
guthaben = guthaben + neuguthaben;
textfeld.setText("Guthaben um 5 € erhöht");
}
// connection of the diffrent methods and the explicit ActionCommands
public void actionPerformed(ActionEvent e)
{
zahl = textfeld.getText();
// check the kind of command
if (e.getActionCommand().equals("1")) textfeld.setText(zahl + "1");
else if (e.getActionCommand().equals("2")) { textfeld.setText(zahl + "2"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("3")) { textfeld.setText(zahl + "3"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("4")) { textfeld.setText(zahl + "4"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("5")) { textfeld.setText(zahl + "5"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("6")) { textfeld.setText(zahl + "6"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("7")) { textfeld.setText(zahl + "7"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("8")) { textfeld.setText(zahl + "8"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("9")) { textfeld.setText(zahl + "9"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("*")) { textfeld.setText(zahl + "*"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("0")) { textfeld.setText(zahl + "0"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("#")) { textfeld.setText(zahl + "#"); Toolkit.getDefaultToolkit().beep(); }
else if (e.getActionCommand().equals("call")) { nummerAnzeigen(); telefonieren(); }
else if (e.getActionCommand().equals("€")) guthaben();
else if (e.getActionCommand().equals("C")) nummerLöschen();
else if (e.getActionCommand().equals("on")){ einschalten(); }
else if (e.getActionCommand().equals("+ 5 €")) erhoeheGuthaben();
else if (e.getActionCommand().equals("off")) ausschalten();
else if (e.getActionCommand().equals("enter")) handyPIN();
else if (e.getActionCommand().equals("t")) telefonbuch();
else if (e.getActionCommand().equals("s")) TelefonBuchSpeichern();
else if (e.getActionCommand().equals("set")) { handyPINdefinition();textfeld.setVisible(true);Passwortfeld.setVisible(false);passwortFestlegen.setVisible(false);
Passwortaufforderung.setVisible(false); }
}
// main method to create an "Telefon" Object
public static void main(String[] args)
{
Telefon handy1 = new Telefon();
//Telefon handy2 = new Telefon();
WindowQuitter wquit = new WindowQuitter();
handy1.addWindowListener(wquit);
handy1.setSize(350, 350);
handy1.setVisible(true);
}
}
// Window Closing Adapter
class WindowQuitter extends WindowAdapter
{
public void windowClosing(WindowEvent ex)
{
System.exit(0);
}
}
8)