gpt4 book ai didi

java gui 使用 split 函数在文本区域中显示

转载 作者:行者123 更新时间:2023-12-02 10:12:33 24 4
gpt4 key购买 nike

嗨,我尝试在文本区域中使用分割功能,这样它只会向用户显示某些信息,目前我尝试使用一般编程中使用的典型方法进入 GUI 界面,但我相信我错误地实现了它。

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
String st;
String[] setdate = null;
String[] submission = null;
String[] title = null;
String[] value = null;
try
{
FileReader reader = new FileReader (filename);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();

while ((st = br.readLine()) != null) {
System.out.println(st);
if(st.contains("TITLE"))
title = st.split(":");
if(st.contains("DATE SET"))
setdate = st.split(":");
if(st.contains("SUBMISSION"))
submission = st.split(":");
if(st.contains("VALUE:"))
value = st.split(":");
}
}
catch (Exception e ) {
JOptionPane.showMessageDialog( null, e);
}
}

cuurent display

最佳答案

FileReader reader = new FileReader (filename);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();

while ((st = br.readLine()) != null) {

当前您打开BufferedReader,用它读取一些内容并直接关闭它。然后,您又想使用刚刚关闭的同一个阅读器再次阅读 br.readLine()

br.close(); 应该在 finally block 中完成

try (FileReader reader = new FileReader(filename); BufferedReader br = new BufferedReader(reader)) {
jTextArea1.read(br, null);
jTextArea1.requestFocus();

while ((st = br.readLine()) != null) {
System.out.println(st);
if (st.contains("TITLE"))
title = st.split(":");
if (st.contains("DATE SET"))
setdate = st.split(":");
if (st.contains("SUBMISSION"))
submission = st.split(":");
if (st.contains("VALUE:"))
value = st.split(":");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}

关于java gui 使用 split 函数在文本区域中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54904300/

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