【问题标题】:Java- Main method cannot be found in classJava-在类中找不到主要方法
【发布时间】:2011-11-30 18:41:30
【问题描述】:

我知道这个主题已经被破坏了几次,但是我在修复它时遇到了一个错误,据说它在其他主题中是固定的。

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 会显示另一个错误。“无效的方法声明”

【问题讨论】:

  • 在哪里是无效的方法声明,究竟是什么?这是另一个内部的嵌套类吗?请提供所有您看到的错误的详细信息。
  • 你如何尝试启动程序?
  • 问题标题似乎与正文不匹配。
  • 如果你真的有一个不错的编译器,你会得到以下错误 - “类 FlowAp 的非法修饰符;只允许 public、abstract 和 final”
  • a) 它说“FlowAp fa=new FlowAp(); b) 那是程序的开始。c) 那是错误,我只是说其他人的修复由于不同的错误,在另一个线程中将无法工作。d)我使用的是 NetBeans 7.0.1

标签: java swing main


【解决方案1】:

在您的情况下,您的班级不允许使用修饰符“static” - 删除它,它会起作用。如果您想访问您的变量,您必须将它们设为静态,以便您可以从 main 方法中引用它们。

【讨论】:

  • 我不这么认为,但它是说如果没有静态,这将无法工作。它给出了一个单独的错误消息,关于您如何无法在静态上下文中引用非静态变量。
  • 那你应该让你的变量是静态的,而不是你的类
【解决方案2】:

there是基本的东西,有很多错误我不能评论,然后

来自代码

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

【讨论】:

    【解决方案3】:

    尝试删除“静态”:

    public class FlowAp extends JFrame{
    

    【讨论】:

    • 当我从类中删除静态时,它表明我不能在静态上下文中引用非静态变量。
    【解决方案4】:

    应该是:

    public static void main(String[] argv){
    

    贴出你写这篇文章时发生的错误。

    注意:
    - 类不能是静态的。

    【讨论】:

    • 没有任何区别。 []s 通常是您首选的方式,但 C 兼容方式有效。
    • 在 java 中,[] 在哪里并不重要,String[] blah 与 String blah[]; 相同;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多