0

我正在将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 );
    }
}
4

3 回答 3

1

您不需要构建自己的 Comparator 接口,Java 已经有了。您需要的是构建 Java 的 Comparator 接口的对象并将这些对象传递给 Collections.sort

您甚至可以在 Song 类中为多个比较器执行此操作,如下所示:

public class Song {

// ...

public static Comparator<Song> getAlbumComparator() {
    return new Comparator<Song>() {
        public int compare(YourClass one, YourClass two) {
            return(songA.getAlbum().compareTo(songB.getAlbum());
        }
    };
}

// Other comparators
}

然后你会打电话:

Collections.sort(playlist, Song.getAlbumComparator());

确保您还删除了您自己的 Comparator 接口。

于 2013-10-02T16:59:28.777 回答
0

您在SongSorter() 构造函数中创建了 SongByTitle、SongByArtist、SongByAlbum 和 SongByYear 类。

要解决此问题,您需要在 SongSorter() 构造函数之外创建 SongByTitle、SongByArtist、SongByAlbum、SongByYear 类。

public class SongSorter extends JFrame
   public SongSorter()
   {//here the problem starts
         class  SongByTitle{

        }
        class SongByArtist{

        }
        class SongByAlbum{

        }
        class SongByYear{

       } 
   }//here the problem ends
} 

更正

public class SongSorter extends JFrame
   public SongSorter()
   {
   }
   class  SongByTitle{

   }
   class SongByArtist{

   }
   class SongByAlbum{

   }
   class SongByYear{

   } 
}
于 2013-10-02T16:54:52.700 回答
0

如果这是您的代码的复制/粘贴中的错误,或者真的在这里,我会忽略,但是在您粘贴到问题中的代码中,您在构造函数中声明了 Comparator 类。这样,当您尝试使用它们时,类就不存在了。

您必须提取此类:

public class SongSorter extends JFrame
{
    ...

    public SongSorter() { ... }

    class SongByTitle implements Comparator<Song> { ... }

    class SongByArtist implements Comparator<Song> { ... }

    class SongByAlbum implements Comparator<Song> { ... }

    class SongByYear implements Comparator<Song> { ... }
}

这是你的事吗?

于 2013-10-02T16:55:25.237 回答