我将从如何使用文件选择器开始,但这里org.netbeans.swing.outline.Outline
讨论的示例很吸引人。
附录:@Gilbert Le Blanc 对 Swing 的易用性和可移植性提出了一个很好的观点。相比之下,SWT 需要稍微多一点的精力来部署,但一些用户更喜欢更高的保真度,如此处org.eclipse.swt.widgets.FileDialog
所示。
附录:我注意到它FileDialog
显示了一个看起来更原生的窗口,如此处所示。您可以在目标平台上试用它。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** @see https://stackoverflow.com/questions/2914733 */
public class FileDialogTest {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1));
frame.add(new JButton(new AbstractAction("Load") {
@Override
public void actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
fd.setVisible(true);
System.out.println(fd.getFile());
}
}));
frame.add(new JButton(new AbstractAction("Save") {
@Override
public void actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(frame, "Test", FileDialog.SAVE);
fd.setVisible(true);
System.out.println(fd.getFile());
}
}));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}