program help!

United States
January 3, 2009 2:52pm CST
So i'm learning java and I got this book, "Java for the absolute beginner"...and its been a great help. I got the the basic GUI part: Frames and Components...and I typed up the source code for the most basic window, that lets you exit it. But i keep getting tons of errors and i don't know whats happening. Here's the source code, can you tell me what went wrong or offer any help to Frames and Components...thanx: import java.awt.* import java.awt.event.* public class ComponentTestFrame extends Frame implements WindowListener { public ComponentTestFrame() { setBackground(SystemColor.control); setSize(400,300); setLocation(200,150); setLayout(new FlowLayout()); addwindowListener(this); } public void windowClosing(windowEvent e) { dispose(); System.exit(0); } public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowOpened(WindowEvent e) {} } I then have a Label class that uses this class to make labels...the label program has bunches of errors too! idk what to do!
3 responses
• India
5 Jan 09
Still you can't run the program? Just now I ran the program without any errors to make sure it works. Please supply following info: Which JDK you are using? How are you running the program? (I mean, each step) Are you sure you did not do any typing mistakes? Yes, label declarations and all other things are right....
• India
5 Jan 09
Copy paste the program as I have given and also please state at which line you are getting the errors....
• India
5 Jan 09
Oh.... you mean you had two files for two classes and you forgot to import awt in one of them.... Ok... No problem... contact me whenever you have got a problem, I will only be glad to help you out...
• United States
5 Jan 09
I got it. Ha. I am using Easy Eclipse for my JDK. I have two separate classes under the same project folder...shouldn't make any difference... So the solution: Under my LabelTet i had to add one line: import java.awt.*; soo...thanks for all your help thou! You seem to know your Java. If i run across any other problems (prolly simple ones! ha) mind if i give you a "call"? Thanx again!
• India
4 Jan 09
The code that you have provided seems to be fine. To implement listeners, avoid using the Listener interfaces. You should use Adapter classes for this. Because, if you use interfaces you will have to override all the methods in the class like here, you have declared windowDeactivated, WindowDeiconified, etc. If you extend adapter class or use inner classes, you will have to declare and use only the method that you are actually using. If you show me the errors I will be able to help you out with those. You can print screen the errors and upload the image. All the best learning java.. Bye...
• United States
4 Jan 09
here's the source code that uses ComponentTestFrame: import java.awt*; public class LabelTest( public LabelTest() { Label 11 = new Label("Label"); Label 12 = new Label("I am a Label"); 12.setFont(new Font("Timesroman", Font.BOLD, 18)); Label 13 = new Label(); 13.setText("I am disabled"); 13.setEnabled(false); Label 14 = new Label("Colored, Right aligned", Label.RIGHT); 14.setForeground(Color.green); 14.setBackground(Color.black); ComponentTestFrame frame = new ComponentTestFrame("Label Test"); frame.add(11); frame.add(12); frame.add(13); frame.add(14); frame.setVisible(true); } public static void main(String args[]) { LabelTest lt = new LabelTest(); } }
• United States
4 Jan 09
Here's the errors that I get: 4 "Label cannont be resolved" "Syntax error, insert ";" to complete BlockStatements" 8 "Sytax error, insert ";" to complete Statement" 9 "Syntax error, insert "AssignmentOperator Expression" to complete Assignment" "The cronstructor ComponentTestFrame(String) is undefined" 9 "The left-hand side of an assignment must be a variable" 4 "the method add(Component) in the type Conis not applicable for the arguments(int)" "The method setBackground(Color) is undefined for the type LabelTest" "The method setEnabled(boolean) is undefined for the type LabelTest" "The method setFon(Font) is undefined for the type LabelTest" "The method setForeground(Color) is undefined for the type LabelTest" "The method setText(String) is undefined for the type LabelTest"
• India
5 Jan 09
Here is the final program that run: import java.awt.*; import java.awt.event.*; class ComponentTestFrame extends Frame implements WindowListener{ ComponentTestFrame(){ setBackground(SystemColor.control); setSize(400,300); setLocation(200,150); setLayout(new FlowLayout()); addWindowListener(this); } public void windowClosing(WindowEvent e){ dispose(); System.exit(0); } public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} } public class LabelTest{ public LabelTest(){ Label l1= new Label("Label"); Label l2= new Label("I am a Label"); l2.setFont(new Font("Timesroman", Font.BOLD, 18)); Label l3= new Label(); l3.setText("I am disabled"); l3.setEnabled(false); Label l4= new Label("Colored, Right aligned", Label.RIGHT); l4.setForeground(Color.green); l4.setBackground(Color.black); ComponentTestFrame frame= new ComponentTestFrame(); frame.add(l1); frame.add(l2); frame.add(l3); frame.add(l4); frame.setVisible(true); } public static void main(String args[]){ LabelTest lt= new LabelTest(); } } Here is the list of changes that I made to the program: 1. Variable names should not start with a digit. So I converted all 11,12, etc to l1,l2,etc 2. ComponentTestFrame's constructor does not have arguments. Either take an argument or remove the String that you have passed to the constructor. I have removed the argument.
• United States
5 Jan 09
Hey thanx narrowed my errors from 41 to 13! However....it still won't run! Here are my errors: Color cannot be resolved (x2) Font cannot be resolved Font cannot be resolved to a type Label cannot be resolved Label cannot be resolved to a type (x8) I don't get it. Is that not how you declare labels? Hmm....any suggestions?