简体   繁体   English

如何从 txt 文件中读取单独的部分以在 Java GUI 中显示?

[英]How do I read separate parts from a txt File to show in Java GUI?

So here like this picture I need to show the stats of the tickets I have in a txt file.所以像这张图片一样,我需要在txt文件中显示我拥有的门票的统计信息。 I've separated the 4 fields using ", " in my file but I don't know how to show them separately in GUI, ie for the 4 fields there under Ticket price I need to show the prices which is the 2nd part of every line in my txt file, and for Ticket Amounts the 3rd part of every line from the file.我在文件中使用“,”分隔了 4 个字段,但我不知道如何在 GUI 中分别显示它们,即对于 Ticket price 下的 4 个字段,我需要显示价格,这是每个字段的第二部分我的 txt 文件中的行,以及文件中每行的第 3 部分的票证金额。 So how do I show them?那么我如何向他们展示呢? And I have set an Demo of how should it look like and for every type in the picture I need to show similar lines for every type that's in the txt file.我已经设置了一个演示,它应该是什么样子,对于图片中的每种类型,我需要为txt文件中的每种类型显示类似的行。 So basically I need to show from "VIP-AC, 30$, 66, 30/10/2020" to this and so on (for the next lines)所以基本上我需要从“VIP-AC, 30$, 66, 30/10/2020”到这个等等(对于下一行)

and this is my code till now:这是我的代码,直到现在:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;

public class TicketStats extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TicketStats frame = new TicketStats();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public TicketStats() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1016, 566);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    
    JButton backButton = new JButton("Back");
    backButton.setBounds(846, 11, 144, 54);
    backButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
            User user = new User();
            user.setVisible(true);
        }
    });
    contentPane.setLayout(null);
    backButton.setFont(new Font("Tahoma", Font.PLAIN, 16));
    contentPane.add(backButton);
    
    JLabel lblNewLabel = new JLabel("Ticket Info");
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 36));
    lblNewLabel.setBounds(385, 66, 227, 44);
    contentPane.add(lblNewLabel);
    
    JLabel lblNewLabel_1 = new JLabel("Ticket Type");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblNewLabel_1.setBounds(183, 164, 108, 25);
    contentPane.add(lblNewLabel_1);
    
    JLabel lblNewLabel_2 = new JLabel("Ticket Price");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblNewLabel_2.setBounds(358, 164, 108, 25);
    contentPane.add(lblNewLabel_2);
    
    JLabel lblNewLabel_3 = new JLabel("Tickets Left");
    lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblNewLabel_3.setBounds(535, 164, 108, 25);
    contentPane.add(lblNewLabel_3);
    
    JLabel lblNewLabel_4 = new JLabel("Match Date");
    lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblNewLabel_4.setBounds(699, 164, 108, 25);
    contentPane.add(lblNewLabel_4);
    
    JLabel lblNewLabel_5 = new JLabel("VIP-AC");
    lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel_5.setBounds(183, 228, 76, 14);
    contentPane.add(lblNewLabel_5);
    
    JLabel lblNewLabel_6 = new JLabel("30$");
    lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel_6.setBounds(358, 228, 76, 14);
    contentPane.add(lblNewLabel_6);
    
    JLabel lblNewLabel_7 = new JLabel("66");
    lblNewLabel_7.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel_7.setBounds(535, 228, 82, 14);
    contentPane.add(lblNewLabel_7);
    
    JLabel lblNewLabel_8 = new JLabel("30/10/2020");
    lblNewLabel_8.setBounds(699, 228, 76, 14);
    contentPane.add(lblNewLabel_8);
    
    File file = new File("Ticket.txt");
    try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
        StringBuilder sb = new StringBuilder();
        String text;
        
        while((text = reader.readLine()) != null) {
            sb.append(text).append(", ");
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } 
    
    JSeparator separator = new JSeparator();
    separator.setBounds(172, 201, 621, 2);
    contentPane.add(separator);
    
    JSeparator separator1 = new JSeparator();
    separator1.setForeground(Color.BLACK);
    separator1.setOrientation(SwingConstants.VERTICAL);
    separator1.setBounds(664, 164, 2, 283);
    contentPane.add(separator1);
    
    JSeparator separator_1 = new JSeparator();
    separator_1.setOrientation(SwingConstants.VERTICAL);
    separator_1.setForeground(Color.BLACK);
    separator_1.setBounds(487, 164, 2, 283);
    contentPane.add(separator_1);
    
    JSeparator separator_2 = new JSeparator();
    separator_2.setOrientation(SwingConstants.VERTICAL);
    separator_2.setForeground(Color.BLACK);
    separator_2.setBounds(314, 164, 2, 283);
    contentPane.add(separator_2);
    
}
}

