java - Java新手,找不到符号报错

标签 java syntax

我是 Java 的新手,在编译时总是遇到错误,特别是“找不到符号错误”。我试图让表中的字符串作为文件目标进入,但我无法调用该表,所以我尝试使用变量 holder,结果再次出错。

与此同时,我试图更改可以在文件选择器上打开的文件类型,但我找不到可行的解决方案。

最后,有人可以解释什么是缺失符号吗?因为我觉得我误解了它,因为我已经尝试修复它很多次但都没有成功。

//C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav
//Andrew Douglas
//Imports
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.filechooser.*;
import javax.swing.JTable;

//Creates class
public class jPlayer extends JFrame implements ActionListener {

    //Sets up form items and necessary globals
    JButton save, play, stop, loop;
    JFileChooser dialog;

    String Artist, Song, Album, Loc;
    Object[][] data;
    int n = 1;
    String holder;
    //Makes the library, with a 51 song limit.
    jLibrary[] addedSong = new jLibrary[50];

    public jPlayer() {
        //Creates frame
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("jPlayer");
        this.setSize(800, 600);
        //Makes titles for table
        String[] columnNames =  {"Artist",
                                "Song",
                                "Album",
                                "Location"};
        //Gives one value for array
        addedSong[0] = new jLibrary("Rick Astley", "NGGYU", "UnKnown", "C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav");
        //Adds it to table array
        Object[][] data = {
        {
            addedSong[0]
        }

        };
        //Creates table
        final Jtable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        //Lets it sort the rows
        table.setAutoCreateRowSorter(true);
        //Creates the scroller
        JScrollPane scrollPane = new JScrollPane(table);
        //Makes the save file dialog and the play and save buttons
        dialog = new JFileChooser();
        play = new JButton ("Play Song");
        save = new JButton ("Save a file");
        //Adds the button listeners
        save.addActionListener(this);
        play.addActionListener(this);
        //Adds buttons to panel
        JPanel buttons = new JPanel();
        buttons.add(save);
        buttons.add(play);
        //Puts the buttons at the bottom
        add(buttons, BorderLayout.SOUTH);
        add(scrollPane);
        this.setVisible(true);
        holder = table.getselectedRows[3];

    }
    //Creates action listener for button
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == save) {
            dialog.setFileFilter(new FileNameExtensionFiler("Wave File (*.wav)"));
            int returnVal = dialog.showSaveDialog(jPlayer.this);
            if (returnVal == dialog.APPROVE_OPTION) {
                File file = dialog.getSelectedFile();
                addToLibrary("", "", "", file.getName());

            }
        }
        else if (e.getSource() == play) {
            try {
            File soundFile = new File(holder);
            System.out.println(soundFile);
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
            } catch (UnsupportedAudioFileException f) {
         f.printStackTrace();
      } catch (IOException f) {
         f.printStackTrace();
      } catch (LineUnavailableException f) {
         f.printStackTrace();
      }

    } }
    public static void main(String[]args) {
        new jPlayer();
    }
    public void addToLibrary(String art, String song, String alb, String file) {
            addedSong[n] = new jLibrary(art, song, alb, file);
            int j = 0;
            while (n >= 0) {
            Object[][] data = {
            {
                addedSong[(n-j)],
            }
        };
            j = j+1;
        }
            n = n +1;

    }
}

错误:

--------------------Configuration: <Default>--------------------
C:\Users\Andrew\Documents\ICS4U Final\jPlayer.java:47: error: cannot find symbol
        final Jtable table = new JTable(data, columnNames);
              ^
  symbol:   class Jtable
  location: class jPlayer
C:\Users\Andrew\Documents\ICS4U Final\jPlayer.java:75: error: cannot find symbol
            dialog.setFileFilter(new FileNameExtensionFiler("Wave File (*.wav)"));
                                     ^
  symbol:   class FileNameExtensionFiler
  location: class jPlayer
2 errors

Process completed.

抱歉所有问题,感谢您的帮助!

编辑:我已将 Jtable 更改为 JTable,并将 t 添加到过滤器中,并删除了 holder,因为我不再需要它了。

代码现在看起来像:

//C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav
//Andrew Douglas
//Imports
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.filechooser.*;
import javax.swing.JTable;

