-1

静的にすることはできませんがContentPane、別のクラスでアクセスしている別のメソッドでアクセスする必要があり、このエラーが発生し続けます:

Cannot make a static reference to the non-static method
    getContentPane() from the type JFrame

..そして私がしようとするとsetSize

Cannot make a static reference to the non-static method setDefaultCloseOperation(int) from the type JFrame

私はとても混乱しています。手伝ってくれませんか?

編集1

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();
    }
}
4

1 に答える 1

0

ここで使用した JFrame クラスのすべてのメソッドが非静的であり、非静的メソッドへの静的参照を作成できないため、このエラーが発生しています。ただし、次のようにコードを書き直すことができます-

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();
    }
}

また、JFrame の拡張は避ける必要があります。詳細については、この投稿をお読みください - JFrame を拡張するか、プログラム内で作成するか

于 2015-03-11T05:51:45.237 に答える