[英]java gui using the split function to display in a text area
您好,我试图在文本区域内使用split函数,因此它将仅向用户显示某些信息,目前,我尝试将一般编程中使用的典型方法用于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);
}
}
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
块中完成
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);
}
jTextArea1.read(br, null);
使用JTextArea的read(...)方法的重点是从文件中读取数据并将数据添加到文本区域。
因此,调用该方法后,文件中的所有数据都已被读取。
如果要在将数据添加到文本区域之前解析数据,则不应使用read(...)方法。 相反,您只是从文件中读取数据的每一行,然后使用JTextArea的append(...)
方法添加数据。
if (st.contains("TITLE"))
title = st.split(":");
另外,我不知道文件的格式是什么,但是您希望该代码执行什么操作。 每次阅读包含TITLE
的行时,都会创建一个新数组。 我怀疑您应该创建数组以获取数据,然后使用append(...)
方法将数据添加到文本区域。
但是没有明确的要求,我们只是猜测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.