-1

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?

Edit1

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Innlion
  • 11
  • 7
  • 2
    You should not be using static methods and variables. Read the section from the Swing tutorial on [How to Make Frames](http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html). The `FrameDemo.java` will give you a simple example with a better structure to get you started. Don't forget to read other sections in the tutorial for more complex examples.. – camickr Mar 11 '15 at 04:17
  • Please show us your whole code! – The Well Mar 11 '15 at 04:19
  • my code is massive stretching over 30 classes and each classes containing about 1000+ lines of code – Innlion Mar 11 '15 at 04:35
  • @TheWell *"Please show us your whole code!"* OP *"my code is massive"* That is one of the reasons we often suggest posting not the 'whole code' but an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). ;) – Andrew Thompson Mar 11 '15 at 04:36
  • i posted the erroneous areas – Innlion Mar 11 '15 at 04:42
  • Where is the `main(String[])`? Where are the imports? BTW - See 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 11 '15 at 05:42
  • the `main(String[])` is about 5 layers of code above. the imports are as follows`import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;` – Innlion Mar 11 '15 at 05:50
  • The program i am making is not for other computers the program is just to push me to my limits and code on a higher level each time i revisit. – Innlion Mar 11 '15 at 05:54

1 Answers1

0

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

Community
  • 1
  • 1
Mandeep Rajpal
  • 1,667
  • 2
  • 11
  • 16