import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import layout.GameClient2;
public class testing extends JPanel implements ActionListener{
protected static JButton b1;
static JTextField textField2;
static JFrame frame = new JFrame("Frame");
//Start timer setup:
public static long startTime = 0;
public static long stopTime = 0;
public static boolean running = false;
public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
}
public void stop() {
this.stopTime = System.currentTimeMillis();
this.running = false;
}
public long getElapsedTime() {
long elapsed;
if (running) {
elapsed = (System.currentTimeMillis() - startTime);
}
else {
elapsed = (stopTime - startTime);
}
return elapsed;
}
public long getElapsedTimeSecs() {
long elapsed;
if (running) {
elapsed = ((System.currentTimeMillis() - startTime) / 1000);
}
else {
elapsed = ((stopTime - startTime) / 1000);
}
return elapsed;
}
//End timer setup
public static void main (String args[])
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGame();
}
});
}
public static void createAndShowGame() {
textField2 = new JTextField("0:00:00");
textField2.setEditable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);
b1 = new JButton("Start");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
try {
b1.addActionListener(new GameClient2());
} catch (Exception e) {}
contentPane.add(textField2);
contentPane.add(b1);
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, textField2,
10,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, textField2,
242,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
//SET HEIGHT/WIDTH
layout.putConstraint(SpringLayout.WEST, b1,
197,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, b1,
262,
SpringLayout.NORTH, contentPane);
//END SET HEIGHT/WIDTH
frame.pack();
frame.setSize(458, 325);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try{
GameClient2 s = new GameClient2();
if(e.getSource()==b1)
{
s.start();
//Here, these two lines of code seem to be the problem, how do I fix this so that the time in the textField2 is continuously updating?:
String get = Long.toString(s.getElapsedTime());
textField2.setText(get);
//
}
}catch(Exception e1){}
}
}