Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile err: "No enclosing instance of type ..."

Status
Not open for further replies.

VicM

Programmer
Sep 24, 2001
442
0
16
US
Folks:
Late in life I'm taking a Java language course at local comm. college. Also using 3 dif. ref. books and online tutorials at Sun.

Learning about Layout managers and trying to understand online examples. Using the snippets from online and trying to run the codes to view the created GUIs. Can get most to work, but ran into one where I can't figure out what the error condition means.

Most of the code in the snippets are just that, snippets. I have to add WindowListeners so that I can close the windows after viewing, and that's where this compile error is occurring. (Have done same in other snippets without problems.)

Following is the code:
Code:
import java.awt.*;
import java.awt.event.*;

public class BorderPanel extends Panel 
{
	private static final Insets insets = new Insets(10, 10, 10, 10);

	public Insets getInsets()
	{
		return insets;
	}

	public void paint(Graphics g)
	{
		Dimension size = getSize();
		g.setColor(getBackground());
		g.draw3DRect(5, 5, size.width-11, size.height-11, false);
	}

	public static void main(String[] args) {
		Frame f = new Frame("Test");
		BorderPanel p = new BorderPanel();
		f.setLayout(new GridLayout(1,0));
		f.setBackground(Color.lightGray);
		f.addWindowListener[COLOR=red](new WinCloser())[/color];
		p.setLayout(new GridLayout(1,0));
		p.add(new Button("Hello"));
		f.add(p);
		f.setVisible(true);
		f.pack();
	}

	class WinCloser extends WindowAdapter
	{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
	}
}

Compile error (red phrase above) reads: No enclosing instance of BorderPanel is accessible. Must qualify the allocation with an enclosing instance of type BorderPanel (e.g. x.A()where x is an instance of BorderPanel).

P.S. If I comment out the WindowListener statement and WinCloser class, the code paints the GUI. But I have to use ctrl-alt-del to end the process. :-(

Would greatly appreciate any insight to what this means.

Thanks,
Vic
 
Your main method is static.
WinCloser is not.

So if you change
Code:
class WinCloser extends WindowAdapter
to
Code:
class WinCloser extends WindowAdapter
your problem is solved.

What I normally do is get out of the static context as soon as possible :
Code:
    public static void main(String[] args) {
      BorderPanel p = new BorderPanel();
      p.doIt();
    }

    public void doIt() {
        Frame f = new Frame("Test");
        f.setLayout(new GridLayout(1,0));
        f.setBackground(Color.lightGray);
        f.addWindowListener(new WinCloser());
        setLayout(new GridLayout(1,0));
        add(new Button("Hello"));
        f.add(this);
        f.setVisible(true);
        f.pack();
    }

    class WinCloser extends WindowAdapter
    {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
    }
 
holgram's advice is the best bet really - static void main is just there to kick off a program from the command line and should be left asap.

However, if you must, then use :

Code:
f.addWindowListener(new WindowAdapter() {
	public void windowClosing(WindowEvent e) {System.exit(0);}
	public void windowClosed(WindowEvent e) {System.exit(0);}
});
 
hologram & sedj

Thanks for your input. It seems I always get caught in those static/non-static situations.

However, upon reviewing the code on the Sun tutorial, it appears to me that they are coding most of the examples to be run from an applet.

I guess I'm not very adept at converting from applet type code to standalone code. But I did some rearranging and managed to get the thing to work as warranted on the site.

I will keep in mind your tips about static/non-static environments and hope I can plug the holes in my brain before it leaks out! :)

Thanks again,
Vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top