-3

我有这个类 Playlist 和这个类 PlaylistOperations,我想在 PlaylistOperations 中实现一个来自 Playlist 的类 toString。每次我尝试在 PlaylistOperations 中使用 toString 时,我的程序都会崩溃。addSong 也会发生同样的事情。我真的需要能够以这种方式实现它,我尝试使用 Playlist.toString() 但是当我将 toString 方法设为静态时,它只是说让它非静态导致循环。

以下是播放列表操作

public class PlaylistOperations extends Playlist{



public static void main(String[] args) {

    JFrame mainMenu = new JFrame("Main Menu");
    JPanel mainPane = new JPanel();
    mainMenu.add(mainPane);
    mainMenu.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    mainPane.setLayout(new FlowLayout());
    JButton addSong = new JButton("Add Song");
    mainPane.add(addSong);
    JButton printSongs = new JButton("Print Songs by Artist");
    mainPane.add(printSongs);
    JButton getSong = new JButton("Get Song");
    mainPane.add(getSong);
    JButton removeSong = new JButton("Remove Song");
    mainPane.add(removeSong);
    JButton printAllSongs = new JButton("Print All Songs");
    mainPane.add(printAllSongs);
    JButton size = new JButton("Size");
    mainPane.add(size);
    JButton quit = new JButton("Quit");
    mainPane.add(quit);
    mainMenu.pack();
    mainMenu.setSize(800, 200);
    mainMenu.setVisible(true);
    mainPane.setVisible(true);
    addSong.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            new Playlist().addSong();
        }
    });
    printSongs.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }
    });
    getSong.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }
    });
    removeSong.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }
    });
    printAllSongs.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JFrame toStringFrame = new JFrame("Songs in Playlist");

            toStringFrame.setVisible(true);
            toStringFrame.pack();
            toStringFrame.setSize(250,250);
            JPanel toStringPanel = new JPanel();
            toStringFrame.add(toStringPanel);
            JLabel printed = new JLabel(one.toString());
            toStringPanel.add(printed);

        }
    });
    size.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int amountOfSongs = 0;
            for (int x = 0; x < 50; x++) {
                if (slotFull(new Playlist().songs, x)) {
                    amountOfSongs++;
                }
            }
            final JFrame amountOfSongsFrame = new JFrame("Amount of songs");
            JPanel amountOfSongsPanel = new JPanel();
            JLabel amountOfSongsLabel = new JLabel("There are "
                    + amountOfSongs + " songs in this playlist.");
            JButton okayEnterText = new JButton("Okay");
            amountOfSongsFrame.setVisible(true);
            okayEnterText.setPreferredSize(new Dimension(100, 27));
            amountOfSongsFrame.add(amountOfSongsPanel);
            amountOfSongsPanel.add(amountOfSongsLabel);
            amountOfSongsPanel.add(okayEnterText);
            amountOfSongsFrame.pack();
            amountOfSongsFrame.setSize(300, 200);
            okayEnterText.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    amountOfSongsFrame.dispose();

                }
            });
        }
    });
    quit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            final JFrame exit = new JFrame(
                    "Are you sure you would like quit?");
            JPanel exiter = new JPanel();
            exit.add(exiter);
            exit.setVisible(true);
            JButton yes = new JButton("Yes");
            exiter.add(yes);
            JButton no = new JButton("No");
            exiter.add(no);
            exit.pack();
            exit.setSize(450, 150);
            exiter.setSize(450, 150);
            yes.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);

                }
            });
            no.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    exit.dispose();

                }
            });

        }
    });
}

public static boolean slotFull(SongRecord[] songs, int x) {
    boolean slotFull = true;
    if (new Playlist().songs[x].getTitle() == "" && new Playlist().songs[x].getArtist() == ""
            && new Playlist().songs[x].getSongLengthMinutes() == 0
            && new Playlist().songs[x].getSongLengthSeconds() == 0) {
        slotFull = false;
    }
    return slotFull;

}

}

这是播放列表

