簡體   English   中英

Java- Main方法在類中找不到

[英]Java- Main method cannot be found in class

我知道這個主題已經被突破了幾次,但是在按照其他方式修復它時,我遇到了一個錯誤。

public static class FlowAp extends JFrame{
    String one = "One";
    String two = "Two";
    String three = "Three";
    String four = "Four";
    String five = "Five";

public static void main(String argv[]){
    FlowAp fa=new FlowAp();
    //Change from BorderLayout default
    fa.getContentPane().setLayout(new FlowLayout());
    fa.setSize(200,200);
    fa.setVisible(true);
}
FlowAp(){

    JButton one = new JButton("One");
    getContentPane().add(one);
    JButton two = new JButton("Two");
    getContentPane().add(two);
    JButton three = new JButton("Three");
    getContentPane().add(three);
    JButton four = new JButton("four");
    getContentPane().add(four);
    JButton five = new JButton("five");
    getContentPane().add(five);

}
}

當我實際上將它們放在看起來應該是的括號中時,flowap會顯示另一個錯誤。“無效的方法聲明”

在您的情況下,不允許為您的班級使用修飾語“靜態”-將其刪除即可使用。 如果要訪問變量,則必須將其設置為靜態,以便可以從main方法中引用它們。

嘗試刪除“靜態”:

public class FlowAp extends JFrame{

一些基本的東西,有很多錯誤,我不能發表評論,然后

在此處輸入圖片說明

從代碼

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class FlowAp extends JFrame {

    private static final long serialVersionUID = 1L;
    private String one = "One";
    private String two = "Two";
    private String three = "Three";
    private String four = "Four";
    private String five = "Five";

    public FlowAp() {
        JButton oneButton = new JButton(one);
        JButton twoButton = new JButton(two);
        JButton threeButton = new JButton(three);
        JButton fourButton = new JButton(four);
        JButton fiveButton = new JButton(five);

        setTitle("FlowAp");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        add(oneButton);
        add(twoButton);
        add(threeButton);
        add(fourButton);
        add(fiveButton);
        setLocation(100, 100);
        pack();
        setVisible(true);
    }

    public static void main(String argv[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                FlowAp fa = new FlowAp();
            }
        });
    }
}

應該:

public static void main(String[] argv){

發布您編寫此文件時發生的錯誤。

注意:
-類不能為靜態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM