Hallo liebe Community,
ich bin ein JavaME (bzw. Java) Anfänger. Nun sollte ich für die Schule
ein Programm schreiben welches einen Timer startet wodurch eine Zählervariable
immer weiter Hochgezählt wird.
Nun habe ich das Programm in 2 Klassen erstellt
TimerGUI.java und Counter.Java. Im TimerGUI wird habe ich allgemein commands
display etc. implementiert. Im Counter habe ich dann den Timer der gestartet wird
sowie get() set() Methoden.
Jetzt mein Problem: Die Ausgabe der Zählervariable Klappt in der Klasse Counter
super jedoch im TimerGUI über get garnicht. Entweder habe ich garkeine Ausgabe
oder immer nur eine 0. Für die ausgabe habe ich eine While-Schleife welche durchläuft
solange die Variable timerSt = true ist. Ich hänge hier mal die Funktionierende version
mit Ausgabe über Counter an. Ich wäre sehr über eure Hilfe erfreut.
TimerGUI
Counter
Mit freundlichen Grüßen
Preo
ich bin ein JavaME (bzw. Java) Anfänger. Nun sollte ich für die Schule
ein Programm schreiben welches einen Timer startet wodurch eine Zählervariable
immer weiter Hochgezählt wird.
Nun habe ich das Programm in 2 Klassen erstellt
TimerGUI.java und Counter.Java. Im TimerGUI wird habe ich allgemein commands
display etc. implementiert. Im Counter habe ich dann den Timer der gestartet wird
sowie get() set() Methoden.
Jetzt mein Problem: Die Ausgabe der Zählervariable Klappt in der Klasse Counter
super jedoch im TimerGUI über get garnicht. Entweder habe ich garkeine Ausgabe
oder immer nur eine 0. Für die ausgabe habe ich eine While-Schleife welche durchläuft
solange die Variable timerSt = true ist. Ich hänge hier mal die Funktionierende version
mit Ausgabe über Counter an. Ich wäre sehr über eure Hilfe erfreut.
TimerGUI
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Terror_Elch
*/
public class TimerGUI extends MIDlet implements CommandListener{
private Display disp;
private Form formMain;
Counter count = new Counter();
private Command cmdStart,cmdStop,cmdExit, cmdReset;
public static StringItem msg;
private boolean timerS;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
//</editor-fold>
public TimerGUI() {
formMain = new Form("Stopuhr");
cmdStart = new Command("Start",Command.OK,0);
cmdStop = new Command("Stop",Command.OK,0);
cmdReset = new Command("Reset",Command.OK,0);
cmdExit = new Command ("Exit",Command.EXIT,1);
formMain.addCommand(cmdStart);
formMain.addCommand(cmdExit);
formMain.setCommandListener(this);
msg = new StringItem("","Timer nicht Gestartet");
}
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
*/
private void initialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
/**
* Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
disp = Display.getDisplay(this);
disp.setCurrent(formMain);
formMain.append(msg);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
if(c==cmdExit){
destroyApp(true);
notifyDestroyed();
}
if(c==cmdStart){
formMain.removeCommand(cmdStart);
formMain.addCommand(cmdStop);
count.timerStart();
}
if(c==cmdStop){
formMain.removeCommand(cmdStop);
formMain.addCommand(cmdReset);
count.timerStop();
}
if(c==cmdReset){
formMain.removeCommand(cmdReset);
formMain.addCommand(cmdStart);
count.timerReset();
}
}
}
Counter
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Terror_Elch
*/
public class Counter extends TimerTask {
private Timer timer;
private int zaehler;
private int interval = 1000;
synchronized public int getZaehler(){
return this.zaehler;
}
public void setZaehler(int z){
this.zaehler = z;
}
public void timerStart(){
timer = new Timer();
timer.schedule(new Counter(), interval, interval);
}
public void timerStop(){
timer.cancel();
timer = null;
}
public void timerReset(){
zaehler = 0;
TimerGUI.msg.setText(""+zaehler);
}
public void run(){
zaehler++;
TimerGUI.msg.setText(Integer.toString(zaehler));
}
}
Mit freundlichen Grüßen
Preo
Zuletzt bearbeitet von einem Moderator: