【问题标题】:How to include one java file into another java file both having the main functions in it?如何将一个 java 文件包含到另一个具有主要功能的 java 文件中?
【发布时间】:2015-12-26 03:08:20
【问题描述】:

我想将一个 java 文件包含到另一个文件中。两者都有它的主要功能。一个文件类似于以下内容:

public class FileShow
{

 public static void main(String args[])
  {

        JFrame guiFrame = new JFrame();

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("RTL Parser GUI");
        guiFrame.setSize(500,500);

        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);

        JPanel comboPanel = new JPanel();
        JTextField handle = new JTextField(30);
        comboPanel.add(handle);

    guiFrame.add(comboPanel);
    guiFrame.setVisible(true);
  }
}

而我的另一个 java 文件是:

public class AnotherFile{

    public static void main(String[] args) {

        new AnotherFile();
    }

    public AnotherFile()
    {
        guiFrame = new JFrame();

        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Assertion-Based GUI");
        guiFrame.setSize(500,500);

        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);

    JPanel comboPanel = new JPanel();
        JTextField handle = new JTextField(30);
        comboPanel.add(handle);

    guiFrame.add(comboPanel);
    guiFrame.setVisible(true);
    }
}

有什么方法可以将这两个文件合并并一起运行,因为两者都有主要功能? 如何将这两个文件合并到同一个 java 文件中并一起运行?

【问题讨论】:

  • 你的代码编译了吗?
  • 我单独编译的。
  • 再试一次。在另一个文件类中,guiFrame = new JFrame();它会产生一个错误,因为你没有声明哪个类的对象是 guiFrame。然后就可以编译了。应该是 JFrame guiFrame = new JFrame();
  • 哦,我已经将 JFrame 声明为公共类,我忘了提及。

标签: java file class user-interface main


【解决方案1】:

你不能那样做。每个 Java 文件应该只有一个 main 方法。

但是你可以更好地组织你的文件来做你想做的事:

public class FileShow{

    public void doSomething(){
    //...
    }

}

public class AnotherFile{

    public void doSomething(){
    //...
    }

}

public class mainClass(){
    public static void main(String args[])
         new FileShow().doFileShow();               
         new AnotherFile().doAnotherFile();
    }
}

【讨论】:

    【解决方案2】:

    我只想在 Fileshow 的主要方法中添加“AnotherFile”对象。你只能有一个 main 方法。

    所以在Fileshow.java中,在main方法中添加

    Anotherfile a = new Anotherfile()
    

    【讨论】:

      【解决方案3】:

      从您写的内容来看,完全不清楚您想要实现什么。将两个 Java 类包含在一个 .java 文件中?放入一个 .jar 文件? “一起跑”是什么意思?

      在一个源文件中组合两个顶级 Java 类是可能的(根据 JLS),而其中只有一个可能是公共的。但我相信,这不是最佳实践,只是因为你的课程生命周期相当混乱。但是,如果您仍然想这样做,则必须将其中之一设为私有或嵌套。

      把两者都放在一个罐子里是微不足道的。只需致电jar cf jarname <classes>。也可以通过在 java 命令行中显式提及它们来单独调用主要方法,例如 java -cp jarname <package>.FileShow。

      不过,我不确定我是否正确理解了您的问题。

      【讨论】:

        【解决方案4】:

        在 Java 中,每个 Java 文件可以包含一个公共类,默认情况下 JDK 会调用它的 main 方法。如果你有两个类都有一个 main 方法并且你想把它保存在一个 Java 文件中,这两个类不能是公共的,一个必须是 inner/nested class。我在下面给出了一个例子。

        public class FileShow
        {
             public static void main(String args[])
             {
                 AnotherFile.main(args);;
                 // Your code
             }
        
             static class AnotherFile
             { // as it contains a static method
        
                public static void main(String[] args) //or any static class
                {
                   new AnotherFile();
                }
                public AnotherFile(){
                 // Your code
                }
             }
        }
        

        从逻辑上讲,它会起作用。但我强烈反对这样做。这不是标准的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-24
          • 2017-04-06
          • 1970-01-01
          • 2012-05-01
          • 2019-12-09
          • 1970-01-01
          相关资源
          最近更新 更多