【发布时间】: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