Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30531 > unrolled thread
| Started by | colmjkav@gmail.com |
|---|---|
| First post | 2014-12-09 09:52 -0800 |
| Last post | 2014-12-09 13:56 -0500 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Jbutton and JLabel colmjkav@gmail.com - 2014-12-09 09:52 -0800
Re: Jbutton and JLabel Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-09 12:53 -0500
Re: Jbutton and JLabel colmjkav@gmail.com - 2014-12-09 10:31 -0800
Re: Jbutton and JLabel Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-09 13:56 -0500
| From | colmjkav@gmail.com |
|---|---|
| Date | 2014-12-09 09:52 -0800 |
| Subject | Jbutton and JLabel |
| Message-ID | <cec907d9-e353-47a2-9684-5255e437dd1b@googlegroups.com> |
for some reason the following does not compile. Compiker complains that "the method JLabel is undefined for QuizCardBuilder". How come this is happening? I see the method in javax.swing.*.
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class QuizCardBuilder {
private JTextArea question;
public void main(String[] args) {
QuizCardBuilder qbuilder = new QuizCardBuilder();
qbuilder.go();
}
public void go() {
JLabel qLabel = JLabel("Question:");
}
}
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-12-09 12:53 -0500 |
| Message-ID | <m67cv8$nrh$1@dont-email.me> |
| In reply to | #30531 |
On 12/9/2014 12:52 PM, colmjkav@gmail.com wrote:
> for some reason the following does not compile. Compiker complains that "the method JLabel is undefined for QuizCardBuilder". How come this is happening? I see the method in javax.swing.*.
>[...]
> JLabel qLabel = JLabel("Question:");
JLabel qLabel = /* look: */ new /* see it? */ JLabel("Question:");
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | colmjkav@gmail.com |
|---|---|
| Date | 2014-12-09 10:31 -0800 |
| Message-ID | <87ba2a82-4117-4756-96e4-e7d71976eea7@googlegroups.com> |
| In reply to | #30532 |
lol. Thanks. I guess I found the compiler message a bit confusing as if it hadnt heard of JLabel.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-12-09 13:56 -0500 |
| Message-ID | <m67gjh$8u6$1@dont-email.me> |
| In reply to | #30533 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web