Citat:
Ja så vad är problemet?Har lite kod till ett tidtagarur som jag hittade i en övningsbok. Jag är ganska ny på Java så om någon har gjort en liknande kanske kan visa hur man verkligen får igång klockan när man trycker på start. Är inte alls med på hur den metoden skall skrivas. Här är klasser för själva klockan samt GUI och en main.
Här är stegen som ska uppfyllas. Har läst om trådar så är införstådd hur det fungerar.
-When the start button is pressed. Create a new thread that do the following in the
run method. Sleep for 100ms and call the appropriate method in a stopwatch object.
Run this as an infinite loop until stop has been pressed.
- After each clock tick update the time of the stopwatch.
- When stop is pressed, the timer thread should stop. Note that the proper way to
stop a running thread is to interrupt it and then let the thread exit in a controlled way.
Never use terminate operation if you can avoid it.
- When reset is pressed the time should be set to 0:0:0.0 again.
Här är stegen som ska uppfyllas. Har läst om trådar så är införstådd hur det fungerar.
-When the start button is pressed. Create a new thread that do the following in the
run method. Sleep for 100ms and call the appropriate method in a stopwatch object.
Run this as an infinite loop until stop has been pressed.
- After each clock tick update the time of the stopwatch.
- When stop is pressed, the timer thread should stop. Note that the proper way to
stop a running thread is to interrupt it and then let the thread exit in a controlled way.
Never use terminate operation if you can avoid it.
- When reset is pressed the time should be set to 0:0:0.0 again.
Kod:
import java.awt.EventQueue; import javax.swing.JFrame; public class StopWatch { private WatchGUI myGui; private int hours; private int min; private int sec; private int dsec; //1/10 second public StopWatch(){ //initiate time hours=0; min=0; sec=0; dsec=0; //create GUI myGui = new WatchGUI(this); EventQueue.invokeLater(() -> { myGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myGui.setVisible(true); }); } public void start(){ System.out.println("start"); } public void stop(){ System.out.println("stop"); } public void reset(){ System.out.println("reset"); } public void tick(){ System.out.println("tick"); } }
Kod:
import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.*; public class WatchGUI extends JFrame{ private StopWatch myWatch; //need a reference to signal input private JTextArea textArea; //write status text private JLabel timeLabel; //the time public WatchGUI(StopWatch sWatch){ myWatch=sWatch; setTitle("StopWatch"); setSize(500,300); //ctreate buttons to control stopwatch JButton startButton=new JButton("Start"); JButton stopButton=new JButton("Stop"); JButton resetButton=new JButton("Reset"); JPanel buttonPanel = new JPanel(); buttonPanel.add(startButton); buttonPanel.add(stopButton); buttonPanel.add(resetButton); add(buttonPanel,BorderLayout.NORTH); //add listener to each button GUIListener startAction= new GUIListener("Start"); startButton.addActionListener(startAction); GUIListener stopAction= new GUIListener("Stop"); stopButton.addActionListener(stopAction); GUIListener resetAction= new GUIListener("Reset"); resetButton.addActionListener(resetAction); //add a textArea to print status JPanel tPanel= new JPanel(); textArea = new JTextArea(10,44); tPanel.add(textArea); add(tPanel,BorderLayout.SOUTH); timeLabel=new JLabel("00:00:00.0"); timeLabel.setFont(new Font("Serif", Font.PLAIN, 48)); JPanel timePanel= new JPanel(); timePanel.add(timeLabel); add(timePanel,BorderLayout.CENTER); } public void writeStatus(String text){ textArea.append(text); } public void writeTime(String time){ timeLabel.setText(time); } private class GUIListener implements ActionListener { private String text; public GUIListener(String message){ text=message; } @Override public void actionPerformed(ActionEvent event){ //notify in status that a button is clicked writeStatus(getCurrentTimeStamp()); writeStatus(text); writeStatus(" button pressed\n"); //notify my stopwatch that a button ha been clocked if(text=="Start") myWatch.start(); if(text=="Stop") myWatch.stop(); if(text=="Reset") myWatch.reset(); } public String getCurrentTimeStamp() { SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//dd/MM/yyyy Date now = new Date(); String strDate = sdfDate.format(now); strDate+=": "; return strDate; } } }
Kod:
public static void main(String[] args) { //create stopwatch StopWatch swatch= new StopWatch(); }
Du skriver att du vet hur trådar funkar, ändå finns det ingen kod för att lägga stoppuret i sin egen tråd.
Ditt GUI kommer troligen behöva implementera interfacet Runnable för att kunna starta en tråd, eftersom det redan ärver från en jframe, läs på om hur du skapar trådar så bör det framgå tydligare vad du behöver göra.