> i can get it to work in the console, but not in a graphical
> window. the inwodw opens, but nothing happens. i added code so that it
...
> import java.awt.*;
> import javax.swing.*;
...
Don't bother learning Swing yet. Seriously, it is horrendously complicated
for a newcomer. But quite easy to pick up after you are confident with the
language. I know it is enticing but it will slow your progress, give you an
unnecessarily steep learning curve.
--
Mike W
> hi guys,
>
[quoted text clipped - 18 lines]
>
> public class whoop extends JFrame {
===============^^^
Strictly speaking, by convention the class should be called Whoop,
not whoop (note the caps). The constructor should match.
> JTextField hours = new JTextField(2);
> JTextField minutes = new JTextField(2);
[quoted text clipped - 9 lines]
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> setVisible(true);
========^^^
This command should come last, after you've added all your components
to the window, as it is the thing that will actually force your window
to evaluate itself and its contents. Its a bit wasteful asking the
window to evaluate itself before you've even started setting anything
up.
> Container window = getContentPane();
>
[quoted text clipped - 9 lines]
>
> setContentPane(window);
==========^^^
This isn't necessary. You took a reference to the content pane,
you don't need to copy it back.
> hours.setEnabled(false);
> minutes.setEnabled(false);
[quoted text clipped - 26 lines]
>
> whoop whoop = new whoop();
============^^^
Again, by convention this would be "Whoop whoop = new Whoop();" if
your class was named using standard naming practices.
But where is the call to counting() ??? You call your constructor,
but the method with your timing code never gets called. (?)
> }
> }
>
> ==========
-FISH- ><
mike - 11 May 2004 21:39 GMT
> Again, by convention this would be "Whoop whoop = new Whoop();" if
> your class was named using standard naming practices.
[quoted text clipped - 8 lines]
>
> -FISH- ><>
thanks for the advice. i'm figuring out how to call the timing method now,
and will clean up the code afterwards.
mike
Fahd Shariff - 12 May 2004 09:00 GMT
you need to call the counting method.
This can be done, for instance, by adding the statement:
whoop.counting() ;
after you create the whoop object.
Fahd
http://www.fahdshariff.cjb.net
mike - 13 May 2004 17:41 GMT
> you need to call the counting method.
>
[quoted text clipped - 6 lines]
> Fahd
> http://www.fahdshariff.cjb.net
thanks for the replies, all. i'm sorry to be a bother, but i'm still
having trouble with this. first, i completed and cleaned up the code:
==========
import java.awt.*;
import javax.swing.*;
public class StopWatch extends JFrame {
JTextField hoursfield = new JTextField(2);
JTextField minutesfield = new JTextField(2);
JTextField secondsfield = new JTextField(2);
JLabel hourslabel = new JLabel("Hours: ");
JLabel minuteslabel = new JLabel("Minutes: ");
JLabel secondslabel = new JLabel("Seconds: ");
public StopWatch() {
super("timer");
setSize(240,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
FlowLayout borders = new FlowLayout();
window.setLayout(borders);
window.add(hourslabel);
window.add(hoursfield);
window.add(minuteslabel);
window.add(minutesfield);
window.add(secondslabel);
window.add(secondsfield);
hoursfield.setEnabled(false);
minutesfield.setEnabled(false);
secondsfield.setEnabled(false);
setVisible(true);
}
public void timer() {
int secs = 0;
int mins = 0;
int hrs = 0;
String secondstring;
String minutestring;
String hourstring;
for (int seconds = 0; seconds < 61; seconds++) {
secs = seconds;
secondstring = String.valueOf(seconds);
minutestring = String.valueOf(mins);
hourstring = String.valueOf(hrs);
secondsfield.setText(secondstring);
minutesfield.setText(minutestring);
hoursfield.setText(hourstring);
if (secs == 60) {
mins++;
secs = 0;
seconds = 0;
}
if (mins == 60) {
hrs++;
mins = 0;
}
if (secs < 10 && mins < 10)
System.out.println("0" + hrs + ":0" + mins + ":0" + secs);
else if (secs >= 10 && mins < 10)
System.out.println("0" + hrs + ":0" + mins + ":" + secs);
else if (secs < 10 && mins >= 10)
System.out.println("0" + hrs + ":" + mins + ":0" + secs);
else if (secs >= 10 && mins >=10)
System.out.println("0" + hrs + ":" + mins + ":" + secs);
try {
Thread.currentThread().sleep(1000);
}
catch (Exception oops) {
oops.printStackTrace();
}
}
}
public static void main (String[] args) {
try {
UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
new StopWatch().timer();
StopWatch stopwatch = new StopWatch();
StopWatch.timer();
}
}
==========
i'm now getting an error when i try to compile:
D:\j2se\apps\StopWatch>javac StopWatch.java
StopWatch.java:100: non-static method timer() cannot be referenced from a
static context
StopWatch.timer();
^
1 error
if i change the line "new StopWatch().timer();" to, "StopWatch timer =
new timer();" or any variation thereof, i get the previous error as well
as a "cannot resolve symbol" error referencing the timer class. i believe
i need to instantiate a timer object - if i'm using the terminology
correctly - but how do i do that?
-mike
Bjorn Abelli - 13 May 2004 18:27 GMT
> i'm now getting an error when i try to compile:
>
[quoted text clipped - 4 lines]
> ^
> 1 error
With "static" means that the method isn't assigned to instances of the
class, but only to the class itself.
Hence static methods can't reference instance methods or instance variables.
In your case, you're trying to call "timer" as it would be a "static"
method, from "main" which in itself is a static method.
StopWatch.timer();
However, "timer" *isn't* a static method, but an *instance* method, i.e.
must have an instance to work on.
To get rid of that message, you could just reference the instance you just
created:
StopWatch stopwatch = new StopWatch();
stopwatch.timer();
On the other hand, why do it *twice*. The single line above those does the
same thing!
new StopWatch().timer();
This line creates a new instance of StopWatch and calls the timer method on
that instance, so you can simply get rid of the two last lines...
> if i change the line "new StopWatch().timer();" to,
> "StopWatch timer = new timer();"
> or any variation thereof, i get the previous error
> as well as a "cannot resolve symbol" error referencing
> the timer class.
With "new" you tell that you want an instance of the following class.
"new timer" would implicate that you want an instance of the "timer" class,
but you haven't defined any class with that name, hence that "symbol"
doesn't exist.
> i believe i need to instantiate a timer object -
> if i'm using the terminology
> correctly - but how do i do that?
You can't instantiate any objects from a class that don't exist. Methods
are'nt classes.
You have defined one class, StopWatch. That you have instantiated with "new
StopWatch", twice...
// Bjorn A
mike - 13 May 2004 18:46 GMT
> With "static" means that the method isn't assigned to instances of the
> class, but only to the class itself.
[quoted text clipped - 48 lines]
>
> // Bjorn A
it works now that i've commented out the last two lines! thank you very
much. i think my biggest hurdle here is going to be familiarizing myself
with the terminology -- class/method/instance/object and so forth. once i
get that down i think i'll be doing much better. your explanation was
very helpful. thanks again.
-mike
Andrew Thompson - 13 May 2004 18:38 GMT
You are so close!
change the end of the constructor to this
<snippet>
....
// important to get the correct size
pack();
setSize( getPreferredSize() );
setVisible(true);
// the fields exist now, so this can be called
timer();
}
</snippet>
..and change main to
<snippet>
public static void main (String[] args) {
try {
UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
new StopWatch().timer();
StopWatch stopwatch = new StopWatch();
// moved the call to timer to the end of constructor
}
</snippet>
That should do the trick.. If the trick
was getting a timer on screen (shrugs).

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Tony Morris - 15 May 2004 12:43 GMT
> > public whoop() {
> > super("timer");
[quoted text clipped - 5 lines]
> This command should come last, after you've added all your components
> to the window,
This command [sic] shouldn't be there at all.
Calling overridable methods from a constructor on a 'this' reference
(implied or not) is a violation and thus, is a Bad Idea(TM) Copyright (c)
2004 (Patents Pending).
Either define the methods in the subclass and declare them final, or better
still, remove business logic from the constructor, where it doesn't belong.

Signature
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)