gpt4 book ai didi

java - JFileChooser 未显示

转载 作者:行者123 更新时间:2023-12-01 17:46:19 25 4
gpt4 key购买 nike

我有一个方法,将 txt 文件作为输入。我曾经通过键入文件的直接路径来使用字符串。但每当我尝试使用不同的文件作为输入时,它就变得很麻烦。我尝试实现 JFileChooser 但没有成功。

这是代码,但什么也没发生。

public static JFileChooser choose;
File directory = new File("B:\\");
choose = new JFileChooser(directory);
choose.setVisible(true);
File openFile = choose.getSelectedFile();

FileReader fR = new FileReader(openFile);
BufferedReader br = new BufferedReader(fR);

最佳答案

根据 How to Use File Choosers 上的 Java 教程:

Bringing up a standard open dialog requires only two lines of code:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

The argument to the showOpenDialog method specifies the parent component for the dialog. The parent component affects the position of the dialog and the frame that the dialog depends on.

注意as per docs它也可以是:

int returnVal = fc.showOpenDialog(null);

If the parent is null, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.

另请阅读 Concurrency in Swing如果您还没有的话。

关于java - JFileChooser 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14640103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com