LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java "<identifier> expected" error. (https://www.linuxquestions.org/questions/programming-9/java-identifier-expected-error-224987/)

nro 08-31-2004 06:29 PM

Java "<identifier> expected" error.
 
I have a couple simple lines of code, one of which is giving me trouble.

Code:

JTextField txtDisplay = new JTextField();
txtDisplay.setHorizontalAlignment(JTextField.RIGHT);

The text field is created fine, but configuring the alignment is giving trouble. If you see the trouble with my code, please let me know.
The definition for setHorizontalAlignment is:

public void setHorizontalAlignment(int alignment)

Here is my error:

calculator/calculator.java [12:1] <identifier> expected
txtDisplay.setHorizontalAlignment(JTextField.RIGHT);
-------------------------------------^
1 error
Errors compiling calculator

ToniT 08-31-2004 06:44 PM

This compiles for me:
Code:

import javax.swing.*;

public class Foo {
    public static void main(String[] args) {
        JTextField txtDisplay = new JTextField();
        txtDisplay.setHorizontalAlignment(JTextField.RIGHT);
    }
   
}


Mega Man X 09-01-2004 12:20 AM

I'm not sure if you know about this:

http://java.sun.com/j2se/1.4.2/docs/api/

but it has a lot of examples about Java classes. I also agree with ToniT, looks like you are forgetting to import the classes...


All times are GMT -5. The time now is 05:48 AM.