Loading...
首页问答  

java Swing中如何动态更改JList对象的值并显示

简简单单
简简单单  发布于 2020-12-06 23:12:26 93

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeListener;
import javax.swing.JList;
import javax.swing.JPanel;
import java.awt.Color;

public class MusicPlayerFrame_Test implements ActionListener{

    private JFrame frame;
    JButton button = null;
    JButton button_1 = null;
    JButton button_2 = null;
    JProgressBar progressBar = null;
    JFileChooser chooser = null;
    JList list = null;
    JPanel panel = null;


    File[] files = null;
    FileInputStream fis = null;               //读取文件内容
    BufferedInputStream bis = null;            //缓存字节流

    ArrayList<String> music_ArrayList = new ArrayList();
    String[] music_List = new String[music_ArrayList.size()];



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MusicPlayerFrame_Test window = new MusicPlayerFrame_Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MusicPlayerFrame_Test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        button_1 = new JButton("\u9009\u62E9\u6B4C\u66F2");
        button_1.setBounds(165, 147, 93, 23);
        frame.getContentPane().add(button_1);

        button_2 = new JButton("\u64AD\u653E/\u6682\u505C");
        button_2.setBounds(165, 200, 93, 23);
        frame.getContentPane().add(button_2);

        progressBar = new JProgressBar();
        progressBar.setBounds(10, 237, 414, 14);
        frame.getContentPane().add(progressBar);



        list = new JList(music_List);
        list.setBounds(129, 32, 1, 1);

        panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.add(list);
        panel.setBounds(161, 23, 97, 114);
        frame.getContentPane().add(panel);
        button_1.addActionListener(this);
        button_2.addActionListener(this);
    }

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

        new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(e.getSource() == button_1){

                    chooseMusic();
                    list = new JList(music_List);
                    panel.repaint();

                }
                if(e.getSource() == button_2){


                }
            }

        }).start();



    }

public void chooseMusic(){

        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setMultiSelectionEnabled(true);
        FileNameExtensionFilter filter = new FileNameExtensionFilter(".", "mp3");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            files = chooser.getSelectedFiles();
            for(File file : files){
                music_ArrayList .add(file.getName());
                System.out.println(music_ArrayList);
            }

                music_List = music_ArrayList.toArray(music_List);
                System.out.println(music_List[0]);

        } 
        else if(chooser.getSelectedFiles() == null){
            return;
        }

    }
}
添加附件:文件小于20M 文件格式必须为doc,docx,xls,xlsx,pdf,ppt,pptx,txt,zip,rar,tar,7z,gz
上传
文件格式必须为doc,docx,xls,xlsx,pdf,ppt,pptx,txt,zip,rar,tar,7z,gz,gz
易百纳技术社区
确定要删除此文章、专栏、评论吗?
确定
取消
易百纳技术社区