在文本框Java GUI中显示字符串并停止写入

时间:2011-12-06 05:15:20

标签: java swing user-interface textbox

好的,所以我正在写一个基本的Mp3播放器程序。我需要它来执行以下操作,我遇到了一些麻烦。 首先,我按照我想要的方式使用按钮和GUI,功能优先。我需要能够显示我在艺术家/专辑/曲目下的文本字段中输入的字符串,以显示在左侧的列表框中....所以也许我可以发布一些我的代码和某人可以给我一个如何让它工作的例子.... 有人能告诉我一个如何连接用户输入并在GUI中显示的例子......这里显示的第一个代码是管理程序,第二个部分是处理用户输入的mp3程序。 .sorry我知道这很多。我很感激帮助。感谢。

private JButton addMP3Button, displayMP3sButton, findMP3Button, deleteMP3Button, 
                albumButton, artistButton, songButton, lengthButton;            
private JLabel artistLabel, songLabel, albumLabel, trackLengthLabel;
private JTextField artistField, songField, albumField, trackLengthField;
private JPanel buttonPanel, fieldPanelArtist, fieldPanelSong,
                fieldPanelAlbum, fieldPanelTrackLength;
{


    artistLabel = new JLabel("Artist Name ");
    artistField = new JTextField(20);


    fieldPanelArtist = new JPanel();
    fieldPanelArtist.setLayout(new FlowLayout());
    fieldPanelArtist.add(artistLabel);
    fieldPanelArtist.add(artistField);

    songLabel = new JLabel("Song Title ");
    songField = new JTextField(20);

    fieldPanelSong = new JPanel();
    fieldPanelSong.setLayout(new FlowLayout());
    fieldPanelSong.add(songLabel);
    fieldPanelSong.add(songField);

    albumLabel = new JLabel("Album Name ");
    albumField = new JTextField(20);

    fieldPanelAlbum = new JPanel();
    fieldPanelAlbum.setLayout(new FlowLayout());
    fieldPanelAlbum.add(albumLabel);
    fieldPanelAlbum.add(albumField);

    trackLengthLabel = new JLabel("Track Length (in seconds) ");
    trackLengthField = new JTextField(20);

    fieldPanelTrackLength = new JPanel();
    fieldPanelTrackLength.setLayout(new FlowLayout());
    fieldPanelTrackLength.add(trackLengthLabel);
    fieldPanelTrackLength.add(trackLengthField);


    JPanel inputPanel = new JPanel();
    inputPanel.setPreferredSize( new Dimension( 200, 200 ) );
    //setPreferredSize( );
    inputPanel.setLayout(new GridLayout(4, 1, 5, 5));
    inputPanel.add(fieldPanelArtist);
    inputPanel.add(fieldPanelSong);
    inputPanel.add(fieldPanelAlbum);
    inputPanel.add(fieldPanelTrackLength);


    //add mp3 file to the text field
    addMP3Button = new JButton(" Add MP3 ");

    //display the list of mp3 files already loaded
    displayMP3sButton = new JButton("Display MP3s");
    //find a certain mp3 file based on key word
    findMP3Button = new JButton(" Find MP3 ");
    //delete a file from the list
    deleteMP3Button = new JButton("Delete MP3");


    //sort functions here
    albumButton = new JButton(" Album ");
    //sort by album-alphabetical
    artistButton = new JButton(" Artist ");
    //sort by artist- alphabetical
    songButton = new JButton(" Song ");
    //songs
    lengthButton = new JButton(" Length ");
    //longest track


    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(2, 2, 2, 2));
    buttonPanel.add(addMP3Button);
    buttonPanel.add(displayMP3sButton);
    buttonPanel.add(findMP3Button);
    buttonPanel.add(deleteMP3Button);
    buttonPanel.setPreferredSize( new Dimension( 200, 50 ) );

    JPanel sortPanel = new JPanel();
    sortPanel.setLayout(new GridLayout(2, 2, 2, 2));
    sortPanel.add(artistButton);
    sortPanel.add(albumButton);
    sortPanel.add(songButton);
    sortPanel.add(lengthButton);
    sortPanel.setPreferredSize( new Dimension( 200, 50 ) );


    JPanel RightPanel = new JPanel();
    RightPanel.setLayout(new FlowLayout( FlowLayout.RIGHT, 2, 2 ));

    RightPanel.add(inputPanel); 
    RightPanel.add(buttonPanel); 



    JTextArea textArea = new JTextArea(12, 20);
    textArea.setLineWrap(false);
    JScrollPane scrollPane = new JScrollPane(textArea);


    JPanel LeftPanel = new JPanel();
    LeftPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
    LeftPanel.add(textArea);
    LeftPanel.add(sortPanel);
    LeftPanel.add(scrollPane);
    textArea.setVisible(true);
    textArea.setBackground(Color.WHITE);

    JPanel displayPanel = new JPanel();

    Container container = getContentPane();
    container.setLayout(new GridLayout(1, 2, 5, 5));
    container.add(LeftPanel);
    container.add(RightPanel);




    //add(container, BorderLayout.NORTH );

    //JTextArea textArea = new JTextArea();

    //add(textArea, BorderLayout.CENTER);


    addMP3Button.addActionListener(this);

    displayMP3sButton.addActionListener(this);

    findMP3Button.addActionListener(this);

    deleteMP3Button.addActionListener(this);



}

private String artist;
private String song;
private String album;
private int trackLength;

public Mp3(String artistName, String songName, String albumName,
        int trackLeng) {
    setArtist(artistName);
    setSong(songName);
    setAlbum(albumName);
    setTrackLength(trackLeng);
}

public void setArtist(String artistName) {
    artist = artistName;
}

public String getArtist() {
    return artist;
}

public void setSong(String songName) {
    song = songName;
}

public String getSong() {
    return song;
}

public void setAlbum(String albumName) {
    album = albumName;
}

public String getAlbum() {
    return album;
}

public void setTrackLength(int trackLeng) {
   trackLength = (trackLeng > 0) ? trackLeng : 5 ;
}

public int getTrackLength() {
    return trackLength;
}

public String toString() {
    return String.format("%s, %s, %s, %d : %d", getArtist(), getSong(),
            getAlbum(), getTrackLength() / 60, getTrackLength() % 60);
}

}

0 个答案:

没有答案