我的程序的问题是,一旦"getMP3Tags"方法从名为"musicManagerUI"的主类中删除并放置在它自己的类中,该方法就不会向"musicManager UI"类中的适当方法发送任何信息。但是,如果"getMP3Tags"方法放置在"类,然后一切都很完美。我想知道为什么会发生这种情况,如果有人能帮助我解决这个问题,因为我不想用大量的方法集群"musicManagerUI"类。
以下是主类"musicManagerUI"的构造函数:
public musicManagerUI() {
super ("Music Manager");
initComponents();
handlerClass handler = new handlerClass();
jMenuItem5.addMouseListener(handler);
}
以下是将数据打印到jTable的四种方法之一:
public void printArtistName(ArrayList arrayOfArtistNames)
{
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
if (arrayOfArtistNames == null) {model.setValueAt("No Artist Names Found", 0, 0);}
else {
Object [] rowData;
rowData = new Object[3];
for (Object x : arrayOfArtistNames)
{
model.addRow(rowData);
model.setValueAt(x, counterArtist, 0);
counterArtist++;
}
}
}
以下是鼠标按下处理程序方法的代码:
public void mousePressed(MouseEvent e) {
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
if (counterArtist == 0){}
else if (counterArtist > 0) {
model.setRowCount(0);
counterArtist = 0; counterTitle = 0; counterAlbum = 0; genre = 0;}
String FILE_DIR = "C:\Users\Hazzy\Desktop\test-data\";
String FILE_TEXT_EXT = ".mp3";
findCertainExtension fw = new findCertainExtension();
try {
fw.getMP3Tags(FILE_DIR);
} catch (IOException ex) {
System.err.println(" Could Not read file in ");
} catch (TagException ex) {
System.err.println(" Could Not find tag");
}
}
最后是"getMP3Tags"方法:
public void getMP3Tags( String path ) throws IOException, TagException,
try{
File root = new File( path );
File[] list = root.listFiles();
ArrayList addArtistTitle = new ArrayList();
ArrayList addTrackTitle = new ArrayList();
ArrayList addAlbumTitle = new ArrayList();
ArrayList addGenre = new ArrayList();
if (list == null) return;
for ( File f : list ) {
if ( f.isDirectory()) {
getMP3Tags( f.getAbsolutePath() );
}
else if (!f.getAbsoluteFile().toString().toLowerCase().endsWith("mp3"))
{
getMP3Tags(f.getAbsolutePath());
}
else{
musicManagerUI setFields = new musicManagerUI ();
//System.out.println(f.getAbsoluteFile());
for (Object x : list) {}
MP3File mp3file = new MP3File(f.getAbsoluteFile());
ID3v2_2 name = (ID3v2_2) mp3file.getID3v2Tag();
if (name.getLeadArtist().isEmpty() == true){ name.setLeadArtist("Unknown");}
else if (name.getSongTitle().isEmpty() == true) {name.setSongTitle("Unknown");}
else if (name.getSongGenre().isEmpty() == true) {name.setSongGenre("Unknown");}
else if (name.getAlbumTitle().isEmpty() == true) {name.setAlbumTitle("Unknown");}
addArtistTitle.add(name.getLeadArtist());
addTrackTitle.add(name.getSongTitle());
addAlbumTitle.add(name.getAlbumTitle());
addGenre.add(name.getSongGenre());
}
}
musicManagerUI sendTrackInfo = new musicManagerUI();
sendTrackInfo.printArtistName(addArtistTitle);
sendTrackInfo.printTrackTitle(addTrackTitle);
sendTrackInfo.printAlbumTitle(addAlbumTitle);
sendTrackInfo.printGenre(addGenre);
}
catch(NullPointerException e){
System.err.println("Unknown Artist");
//return;
}
}
}
对于这个程序,我使用ID3标记库/API。如果你需要完整的代码,请给我发电子邮件。提前谢谢。
其他一些可能有用的来源:
JTable没有显示任何内容?加载Java JTable:为什么它不起作用?
我的第一个想法是,在您的getMP3Tags
方法中,您正在调用
musicManagerUI setFields = new musicManagerUI ();
它正在创建一个新的musicManagerUI
实例,该实例与屏幕上的实例无关。。。
您可能会考虑将musicManagerUI
的当前实例的引用传递给方法。。。
fw.getMP3Tags(FILE_DIR, this);
假设this
是实际显示的musicManagerUI
的实例。。。
ps-我应该提到的是,您实际上正在getMP3Tags
中创建musicManagerUI
的另一个实例。。。
musicManagerUI sendTrackInfo = new musicManagerUI();