javajframe与另一类交互

javajframe与另一类交互,java,interaction,Java,Interaction,我已经找到了答案,但我还不清楚 所以,我有一个主类ProcessorCalculations(),我从中调用MainFrame()类。在大型机类中,用户应选择文件夹。如何将JFileChooser()对象从MainFrame()传输到ProcessorCalculations() 我已尝试实现上面链接中的提示: ProcessorCalculation processor = new ProcessorCalculation(); MainFrame mainFrame = new

我已经找到了答案,但我还不清楚

所以,我有一个主类ProcessorCalculations(),我从中调用MainFrame()类。在大型机类中,用户应选择文件夹。如何将JFileChooser()对象从MainFrame()传输到ProcessorCalculations()

我已尝试实现上面链接中的提示:

   ProcessorCalculation processor = new ProcessorCalculation();
   MainFrame mainFrame = new MainFrame(processor);
但是我不知道如何在不创建新对象的情况下从大型机调用
处理器
方法。
即使我不知道该问谷歌什么问题。

请提供帮助。

如果您正在使用上面编写的代码,那么您正在将当前处理器实例传递到大型机构造函数中。您在这个构造函数中使用引用做什么?是否将Processor计算实例设置为此引用?请给我们看看你的构造器

你的大型机类应该看起来像

public class MainFrame extends JFrame {
   // your ProcessorCalculation field  
   private ProcessorCalculation processor;

   public MainFrame(ProcessorCalculation processor) {
      // set the field with ref passed in parameter
      this.processor = processor; 

      // of course other code goes here
   }

   public void someMainFrameMethod() {
      // use the reference
      processor.someProcessorMethod();
   }

}

如果您正在使用上面编写的代码,那么您正在将当前处理器实例传递到大型机构造函数中。您在这个构造函数中使用引用做什么?是否将Processor计算实例设置为此引用?请给我们看看你的构造器

你的大型机类应该看起来像

public class MainFrame extends JFrame {
   // your ProcessorCalculation field  
   private ProcessorCalculation processor;

   public MainFrame(ProcessorCalculation processor) {
      // set the field with ref passed in parameter
      this.processor = processor; 

      // of course other code goes here
   }

   public void someMainFrameMethod() {
      // use the reference
      processor.someProcessorMethod();
   }

}

在大型机中创建一个属性,例如
files
,通过该属性引用
JFileChooser()
的内容(可以说内容存储在此属性中)。如果此属性是私有的,则在编写
Mainframe.getFiles()
(您已经在那里创建了对象
Mainframe
对象)时,将此属性的getter setter方法放在大型机中(以使其可从其他类访问)它返回这个类中需要的数据


如果您仍然面临问题,请要求我提供编码解决方案。

在大型机中创建一个属性,例如
文件
,通过该属性引用
JFileChooser()
的内容(您可以说内容存储在此属性中)。如果此属性是私有的,则在编写
Mainframe.getFiles()
(您已经在那里创建了对象
Mainframe
对象)时,将此属性的getter setter方法放在大型机中(以使其可从其他类访问)它返回这个类中需要的数据

如果你仍然面临一个问题,请要求一个编码的解决方案,我会做的