I can't make the ContentPane
static but I have to access it In another method that I'm accessing in another class and I keep getting this error:
Cannot make a static reference to the non-static method
getContentPane() from the type JFrame
..and when I try to setSize
Cannot make a static reference to the non-static method setDefaultCloseOperation(int) from the type JFrame
I am so confused. Can you please help me?
ToutrialStart
public class ToutrialStart extends JFrame implements ActionListener
{
static Container contentPane = getContentPane();
public static void schoolDecider()
{
contentPane.setLayout(null);
contentPane.setBackground(Color.BLACK);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1024, 600);
setLocation(0,0);
setTitle("Title");
setResizable(false);
}
}
touDia
public class touDia extends JFrame implements actionListener
{
public void school()
{
ToutrialStart.schoolDecider();
}
}
You are getting this error because all the methods of JFrame class which you have used here are non-static and you can not make a static reference to the non-static methods. However you can rewrite your code as follow -
public class ToutrialStart extends JFrame implements ActionListener
{
Container contentPane = getContentPane();
public void schoolDecider()
{
contentPane.setLayout(null);
contentPane.setBackground(Color.BLACK);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1024, 600);
setLocation(0,0);
setTitle("Title");
setResizable(false);
}
}
public class touDia extends JFrame implements actionListener
{
public void school()
{
new ToutrialStart().schoolDecider();
}
}
Also you should avoid extending JFrame. Read this post for further info - Extends JFrame vs. creating it inside the program