gpt4 book ai didi

java - JFileChooser 不将文件名返回到主方法

转载 作者:行者123 更新时间:2023-12-01 23:45:54 28 4
gpt4 key购买 nike

我对 Swing 和 AWT 还很陌生。虽然我有Java开发经验。我正在尝试使用 JFileChooser 读取文件并获取主方法中的内容。所有 Swing 和 AWT 的大师请帮助我确定我缺少什么。

这是我的代码:

package com.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class HomeScreen extends JFrame
{
private static final long serialVersionUID = -7604272718213756686L;

String fileContent;
final JFileChooser fc = new JFileChooser();
public HomeScreen()
{
super("Home Screen");
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);

// Button to open file
final JButton button = new JButton("Select File");
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int ret = fc.showDialog(button.getParent(), "Open");
if(ret == JFileChooser.APPROVE_OPTION)
{
File f = fc.getSelectedFile();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(f));
String st ="";
StringBuilder sb = new StringBuilder();
while((st=br.readLine())!=null)
{
sb.append(st);
}
fileContent = sb.toString();

} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
);

System.out.println(fc.getSelectedFile()+" _____________________");
add(button);
}
public static void main(String[] args)
{
HomeScreen screen = new HomeScreen();
}
}

即使我选择了包含大量内容的文件,也会打印 null___________________________

最佳答案

您的System.out.println()语句不在适当的代码块中。您将其放入 UI 初始化方法(这里是构造函数)中,而不是放在 actionPerformed 方法中。

现在,还有一些额外的事情需要考虑:

  • 所有与 Swing 相关的操作都必须在 EDT 中完成(例如使用 SwingUtilities.invokeLater)
  • JFrame.setVisible(true) 应该是 UI 初始化代码的最后一个语句
  • 如果加载大文件,则应考虑将文件的加载移至 EDT(事件调度线程)以外的另一个线程,以避免 GUI 卡住(SwingWorker 通常对此很有帮助) )

这是代码的更新版本(我添加了一个文本区域来显示加载的内容):

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class HomeScreen extends JFrame {
private static final long serialVersionUID = -7604272718213756686L;

String fileContent;
final JFileChooser fc = new JFileChooser();

private JTextArea textArea;

public HomeScreen() {
super("Home Screen");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);

// Button to open file
final JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int ret = fc.showDialog(button.getParent(), "Open");
if (ret == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
System.out.println(fc.getSelectedFile() + " _____________________");
BufferedReader br;
try {
br = new BufferedReader(new FileReader(f));
String st = "";
StringBuilder sb = new StringBuilder();
while ((st = br.readLine()) != null) {
sb.append(st);
}
fileContent = sb.toString();
textArea.setText(fileContent);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}
});
textArea = new JTextArea(24, 40);
textArea.setLineWrap(true);
textArea.setEditable(false);
add(new JScrollPane(textArea));
add(button, BorderLayout.SOUTH);
pack();
setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
HomeScreen screen = new HomeScreen();
}
});

}
}

关于java - JFileChooser 不将文件名返回到主方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060889/

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