Well, I'm new to this and I've got a book. I did quite some lessons and I liked it, everything was going fine until something new started.
I'm using textpad
the code which is exactly the same (atleast, it looks the same) as my book says
The code I got right now :
[code]import java.awt.*;
import javax.swing.*;
public class Greeting {
public static void main (String[] arg) { // start of program
Welcome2 w2 = new Welcome2(); // create a Welcome2 object
}
}
class Welcome2 extends JFrame {
public Welcome2() { // Constructor, called automatically
JLabel 1 = new JLabel("Welcome to Java", JLabel.CENTER);
getContentPane().add(1); // put 1 in the window
1.setOpaque(true); // opaque background
1.setBackground(color.yellow);
1.setForeground(color.blue);
1.setFont(new Font("SansSerif", Font.Bold, 24));
setSize(400,150); // the size of the window
setVisible(true); // make the window visible
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}[/code]
this is the error list I get after compiling
[code]C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:12: not a statement
JLabel 1 = new JLabel("Welcome to Java", JLabel.CENTER);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:12: ';' expected
JLabel 1 = new JLabel("Welcome to Java", JLabel.CENTER);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:14: not a statement
1.setOpaque(true); // opaque background
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:14: ';' expected
1.setOpaque(true); // opaque background
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:15: not a statement
1.setBackground(color.yellow);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:15: ';' expected
1.setBackground(color.yellow);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:16: not a statement
1.setForeground(color.blue);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:16: ';' expected
1.setForeground(color.blue);
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:17: not a statement
1.setFont(new Font("SansSerif", Font.Bold, 24));
^
C:\Users\Burak\Desktop\Java\GUI - Greeting\Greeting.java:17: ';' expected
1.setFont(new Font("SansSerif", Font.Bold, 24));
^
10 errors
Tool completed with exit code 1
[/code]
I don't quite get this, could someone please explain this to me?
Thanks!
Variable names must start with a letter.
[QUOTE=micropro;19425360]Variable names must start with a letter.[/QUOTE]
Or _ or $
[QUOTE=micropro;19425360]Variable names must start with a letter.[/QUOTE]
Oh, I see, it had to do with some confusion between 1 and L
you see, they kind of look the same [IMG]http://i46.tinypic.com/25zqvlk.png[/IMG]
Thank you!
[QUOTE=BrQ;19425484]Oh, I see, it had to do with some confusion between 1 and L
you see, they kind of look the same [IMG]http://i46.tinypic.com/25zqvlk.png[/IMG]
Thank you![/QUOTE]
You should use descriptive identifiers for your variables. "label", for example (don't use ambiguous names like "label" when the context doesn't make it obvious, though).
[QUOTE=jA_cOp;19425512]You should use descriptive identifiers for your variables. "label", for example.[/QUOTE]
Well, you're right, but I always first copy the exact code that is written in the book.
[QUOTE=BrQ;19425547]Well, you're right, but I always first copy the exact code that is written in the book.[/QUOTE]
Be sure to then try and rewrite it without looking. It does wonders for learning, and also helps make sure you don't turn into a paste monkey.
[QUOTE=NukePower;19427460]Be sure to then try and rewrite it without looking. It does wonders for learning, and also helps make sure you don't turn into a paste monkey.[/QUOTE]
Usually, when I do that, I make tiny mistakes such as using lowercase letters and those ;
but I guess it helps me learn from my mistakes. Thanks for the tip. I'll do that the nextime :D
[QUOTE=BrQ;19427614]Usually, when I do that, I make tiny mistakes such as using lowercase letters and those ;[/QUOTE]
Your compiler will throw errors for that anyway. You will need to learn where semicolons are needed, and you [I]should[/I] really learn the naming convention used. Once those are down, you won't make mistakes like that anymore :)
Sorry, you need to Log In to post a reply to this thread.