Java中如何将字符串从一个类传输到另一个类

Java中如何将字符串从一个类传输到另一个类,java,string,class,Java,String,Class,我有两门课: ConnectionPanel.class public class ConnectionPanel extends JPanel implements ActionListener,ItemListener, PropertyChangeListener { private MasterModel m_masterModel; private JTextField m_keyField; public ConnectionPanel(MasterModel m

我有两门课:

ConnectionPanel.class

public class ConnectionPanel extends JPanel implements ActionListener,ItemListener, PropertyChangeListener {
   private MasterModel m_masterModel;
   private JTextField m_keyField;

   public ConnectionPanel(MasterModel masterModel) {
       m_masterModel = masterModel;
       setLayout(new GridLayout(0, 2));
       JPanel panel = null;
       panel = new JPanel();
       panel.add(new JLabel("Type Key:"));
       m_keyField = new JTextField(9);
       m_keyField.setText("dertfgdertabcdef");
       panel.add(m_keyField);
       add(panel);
       getChatModel().addPropertyChangeListener(this);
       getChatModel().setListen(true);
    } 

    public String getEncryptionKey(){
       return m_keyField.getText();
    }
}
音频播放类

public class AudioPlayback extends AudioBase {
    // and here I want to be able to get
    // somehow String key = ConnectionPanel.getEncryptionKey()
    // I tried ConnectionPanel panel = new ConnectionPanel(); but does not work
    // it messes my graphical interface
    // also there is lots of code here too
}

你知道如何将该字段输入到audioplayback.class中吗?

我完全同意moffeltje&Luke的观点。这个问题很简单,所以我认为您可以学习一些关于Java的基础知识。 但我复习了你们的连接面板构造器。对不起,你们知道构造器吗? 您可以在此处找到详细信息:

现在,让我们谈谈你的代码。您只为ConnectionPanel提供了一个带有一个输入参数的构造函数,这样您就不能在AudioPlayback中使用没有参数的构造函数。 但是您的ConnectionPanel中似乎没有调用属性masterModel,因此您可以大致使用以下代码:

// Notice! Don't do this in your production enviroment. It's only for test otherwise you can make sure that you can give the null value to it.
ConnectionPanel panle = new ConnectionPanel(null);
System.out.println(panel.getEncryptionKey());

你知道Java中类、变量和关键字的基本知识吗?我很少使用多个类,我的大多数应用程序都是过程性的。这就是我面临这个问题的原因。然后我建议看一些关于如何使用类的教程,如果你想用它来制作一个应用程序,那么我建议@moffeltje——这是一个非常基本的java语法/OO设计问题,可能不适合堆栈溢出。在这种情况下,您的音频播放需要获得一个参考连接面板。对不起,我知道这个问题可能很简单,但我甚至不知道在哪里可以找到答案。如果您能提供任何链接以找到此问题的答案,我将不胜感激。