public class Playlist  {
public Playlist() {
    for (int x = 0; x < 50; x++) {
        songs[x].setTitle("");
        songs[x].setArtist("");
        songs[x].setSongLengthMinutes(0);
        songs[x].setSongLengthSeconds(0);
    }
}

public Object clone() {

}

public boolean equals(Object obj) {

}

public int size() {

}

public void addSong() {

    final JFrame newSong = new JFrame("New Song");
    newSong.setVisible(true);
    newSong.pack();
    newSong.setSize(450, 230);
    JPanel songSettings = new JPanel();
    newSong.add(songSettings);

    JLabel titleLabel = new JLabel("Enter the song title: ");
    songSettings.add(titleLabel);
    final JTextField titleEnter = new JTextField(20);
    songSettings.add(titleEnter);

    JLabel artistLabel = new JLabel("Enter the song artist: ");
    songSettings.add(artistLabel);
    final JTextField artistEnter = new JTextField(20);
    songSettings.add(artistEnter);

    JLabel minutesLabel = new JLabel(
            "Enter the song length (minutes): ");
    songSettings.add(minutesLabel);
    final JTextField minutesEnter = new JTextField(20);
    songSettings.add(minutesEnter);

    JLabel secondsLabel = new JLabel(
            "Enter the song length (seconds): ");
    songSettings.add(secondsLabel);
    final JTextField secondsEnter = new JTextField(20);
    songSettings.add(secondsEnter);

    JLabel positionLabel = new JLabel("Enter the position: ");
    songSettings.add(positionLabel);
    final JTextField positionEnter = new JTextField(20);
    songSettings.add(positionEnter);

    JButton enter = new JButton("Enter");
    songSettings.add(enter);

    enter.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (!positionEnter.getText().isEmpty()
                    && !titleEnter.getText().isEmpty()
                    && !artistEnter.getText().isEmpty()
                    && !minutesEnter.getText().isEmpty()
                    && !secondsEnter.getText().isEmpty()) {
                String positionString = positionEnter.getText();
                int position = Integer.parseInt(positionString);

                String title = titleEnter.getText();
                songs[position].setTitle(title);

                String artist = artistEnter.getText();
                songs[position].setArtist(artist);

                String songLengthMinutesString = minutesEnter
                        .getText();
                int songLengthMinutes = Integer
                        .parseInt(songLengthMinutesString);
                songs[position].setSongLengthMinutes(songLengthMinutes);

                String songLengthSecondsString = secondsEnter
                        .getText();
                int songLengthSeconds = Integer
                        .parseInt(songLengthSecondsString);
                songs[position].setSongLengthSeconds(songLengthSeconds);
                newSong.dispose();

                final JFrame songAdded = new JFrame("Song Added");
                songAdded.setVisible(true);
                songAdded.pack();
                songAdded.setSize(250, 300);
                JPanel songAddedPanel = new JPanel();
                songAdded.add(songAddedPanel);
                JLabel addedSongText = new JLabel(
                        "You have added the song " + title);
                songAdded.add(addedSongText);

                JButton okay = new JButton("Okay");
                songAdded.add(okay);
                okay.setPreferredSize(new Dimension(100, 27));

                okay.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        songAdded.dispose();

                    }
                });
            } else {
                final JFrame enterText = new JFrame(
                        "Please Enter Text");
                JPanel enterTextPanel = new JPanel();
                JLabel enterTextLabel = new JLabel(
                        "Please enter text in " + "all the fields.");
                JButton okayEnterText = new JButton("Okay");
                enterText.setVisible(true);
                okayEnterText.setPreferredSize(new Dimension(100,
                        27));
                enterText.add(enterTextPanel);
                enterTextPanel.add(enterTextLabel);
                enterTextPanel.add(okayEnterText);
                enterText.pack();
                enterText.setSize(300, 200);
                okayEnterText
                        .addActionListener(new ActionListener() {

                            public void actionPerformed(
                                    ActionEvent e) {
                                enterText.dispose();

                            }
                        });
            }

        }
    });

    JButton cancel = new JButton("Cancel");
    songSettings.add(cancel);
    enter.setPreferredSize(new Dimension(100, 27));
    cancel.setPreferredSize(new Dimension(100, 27));
    cancel.addActionListener (new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            newSong.dispose();

        }
    {;
}
 ;
});

}

public void removeSong(int position) {

}

public SongRecord getSong(int position) {

}

public void printAllSongs() {

}

public static Playlist getSongsByArtist(Playlist originalList, String artist) {

}

public String toString() {
    System.out.print("test");
    String a ="Song#";
    String b ="Title";
    String c ="Artist";
    String d ="Length";
    String e ="------------------------------------------------";
    String finalString = String.format("%-21s%-26s%19s%06d", a, b, c, 
            d)+ "\n"+e;
    for(int x =0;x<50;x++){
        if(PlaylistOperations.slotFull(songs, x)){
            finalString+= "\n" + String.format("%-21s%-26s%19s%06d", x, new SongRecord().getTitle(), new SongRecord().getArtist(), 
                     new SongRecord().getSongLengthMinutes() +":"+new SongRecord().getSongLengthSeconds());
    }
        }
    return finalString;
}

final int arraySize = 50;
SongRecord[] songs = new SongRecord[arraySize];

}
4

1 回答 1

3

对于初学者:

..new SongRecord().getTitle()..

其余的应该是以下形式:

..this.getTitle()..

更多提示:

  1. 为了尽快获得更好的帮助,请发布SSCCE
  2. 对代码块使用一致且合乎逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
  3. 请参阅使用多个 JFrame,好/坏做法?
  4. 不要设置顶级容器的大小。而是布局内容并调用pack()
于 2013-09-07T07:11:06.830 回答