gpt4 book ai didi

java - java GUI 中 JFileChooser 的问题

转载 作者:行者123 更新时间:2023-12-01 23:11:37 25 4
gpt4 key购买 nike

我正在尝试将 JFileChooser 添加到 JPanel。有时它工作正常,有时它只显示 JPanel,而不显示 JFileChooser 对话框。现在我其实不知道该怎么办。有人可以帮我吗?

这是我的代码(它是一个方法):

public  File chooseFileFromComputer(){

methodPanel = new JPanel();
methodPanel.setLayout(null);


methodFrame = new JFrame();
methodFrame.setTitle("File Chooser");
methodFrame.setVisible(true);

BufferedImage removeJavaImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
methodFrame.setIconImage(removeJavaImage);

methodLabel = new JLabel("Choose a file: ");
methodLabel.setBounds(10, 10, 80, 20);
methodPanel.add(methodLabel);

fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setBounds(10, 35, 550, 500 );
fileChooser.setVisible(true);
add(fileChooser);
/**
* Action Events #********AE*******#
**/

fileChooser.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
if (command.equals(JFileChooser.APPROVE_SELECTION)) {
selectedFile = fileChooser.getSelectedFile();
methodFrame.setVisible(false);
} else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
methodFrame.setVisible(false);
}
}
});

//End of Action Events #________AE_______#


methodPanel.add(fileChooser);
methodFrame.setContentPane(methodPanel);
methodFrame.setResizable(false);
methodFrame.setSize(600, 600);
methodFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

return selectedFile;
}

最佳答案

  1. 您正在使用空布局并在组件上调用 setBounds(...)。虽然对于新手来说,这似乎是创建复杂 GUI 的更好方法,但这是一个谬论,您创建 Swing GUI 的次数越多,您就越学会尊重和使用布局管理器,并且会发现这些生物对创建灵活、美观和实用的界面有很大帮助。需要复杂的 GUI。
  2. 将组件添加到 JFrame,然后才在 JFrame 上调用 setVisible(true)
  3. 如果您将 JFrame 设置为可见和不可见,那么您似乎根本不应该使用 JFrame。也许您确实想使用 JDialog 甚至独立的 JFileChooser 对话框。
<小时/>

编辑
您正在将 JFileChooser 添加到多个容器:

  add(fileChooser); // ******************* here *************

fileChooser.addActionListener( new ActionListener(){
private File selectedFile;

public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
if (command.equals(JFileChooser.APPROVE_SELECTION)) {
selectedFile = fileChooser.getSelectedFile();
methodFrame.setVisible(false);
} else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
methodFrame.setVisible(false);
}
}
});

methodPanel.add(fileChooser); // ******** here *******

你不能这样做。仅将其添加到一个容器中,否则可能无法正确显示或根本不显示。

<小时/>

编辑2

您的方法返回了错误的结果。您返回 selectedFile 变量,但在设置它之前执行此操作,因为它是由 ActionListener 设置的,在该方法返回后很长时间调用该 ActionListener。

解决方案:再次强调,不要在此处使用 JFrame,模态 JDialog 会工作得更好。如果您使用模式对话框并在 ActionListener 完成后返回,您的代码将正常工作。

<小时/>

编辑3
但同样为了我的钱,我只是使用 JFileChooser 作为模式对话框。例如:

  JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose a File");

// don't use null in the method below but rather a reference to your current GUI
int response = fileChooser.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println(file);
}

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

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