BlueJ - 排序方法'找不到合适的方法'

时间:2013-10-02 16:34:58

标签: java sorting arraylist bluej

我正在使用 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 );
    }
}

3 个答案:

答案 0 :(得分: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界面。

答案 1 :(得分:0)

您在SongSorter()构造函数中创建SongByTitle,SongByArtist,SongByAlbum,SongByYear类

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

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{

   } 
}

答案 2 :(得分: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> { ... }
}

这是你的事吗?

相关问题