Re: Jbutton and JLabel
On 12/9/2014 1:31 PM, colmjkav@gmail.com wrote:
lol. Thanks. I guess I found the compiler message a bit confusing as if it hadnt heard of JLabel.
(Context: Compiler rejected `JLabel qLabel = JLabel("Question:");')
In truth, the compiler hadn't heard of `JLabel' -- as a method
name. In the context
... = IDENTIFIER ( EXPRESSION ) ;
the IDENTIFIER must be a method name; nothing else would parse. So the
compiler looked for a method named `JLabel', failed to find one, and
told you so.
The correction I suggested (insert the `new' keyword) is almost
certainly what you intended. However, another possible correction
would have been to write a new method elsewhere in the class:
private JLabel JLabel(String title) {
return new JLabel(title);
}
This may at first look erroneous (and it certainly wouldn't survive
a code review!), but it's really the same as
private JLabel labelMaker(String title) {
return new JLabel(title);
}
.... except for the change of name. The compiler can tell from the
context which `JLabel' occurrences are type names and which are
method names.
A fundamental problem with generating diagnostic messages for an
erroneous source is that the source is, well, erroneous. There are
many correct codes at different "edit distances" from an incorrect
original, and the compiler can't read your mind to tell which of the
many possibilities you might have intended. "The" error is essentially
the difference between the flawed original and a guessed-at "nearby"
alternative, and the alternative the compiler plucks out of the cloud
may not be close to what you wanted. It is, after all, guessing --
because the original incorrect program didn't say what you meant.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM