我正在将BlueJ用于一个项目。这是我的代码:
import javax.swing.*;`
import java.awt.*;
import java.util.Collection;
import java.util.Collections;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class SongSorter extends JFrame
{
private JPanel main, header, buttons1, buttons2, labels, texts, SongPanel;
private JTextField Title, Artist, Album, Year;
private JLabel credits, title, artist, album, year;
private JButton add, shuffle, sortTitle, sortArtist, sortAlbum, sortYear;
private JTextArea RecordsText;
private Song s;
private List<Song> playlist;
public SongSorter()
{
Container c = this.getContentPane();
c.setLayout( new FlowLayout() );
c.setBackground( Color.BLACK );
main = new JPanel( new BorderLayout() );
main.setSize( 200, 200 );
c.add( main);
// for buttons
header = new JPanel( new GridLayout(4, 1) );
main.add( header, "North" );
buttons1 = new JPanel( new FlowLayout() );
header.add( buttons1 );
add = new JButton( "Add" );
shuffle = new JButton( "Shuffle" );
buttons1.add( add );
buttons1.add( shuffle );
buttons2 = new JPanel( new FlowLayout() );
header.add( buttons2 );
sortTitle = new JButton( "Title" );
sortArtist = new JButton( "Artist" );
sortAlbum = new JButton( "Album" );
sortYear = new JButton( "Year" );
buttons2.add( sortTitle );
buttons2.add( sortArtist );
buttons2.add( sortAlbum );
buttons2.add( sortYear );
texts = new JPanel( new FlowLayout() );
header.add( texts );
Title = new JTextField( "Title", 10 );
Artist = new JTextField( "Artist", 10 );
Album = new JTextField( "Album", 10 );
Year = new JTextField( "Year", 10 );
title = new JLabel("Title: ");
artist = new JLabel("Artist: ");
album = new JLabel("Album: ");
year = new JLabel("Year: ");
texts.add( title );
texts.add( Title );
texts.add( artist );
texts.add( Artist );
texts.add( album );
texts.add( Album );
texts.add( year );
texts.add( Year );
// where the user will see the record of songs listed
SongPanel = new JPanel( new GridLayout(1, 1) );
main.add( SongPanel, "Center" );
SongPanel.setSize(100, 100);
RecordsText = new JTextArea();
RecordsText.setWrapStyleWord( true );
RecordsText.setLineWrap( true );
RecordsText.setTabSize( 100 );
SongPanel.add( RecordsText );
playlist = new ArrayList<Song>();
// sorting comparators
final SongByTitle t = new SongByTitle();
final SongByArtist a = new SongByArtist();
final SongByAlbum b = new SongByAlbum();
final SongByYear y = new SongByYear();
// time for buttons and shit
// add and shuffle
add.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
String t = Title.getText();
String a = Artist.getText();
String b = Album.getText();
int y = Integer.parseInt( Year.getText() );
s = new Song(t, a, b, y);
playlist.add( s );
RecordsText.append( s.print() );
}
}
);
shuffle.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
Collections.shuffle( playlist );
RecordsText.setText( playlist.toString() );
}
} );
// sorting buttons
sortTitle.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
Collections.sort( playlist, t );
RecordsText.setText( playlist.toString() );
}
} );
sortArtist.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
Collections.sort( playlist, a );
RecordsText.setText( playlist.toString() );
}
} );
sortAlbum.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
Collections.sort( playlist, b );
RecordsText.setText( playlist.toString() );
}
} );
sortYear.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ae )
{
Collections.sort( playlist, y );
RecordsText.setText( playlist.toString() );
}
} );
}
}
class SongByTitle implements Comparator<Song>
{
public int compare( Song a, Song b )
{
return a.getTitle().compareTo( b.getTitle() );
}
}
class SongByArtist implements Comparator<Song>
{
public int compare( Song a, Song b )
{
return a.getArtist().compareTo( b.getArtist() );
}
}
class SongByAlbum implements Comparator<Song>
{
public int compare( Song a, Song b )
{
return a.getAlbum().compareTo( b.getAlbum() );
}
}
class SongByYear implements Comparator<Song>
{
public int compare( Song a, Song b )
{
if( a.getYear() < b.getYear() ) return -1;
else if(a.getYear() > b.getYear() ) return 1;
else return 0;
}
}
我不断收到这个错误:
no suitable method found for sort(java.util.List<Song>, SongByTitle)
method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable
(no instance(s) of type variable(s) T exist so that argument type SongByTitle conforms to formal parameter type java.util.Comparator<? super T>)
method java.util.Collections.<T>sort(java.util.List<T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
我一直在网上搜索一个小时左右我可以做些什么来解决这个问题;我已将播放列表从 ArrayList 更改为仅 List,我尝试仅在 sort() 方法本身内调用 Comparator - 我觉得我已经尝试了所有方法,但它仍然不起作用。
每当我尝试编译它时,它总是突出显示我的“.sort”部分。我觉得我已经没有地方可以去寻求有关此事的建议了。
我还在stackoverflow上阅读了其他问题。我已经浏览这些页面一个小时了,但仍然没有任何线索。我不知道我哪里弄错了。在我看来很好。
谁能在这方面提供帮助,我们将不胜感激。(等我做完你会帮我早点睡。XD)
--
哦,以防万一,这是程序中的其他类。
比较器接口
public interface Comparator<Song>
{
int compare( Song a, Song b );
}
歌曲类
public class Song
{
String title, artist, album;
int year;
public Song(String t, String a, String b, int y)
{
title = t;
artist = a;
album = b;
year = y;
}
public String getTitle()
{
return title;
}
public String getArtist()
{
return artist;
}
public String getAlbum()
{
return album;
}
public int getYear()
{
return year;
}
public String print()
{
String t = this.getTitle();
String a = this.getArtist();
String b = this.getAlbum();
int y = this.getYear();
String song = t +
" by " + a + "; Album: " + b + ", " + y + "\n";
return song;
}
@Override
public String toString()
{
return title + " by " + artist
+ "; Album: " + album + ", " + year + "\n";
}
}
赛跑者类
public class SongRunner
{
public static void main( String[] args )
{
JFrame f = new SongSorter();
f.setSize( 200, 200 );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setVisible( true );
f.setResizable( false );
}
}