Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

bitte nicht gleich wieder auf mir rumhacken wenn ich was falsches sag oder so. Brauch wirklich Hilfe, weil ich einfach nicht weiter komme und ich schon so lange an dem Problem sitze.

Eigentlich ist es ja ganz einfach, aber ich bekomme es einfach nicht hin. :(

Ich möchte diesen Code (Für eine Stopuhr) Kapseln und in eine GUI-Klasse und eine Stopuhr-Klasse mit der eigentlichen Funktion aufteilen. Aber ich bekomme es einfach nicht hin. Was mache ich falsch? Von Threads habe ich leider keine Ahnung. Die anderen 2 Codebeispiele sind meine bisherigen Versuche.

Eigentlicher Code:


import java.awt.*;

import java.awt.event.*;

public class StopUhr_all extends Frame {


	private Button start = new Button("Start");

	private Button stop = new Button("Stop");

	private Label ausgabe = new Label();

	private boolean running = false;


	public StopUhr_all() {

		super();

		setLayout(null);

		setTitle("Stoppuhr");


		start.setBounds(100, 100, 100, 50);

		stop.setBounds(220, 100, 100, 50);

		ausgabe.setBounds(130, 175, 250, 50);


		add(start);

		add(stop);

		add(ausgabe);

		ausgabe.setFont(new Font(Font.SERIF, Font.PLAIN, 35));


		start.addActionListener(new ActionListener() {


			@Override

			public void actionPerformed(ActionEvent e) {


				if (!running) {

					running = true;

					UhrzeitThread uhr = new UhrzeitThread();

					uhr.start();

				}


			}

		});


		stop.addActionListener(new ActionListener() {


			@Override

			public void actionPerformed(ActionEvent e) {

				running = false;


			}

		});


		addWindowListener(new WindowListener() {


			@Override

			public void windowClosing(WindowEvent e) {


				running = false;

				System.exit(0);

			}


			@Override

			public void windowOpened(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowIconified(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowDeiconified(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowClosed(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowActivated(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowDeactivated(WindowEvent e) {

				// TODO Auto-generated method stub


			}

		});


	}


	class UhrzeitThread extends Thread {

		private int ms = 0;

		private int sek = 0;

		private int min = 0;

		private int h = 0;


		public void run() {

			while (running) {

				try {

					Thread.sleep(9);

				} catch (Exception e) {

				}

				if (ms <= 99) {

					ms++;

				} else {

					ms = 0;

					if (sek < 59) {

						sek++;

					} else {

						sek = 0;

						if (min < 59) {

							min++;

						} else {

							min = 0;

							h++;

						}

					}

				}

				ausgabe.setText(h + " : " + min + " : " + sek);

			}

		}

	}


	public static void main(String[] args) {

		StopUhr_all uhr = new StopUhr_all();

		uhr.setBounds(0, 0, 500, 300);

		uhr.setVisible(true);


	}

Mein Code:

package gui;


import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;


import app.TEST;


@SuppressWarnings("serial")

public class GUI extends JFrame {


	private JButton start = new JButton("Start");

	private JButton stop = new JButton("Stop");

	private JLabel ausgabe = new JLabel();


	TEST test = new TEST();


	public GUI() {

		super();

		setLayout(null);

		setTitle("Stoppuhr");


		start.setBounds(100, 100, 100, 50);

		stop.setBounds(220, 100, 100, 50);

		ausgabe.setBounds(130, 175, 250, 50);


		add(start);

		add(stop);

		add(ausgabe);

		ausgabe.setFont(new Font(Font.SERIF, Font.PLAIN, 35));


		start.addActionListener(new ActionListener() {


			@Override

			public void actionPerformed(ActionEvent e) {


				if (!test.running) {

					test.running = true;

					test.start();

				}


				ausgabe.setText(test.getH() + " : " + test.getMin() + " : "

						+ test.getSek());


			}

		});


		stop.addActionListener(new ActionListener() {


			@Override

			public void actionPerformed(ActionEvent e) {

				test.running = false;


			}

		});


		addWindowListener(new WindowListener() {


			@Override

			public void windowOpened(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowIconified(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowDeiconified(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowDeactivated(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowClosing(WindowEvent e) {


				test.running = false;

				System.exit(0);

			}


			@Override

			public void windowClosed(WindowEvent e) {

				// TODO Auto-generated method stub


			}


			@Override

			public void windowActivated(WindowEvent e) {

				// TODO Auto-generated method stub


			}

		});


	}


	public static void main(String[] args) {

		GUI uhr = new GUI();

		uhr.setBounds(0, 0, 500, 300);

		uhr.setVisible(true);


	}

}



package app;


public class TEST extends Thread {


	private int ms = 0;

	private int sek = 0;

	private int min = 0;

	private int h = 0;

	public boolean running = false;


	public void run() {


		while (running) {

			try {

				Thread.sleep(9);

			} catch (Exception e) {

			}

			if (ms <= 99) {

				ms++;

			} else {

				ms = 0;

				if (sek < 59) {

					sek++;

				} else {

					sek = 0;

					if (min < 59) {

						min++;

					} else {

						min = 0;

						h++;

					}

				}

			}

		}


	}


	public int getMs() {

		return ms;

	}


	public void setMs(int ms) {

		this.ms = ms;

	}


	public int getSek() {

		return sek;

	}


	public void setSek(int sek) {

		this.sek = sek;

	}


	public int getMin() {

		return min;

	}


	public void setMin(int min) {

		this.min = min;

	}


	public int getH() {

		return h;

	}


	public void setH(int h) {

		this.h = h;

	}

}


Ich bin schon total zittrig weil ich es einfach nicht hinbekomme, also bitte nicht kritisieren wenn irgendwo (Tipp)-Fehler sind.

Danke schon mal

Grüße

Ohne jetzt Deinen Code gelesen zu haben: worin besteht jetzt Dein Problem?

Im Grunde benötigtst Du eine Klasse, die eine Start, eine Stop und eine Reset-Methode besitzt; ggf. noch eine Pause-Methode. Als nächstes benötigst Du eine Callbackfunktion die im Zeitintervall x aufgerufen wird. Dieser Callbackfunktion übergibst Du die aktuelle Zeit als Parameter. Ob dann ein System.out.println() dranklebt oder die GUI aktualisiert wird, braucht die Klasse dann nicht zu stören. Wer sich als Konsument registriert, wird von dem Event benachrichtigt. Fertig.

Um einen Timer zu entwickeln, was letztendlich eine Stopuhr ist, brauchst Du Threads. Zeitgesteuert wäre das ganze via TimerTask zu erreichen https://rz-static.uni-hohenheim.de/anw/programme/prg/java/tutorials/javainsel4/javainsel_09_012.htm#Rxx365java09012040002D91F04A100

d.h. Dein Counter der Uhr wird jede Sekunde aktualisiert. Natürlich kann man das auch vereinfacht wie lilith2k3 schrieb machen, aber sobald während die Uhr läuft andere Prozesse aktiv sind und das System belasten, wird Deine Stoppuhr nicht korrekt arbeiten. Dies lässt sich eben nur durch Nebenläufigkeit lösen. Ich würde eine Stopuhrklasse von TimeTask ableiten und dort eben passende Methoden implementieren.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.