1

My code is an example from my book and I'm doing something wrong concerning the creation and reference on the class paneel.

The error is:

Main.java:14: error: non-static variable this cannot be referenced from a static context
                frame.setContentPane( new paneel() );

What am I doing wrong?

package vb303;

import javax.swing.*;
import java.awt.*;


public class Vb303 extends JFrame{

    public static void main(String[] args) {
        JFrame frame = new Vb303();
                frame.setSize(400,200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("vb303");
                frame.setContentPane( new paneel() );
                frame.setVisible(true);
    }

    public class paneel extends JPanel{
        private int totaalMinuten, uren, restMinuten;

        public paneel(){
            totaalMinuten = 1412;
            uren = totaalMinuten /60;
            restMinuten = totaalMinuten %60;
        }


    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.drawString(totaalMinuten+ "minuten =", 50, 30);
        g.drawString(uren + "uuren",50,30);
        g.drawString(restMinuten + "minuten", 50, 90);
    }
    }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Jairzinho
  • 11
  • 3
  • 1
    Change `public class paneel` to `public static class paneel`. – Andy Turner Nov 08 '16 at 08:21
  • 1
    make it `public static class Paneel extends JPanel` (please stick to class name camel case). Otherwise `Paneel` is bound to an instance of `Vb303` – SomeJavaGuy Nov 08 '16 at 08:22
  • Or create Vb303 object and call it. eg: Vb303 vb303 = new Vb303(); vb303.new paneel() – Ye Win Nov 08 '16 at 08:23
  • thank you its working the example in the book says to make it public class Paneel extends JPanel not static thank u and yes i am aware of the grammar – Jairzinho Nov 08 '16 at 08:29

0 Answers0