0

Using Netbeans, I wrote a simple GUI test app, but it doesn't find the other class defined in the project.

DrawPanel:

import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{
    public void paintComponent( Graphics g)
    {
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        g.drawLine(0, 0, w, h);
        g.drawLine(0, h, w, 0);
    }
}

Guitest2.java

package guitest2;
import javax.swing.JFrame;
public class Guitest2 {

    public static void main(String[] args) {
        DrawPanel panel = new DrawPanel();
        JFrame app = new JFrame();
        app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        app.add(panel);
        app.setSize(250, 250);
        app.setVisible(true);
    }   
}

The error is

error: cannot find symbol
    DrawPanel panel = new DrawPanel();

Please see the picture

enter image description here

mahmood
  • 19,156
  • 36
  • 118
  • 190
  • what is the package of `DrawPanel`class? sorry for the obvious question.. you did not mention it in your snippet. – Bagus Tesa Dec 06 '16 at 08:51
  • There is no package for that – mahmood Dec 06 '16 at 08:51
  • 1
    try to add `package guitest2;` at the very top of the `DrawPanel` source code. – Bagus Tesa Dec 06 '16 at 08:52
  • OK Thank you very much. One more question. Sine `paintComponent()` is from `JPanel`, shouldn't I use `@Override`? It is actually overriding the default but, if I don't write `@Override`, it is still working – mahmood Dec 06 '16 at 08:55
  • glad it worked, sometimes i also forgot the `package` part whenever i move codes from somewhere else. as for `@Override` yes, you should (following the convention) although [Java usually do it automatically](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) but we don't know when such feature will be dropped. – Bagus Tesa Dec 06 '16 at 09:02

0 Answers0