A basic example for reading data in a delimited text file and displaying the data in a JTable follows:读取分隔文本文件中的数据并在 JTable 中显示数据的基本示例如下:

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableFromFile extends JPanel
{
    public TableFromFile()
    {
        setLayout( new BorderLayout() );

        JTable table = new JTable( getTableModel() );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private TableModel getTableModel()
    {
        String delimiter = ":";
        DefaultTableModel model = new DefaultTableModel();

        try
        {
            BufferedReader reader = getFileReader();

            //  First line will contain the column names

            String line = reader.readLine();
            model.setColumnIdentifiers( line.split(delimiter) );

            //  Remaining lines in the file will be the data

            while ((line = reader.readLine()) != null)
            {
                model.addRow( line.split(delimiter) );
            }

            reader.close();
        }
        catch(Exception e) { System.out.println(e); }


        return model;
    }

    private BufferedReader getFileReader()
    {
        //  Create data to simulate reading data from a file

        String data =
            "Letter:Number\n" +
            "A:1\n" +
            "B:2\n" +
            "C:3";

        BufferedReader reader = new BufferedReader( new StringReader( data ) );

        //  In your real application the data would come from a file

        //Reader reader = new BufferedReader( new FileReader(...) );

        return reader;
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table From File");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableFromFile() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

The above example uses ":" as the single character delimiter for the strings in the file.上面的示例使用“:”作为文件中字符串的单字符分隔符。

Since you are using two characters as the delimiter you will need to use a slightly more complex regex to do the split.由于您使用两个字符作为分隔符,因此您需要使用稍微复杂的正则表达式来进行拆分。 So your split statement would be:所以你的拆分语句将是:

model.addRow( line.split(":\\s") );

暂无
暂无

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

相关问题 如何在JAVA中读取.txt文件的特定部分 - How to read specific parts of a .txt file in JAVA 如何从txt文件读取Java中的2个特定列? - How do I read 2 particular columns in Java from a txt file? 如何从 Java 中的 txt 文件中读取 2 个特定列? - How do I read 2 particular columns from a txt file in Java? 如何将由ENTER键(在.txt中)分隔的行(从.txt中读取)添加到字符串arrayList的单独元素中? - How do I add lines (read from a .txt) separated by AN ENTER KEY (in .txt) into separate elements of string arrayList? 如何获取我的Hangman Java文件以从.txt文件中读取随机单词? - How do I get my Hangman Java file to read a random word in from a .txt file? 如何在 java 中将 a.txt 文件读入二维数组? - How do I read a .txt file into a 2D array in java? 如何在Java中以表格格式输出从.txt文件读取的数据? - How do I output data that is read from a .txt file in tabular format in Java? 如何从 a.txt 文件中读取内容,然后在 Java 中找到所述内容的平均值? - How do I read content from a .txt file and then find the average of said content in Java? 如何将方法中的字段变量设置为从 Java 中的 .txt 文件读取的数据? - How do I set the field variables in a method to data being read in from a .txt file in Java? 如何使用Apache POI读取Java中的.DOC文件以将图像与文本分开? - How do I use Apache POI to read a .DOC file in Java to separate images from text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM