如何根据用户输入生成JButton

我想创建一个Java Swing GUI程序,该程序允许用户在GUI上上传他/她的图片。如果用户使用文件选择器选择多张图片,则该窗口将生成包含用户所选文件的按钮。就像在Facebook上上传图片时一样。

还有其他方法可以做到这一点吗?

最佳答案

您可以执行以下操作:

JFileChooser jfc = new JFileChooser();
File[] files = jfc.getSelectedFiles();
jfc.setMultiSelectionEnabled(true);
jfc.showOpenDialog(null);

if ( files != null && files.length > 0) {
    for ( File file : files ) {
        layoutmanager.add(new JButton("Filename")); // Or anything else you want to do with the files/buttons
    }
}

07-24 18:54
查看更多