//Creates class
public class JPlayer extends JFrame implements ActionListener {

    //Sets up form items and necessary globals
    JButton save, play, stop, loop;
    JFileChooser dialog;

    String Artist, Song, Album, Loc;
    Object[][] data;
    int n = 1;
    //Makes the library, with a 51 song limit.
    jLibrary[] addedSong = new jLibrary[50];

    public JPlayer() {
        //Creates frame
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("jPlayer");
        this.setSize(800, 600);
        //Makes titles for table
        String[] columnNames =  {"Artist",
                                "Song",
                                "Album",
                                "Location"};
        //Gives one value for array
        addedSong[0] = new jLibrary("Rick Astley", "NGGYU", "UnKnown", "C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav");
        //Adds it to table array
        Object[][] data = {
        {
            addedSong[0]
        }

        };
        //Creates table
        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        //Lets it sort the rows
        table.setAutoCreateRowSorter(true);
        //Creates the scroller
        JScrollPane scrollPane = new JScrollPane(table);
        //Makes the save file dialog and the play and save buttons
        dialog = new JFileChooser();
        play = new JButton ("Play Song");
        save = new JButton ("Save a file");
        //Adds the button listeners
        save.addActionListener(this);
        play.addActionListener(this);
        //Adds buttons to panel
        JPanel buttons = new JPanel();
        buttons.add(save);
        buttons.add(play);
        //Puts the buttons at the bottom
        add(buttons, BorderLayout.SOUTH);
        add(scrollPane);
        this.setVisible(true);

    }
    //Creates action listener for button
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == save) {
            dialog.setFileFilter(new FileNameExtensionFilter("Wave File (*.wav)"));
            int returnVal = dialog.showSaveDialog(JPlayer.this);
            if (returnVal == dialog.APPROVE_OPTION) {
                File file = dialog.getSelectedFile();
                addToLibrary("", "", "", file.getName());

            }
        }
        else if (e.getSource() == play) {
            try {
            File soundFile = new File(table.getSelectedRows[3]);
            System.out.println(soundFile);
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
            } catch (UnsupportedAudioFileException f) {
         f.printStackTrace();
      } catch (IOException f) {
         f.printStackTrace();
      } catch (LineUnavailableException f) {
         f.printStackTrace();
      }

    } }
    public static void main(String[]args) {
        new jPlayer();
    }
    public void addToLibrary(String art, String song, String alb, String file) {
            addedSong[n] = new jLibrary(art, song, alb, file);
            int j = 0;
            while (n >= 0) {
            Object[][] data = {
            {
                addedSong[(n-j)],
            }
        };
            j = j+1;
        }
            n = n +1;

    }
}

但现在我收到了这个错误:

--------------------Configuration: <Default>--------------------
C:\Users\Andrew\Documents\ICS4U Final\JPlayer.java:83: error: cannot find symbol
            File soundFile = new File(table.getSelectedRows[3]);
                                      ^
  symbol:   variable table
  location: class JPlayer
1 error

Process completed.

有什么帮助吗?

最佳答案

你需要的类是JTable,不是Jtable

该错误基本上意味着您正在使用不存在的符号。您尝试使用的类存在于 package javax.swing(您已导入)中:javax.swing.JTable,带有一个大的 T

IDE 会帮助您找到它,但对于初学者来说,从文本编辑器开始并一个一个地查看错误也是一件好事 ;)

关于java - Java新手,找不到符号报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168670/

相关文章:

syntax - iPython Notebook 中的 Markdown : how to highlight code?

java - 根据输入进行切换并返回动态值

java - 在默认的 Android 日历中创建日历事件

java - ModelMapper:将规则应用于所有类型的字段

jquery - 迭代 JSON 列表会产生 "Error: Syntax error, unrecognized expression"

java - 如何构建 Java 类的元模型以通过代码分析生成服务图?

list - 使用整数列表复制列表元素的 Lisp 程序

mysql - 带 ? 的输入参数语法在 MySQL 查询中

java - Ubuntu 16.04 上的 DB Visualizer - java.util.NoSuchElementException

java - 带有 IN 子句的 Spring Data JPA 中的 NPE