无法在外部类中访问getter

时间:2015-08-07 15:48:03

标签: java swing oop midi

我试图在程序的main方法中创建getter函数后从另一个类中访问它。我试过从其他地方访问该函数,但IntelliJ仍然给出了同样的错误:cannot resolve method "getCurrentDeviceInfo()"

相关代码如下:

主要方法(currentMidiDeviceInfo是一个公共变量)

public static void main(String[] args)
{
    optionsGUI = new optionsGUI();
    currentMidiDeviceInfo = optionsGUI.getCurrentDeviceInfo();
}

OptionsGUI Class

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.swing.*;
import java.awt.*;

public class optionsGUI extends JFrame{

    public MidiDevice.Info currentDeviceInfo;

    public MidiDevice.Info getCurrentDeviceInfo() {
        return currentDeviceInfo;
    }

    public optionsGUI() {

        //Setup JFrame
        setLayout(new FlowLayout());
        setSize(new Dimension(250, 140));

        //Get MIDI Device Info
        MidiDevice.Info[] midiDeviceInfo = MidiSystem.getMidiDeviceInfo();
        currentDeviceInfo = midiDeviceInfo[0];

        //Setup Midi Device Selection Label
        JLabel midiDeviceBoxLabel = new JLabel("Select an MIDI Device");
        add(midiDeviceBoxLabel);

        //Setup Midi Device Selection
        JComboBox midiDeviceBox = new JComboBox(midiDeviceInfo);
        add(midiDeviceBox);

        //Setup OK and Cancel Buttons
        JButton okButton = new JButton("OK");
        okButton.addActionListener(e ->
        {
            currentDeviceInfo = (MidiDevice.Info) midiDeviceBox.getSelectedItem();
            setVisible(false);
        });
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(e -> this.setVisible(false));

        //Setup Button Panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel);

    }

}

2 个答案:

答案 0 :(得分:0)

也许您需要将getCurrentDeviceInfo()方法设为静态?或者可能将代码移到main方法之外?

答案 1 :(得分:0)

optionsGUI被声明为JFrame,我现在已经更改了它,并且它可以正常工作,感谢您的帮助!

相关问题