Не работает JFileChooser и getName

        package display;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PlayerGUI {

    private JFrame frame;
    private JTextField pathField;

    private String songFile;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());    

                    PlayerGUI window = new PlayerGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public PlayerGUI() {
        initialize();
    }


    private void initialize() {
        frame = new JFrame();
        frame.setForeground(Color.BLACK);
        frame.setTitle("GeN.G MP3");
        frame.setBounds(100, 100, 290, 148);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setLayout(null);

        JButton startBtn = new JButton(">");
        startBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //play audio
            }
        });
        startBtn.setFont(new Font("Tahoma", Font.PLAIN, 16));
        startBtn.setBounds(10, 42, 254, 56);
        frame.getContentPane().add(startBtn);

        pathField = new JTextField();
        pathField.setForeground(Color.GRAY);
        pathField.setEditable(false);
        pathField.setText("\u041F\u0443\u0442\u044C \u043A \u043F\u0435\u0441\u043D\u0435");
        pathField.setBounds(10, 11, 188, 20);
        frame.getContentPane().add(pathField);
        pathField.setColumns(10);

        JButton openBtn = new JButton("\u041D\u0430\u0439\u0442\u0438");
        openBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setDialogTitle("Выбери песню для проигрывания...");
                    chooser.showOpenDialog(null);
                    songFile = chooser.getSelectedFile();
                    System.out.println("Файл "+songFile.getName ()+", выбран!");

                }catch(Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        openBtn.setBounds(201, 10, 63, 23);
        frame.getContentPane().add(openBtn);
    }
}

Подчеркивается JFileChooser в JFileChooser chooser = new JFileChooser(); (два раза) и getName в System.out.println("Файл "+songFile.getName ()+", выбран!");.

При нажатии openBtn ошибка такая:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
JFileChooser cannot be resolved to a type
JFileChooser cannot be resolved to a type
The method getName() is undefined for the type String

at display.PlayerGUI$3.actionPerformed(PlayerGUI.java:74)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source).

Eclipse 2019-12 (4.14.0)
В чём может быть проблема? Заранее спасибо!


Ответы (1 шт):

Автор решения: Roman C

Проблема в том что компилятор не может скомпилировать код в котором отсутствует класс JFileChooser.

Известные классы можно добавить используя директиву import.

В вашем случае надо добавить

import javax.swing.JFileChooser;
→ Ссылка