gpt4 book ai didi

java - 如何将一个java文件包含到另一个具有主要功能的java文件中?

转载 作者:行者123 更新时间:2023-12-01 18:11:15 25 4
gpt4 key购买 nike

我想将一个 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 文件中并将它们一起运行?

最佳答案

你就是不能这么做。每个 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();
}
}

关于java - 如何将一个java文件包含到另一个具有主要功能的java文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com