FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


what is wrong with this java code?





Marcuzzo
I've started taking programming lessons ( 3 years, 2 evenings a week) and they've given us the books of all 3 years,
and as impatient as I am, I went ahead and started with the Java book we will be using next year.

I typed over an exercise, and it looks pretty straight forward to me, but for some reason I get an error when I want to compile the bugger.

I'm using the text editor Scite, and I'm compiling with JDK's javac.


these are the files.


tester.java
Code:
import javax.swing.*;
import java.awt.event.*;

public class tester extends JFrame
{
   public static void main ( String [] args )
   {
      
      JFrame frame = new tester();
      frame.setSize( 400, 200 );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setTitle("TestApp");
      JPanel Paneel = new paneel();
      frame.setContentPane ( Paneel );
      frame.setVisible(true);
   }
}


paneel.java
Code:
import javax.swing.*;

public class paneel extends JPanel
{

   private JButton knop, herstelknop;
   private JTextField textvak;
   
   public paneel()
   {
      knop = new JButton("klik");
      KnopHandler kh = new KnopHandler();
      knop.addActionListener ( kh );
      textvak = new JTextField( 10 );
      add( knop );
      add( textvak );
   }
   
   class KnopHandler implements ActionListener
   {
      public void actionPerformed ( ActionEvent e )
      {
         textvak.setText ("Je hebt geklikt");
      }
   }
   
}



and this is the Error I get when I want to compile paneel.java:
Quote:

>C:\Program Files\Java\jdk1.6.0_21\bin\javac paneel.java
paneel.java:19: cannot find symbol
symbol : class ActionListener
location: class paneel
class KnopHandler implements ActionListener
^
paneel.java:21: cannot find symbol
symbol : class ActionEvent
location: class paneel.KnopHandler
public void actionPerformed ( ActionEvent e )
^
paneel.java:13: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (paneel.KnopHandler)
knop.addActionListener ( kh );
^
3 errors



can anybody point me in the right direction as I have no idea what is going wrong,
keep it in mind that this code was typed over from the textbook, and I checked for typo's.
aldhydany
You can know using NEtBeans.......................
Marcuzzo
aldhydany wrote:
You can know using NEtBeans.......................


I've got Eclipse, and there is no point in using eclipse if it fails with javac.
Peterssidan
The second row in tester.java should be moved to paneel.java.
Code:
import java.awt.event.*;
Marcuzzo
Peterssidan wrote:
The second row in tester.java should be moved to paneel.java.
Code:
import java.awt.event.*;



thank you Peterssidan, I'm not too sure why the book puts it in tester.java.
but then again, it doesn't realy say anything about 2 different files and I was always under the implession that you can only have 1 class (like public class, not .class) per java file. am I wrong on that one?
could it be that the exercise in the book uses 1 file only?
Fire Boar
I think it's just a strongly recommended convention.
Peterssidan
You can have only one public class per file but you can declare several non-public classes in the same file.
Code:
// these classes can be put into the same file but note that B and C only is reachable within the same package.
public class A{}
class B{}
class C{}
Marcuzzo
Peterssidan wrote:
You can have only one public class per file but you can declare several non-public classes in the same file.
Code:
// these classes can be put into the same file but note that B and C only is reachable within the same package.
public class A{}
class B{}
class C{}



aah, that explains a lot.
thanks Peterssidan
Related topics
Java
Site Nav Suggestions...
Java Programming Introductory
vBasic
JAva HELP
Problems with adding Mod Users. (Need Help)
What is wrong with this MySQL query?
I'm doing something wrong,,,
A little change color script required
In Command: Syntax Highlighting your code
Stop copy threads from other forums!
Access Java 1.6 Web service using php client
Where should I start? C++ vs. JAVA
Exceptions in Java
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.