Home > Archive > Java Help > May 2004 > another newbie problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
another newbie problem
|
|
|
| hi guys,
i've been independently learning java - my first programming language -
for only about a w . i'm primarily using sun's tutorials and then
making up little related projects for myself. today i tried to write a
simple timer application that would count up the seconds, minutes, and
hours. 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
would count in the console as well, and saw that it's not counting there
anymore, either. below is what i've got so far. it's only programmed to
count the seconds right now. would anyone mind taking a look and let me
know what i've done wrong?
thanks,
mike
==========
import java.awt.*;
import javax.swing.*;
public class whoop extends JFrame {
JTextField hours = new JTextField(2);
JTextField minutes = new JTextField(2);
JTextField seconds = new JTextField(2);
JLabel hourslabel = new JLabel("Hours: ");
JLabel minuteslabel = new JLabel("Minutes: ");
JLabel secondslabel = new JLabel("Seconds: ");
public whoop() {
super("timer");
setSize(240,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container window = getContentPane();
FlowLayout borders = new FlowLayout();
window.setLayout(borders);
window.add(hourslabel);
window.add(hours);
window.add(minuteslabel);
window.add(minutes);
window.add(secondslabel);
window.add(seconds);
setContentPane(window);
hours.setEnabled(false);
minutes.setEnabled(false);
seconds.setEnabled(false);
}
public void counting() {
String secs;
for(int n = 0; n < 60; n++) {
secs = String.valueOf(n);
System.out.println(secs); //testing in console
seconds.setText(secs);
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main (String[] args) {
try {
UIManager.setLookAndFeel
(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
whoop whoop = new whoop();
}
}
==========
| |
| VisionSet 2004-05-12, 7:55 pm |
|
"mike" <uce@ftc.gov> wrote in message
news:Xns94E5B0B41D53uceftcgov@65.32.1.6...
> 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
| |
|
| mike <uce@ftc.gov> wrote in message news:<Xns94E5B0B41D53uceftcgov@65.32.1.6>...
> hi guys,
>
> i've been independently learning java - my first programming language -
> for only about a w . i'm primarily using sun's tutorials and then
> making up little related projects for myself. today i tried to write a
> simple timer application that would count up the seconds, minutes, and
> hours. 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
> would count in the console as well, and saw that it's not counting there
> anymore, either. below is what i've got so far. it's only programmed to
> count the seconds right now. would anyone mind taking a look and let me
> know what i've done wrong?
>
> thanks,
> mike
>
> ==========
>
> import java.awt.*;
> import javax.swing.*;
>
> 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);
> JTextField seconds = new JTextField(2);
>
> JLabel hourslabel = new JLabel("Hours: ");
> JLabel minuteslabel = new JLabel("Minutes: ");
> JLabel secondslabel = new JLabel("Seconds: ");
>
> public whoop() {
> super("timer");
> setSize(240,60);
> 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();
>
> FlowLayout borders = new FlowLayout();
> window.setLayout(borders);
>
> window.add(hourslabel);
> window.add(hours);
> window.add(minuteslabel);
> window.add(minutes);
> window.add(secondslabel);
> window.add(seconds);
>
> 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);
> seconds.setEnabled(false);
> }
>
> public void counting() {
> String secs;
>
> for(int n = 0; n < 60; n++) {
> secs = String.valueOf(n);
> System.out.println(secs); //testing in console
> seconds.setText(secs);
> try {
> Thread.currentThread().sleep(1000);
> }
> catch (InterruptedException e) {
> e.printStackTrace();
> }
> }
>
> }
>
> public static void main (String[] args) {
> try {
> UIManager.setLookAndFeel
> (UIManager.getSystemLookAndFeelClassName());
> }
> catch (Exception e) { }
>
> 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- ><>
| |
|
| joeking@merseymail.com (FISH) wrote in
news:dbc5020.0405110252.5ce91b22@posting.google.com:
> 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- ><>
>
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 2004-05-12, 7:55 pm |
| 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
| |
|
| fahdshariff@yahoo.com (Fahd Shariff) wrote in
news:9bc0209f.0405120000.54ccf4e9@posting.google.com:
> 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
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 2004-05-13, 2:34 pm |
|
"mike" wrote:
> 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
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
| |
| Andrew Thompson 2004-05-13, 2:34 pm |
| On Thu, 13 May 2004 16:41:32 GMT, mike wrote:
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).
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
| |
|
| Message-ID: <Xns94E88C39DF4D3uceftcgov@65.32.1.7>
User-Agent: Xnews/5.04.25
Lines: 64
Date: Thu, 13 May 2004 17:46:18 GMT
NNTP-Posting-Host: 68.201.8.209
X-Complaints-To: abuse@rr.com
X-Trace: twister.tampabay.rr.com 1084470378 68.201.8.209 (Thu, 13 May 2004 13:46:18 EDT)
NNTP-Posting-Date: Thu, 13 May 2004 13:46:18 EDT
Organization: RoadRunner - SouthWest Florida
Xref: number1.nntp.dca.giganews.com comp.lang.java.help:247146
"Bjorn Abelli" <DoNotSpam.bjorn_abelli@hotmail.com> wrote in
news:2ghpgoF2uhelU1@uni-berlin.de:
> 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...
>
>
> 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.
>
>
> 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
>
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
| |
| Tony Morris 2004-05-15, 8:31 am |
| > > public whoop() {
>
> ========^^^
> 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.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
|
|
|
|
|