简体   繁体   English

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

[英]java gui using the split function to display in a text area

Hi im trying to use the split function within a textarea so it will only display certain information to the user, currently I tried to use the typical method used in general programming into the GUI interface however I believe I implemented it incorrectly. 您好,我试图在文本区域内使用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);
    }
} 

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) {

Currently you open the BufferedReader , read something with it and close it directly. 当前,您打开BufferedReader ,用它读取某些内容,然后直接将其关闭。 Then again you want to read again using the same reader you just closed br.readLine() . 再一次,您想使用刚刚关闭br.readLine()的同一阅读器再次阅读。

br.close(); should be done in a finally block 应该在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);

The point of using the read(...) method of the JTextArea is to read data from a file and add the data to the text area. 使用JTextArea的read(...)方法的重点是从文件中读取数据并将数据添加到文本区域。

So after invoking that method all the data in the file has already been read. 因此,调用该方法后,文件中的所有数据都已被读取。

If you want to parse the data BEFORE adding the data to the text area, then you should NOT be using the read(...) method. 如果要在将数据添加到文本区域之前解析数据,则不应使用read(...)方法。 Instead you just read each line of data from the file and then you use the append(...) method of the JTextArea to add the data. 相反,您只是从文件中读取数据的每一行,然后使用JTextArea的append(...)方法添加数据。

if (st.contains("TITLE"))
    title = st.split(":");

Also, I don't know what the format of your file is but what do you expect that code to do. 另外,我不知道文件的格式是什么,但是您希望该代码执行什么操作。 Every time you read a line containing TITLE you create a new array. 每次阅读包含TITLE的行时,都会创建一个新数组。 I suspect instead you should create the array to get the data and then use the append(...) method to add the data to the text area. 我怀疑您应该创建数组以获取数据,然后使用append(...)方法将数据添加到文本区域。

But without a clear requirement we are only guessing. 但是没有明确的要求,我们只是猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM