My question is why do I get the "non-static variable this cannot be referenced from a static content" error?
I understand that static content can't be used in a non-static context. What is the factor that makes the difference between the first and second block that will cause the error?
The first block gives me the error.
/**FIRST BLOCK**/
package vb503;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Jairzinho
*/
public class vb503 extends JFrame{
public static void main(String [] args){
JFrame frame = new vb503();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("vb503");
frame.setContentPane(new KlikPaneel());// error here
frame.setVisible(true);
}
class KlikPaneel extends JPanel{
}
}
/** SECOND BLOCK**/
package vb502;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Jairzinho
*/
public class Vb502 extends JFrame{
public static void main(String[] args) {
JFrame frame = new Vb502();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("vb502");
**frame.setContentPane(new CijferPaneel());**
frame.setVisible(true);
}
}
class CijferPaneel extends JPanel{
private JLabel label;
private JTextField tekstvak;
private double cijfer;
public CijferPaneel(){
label = new JLabel("voer een cijfer in ");
tekstvak = new JTextField(10);
tekstvak.addActionListener(new tekstvakHandler());
add(label);
add(tekstvak);
}
class tekstvakHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String invoer = tekstvak.getText();
cijfer = Double.parseDouble(invoer);
System.out.println("het ingevoerde cijfer is :" + cijfer);
if(cijfer >=6){
System.out.println("dat is een voldoende");
}
System.out.println("dank u!");
System.out.println();
}
}
}