【问题标题】:How do I read separate parts from a txt File to show in Java GUI?如何从 txt 文件中读取单独的部分以在 Java GUI 中显示?
【发布时间】:2021-01-22 15:35:03
【问题描述】:

所以here like this picture 我需要在 txt 文件中显示我拥有的票的统计信息。我已经在我的文件中使用“,”分隔了 4 个字段,但我不知道如何在 GUI 中分别显示它们,即对于 Ticket price 下的 4 个字段,我需要显示价格,这是每个的第二部分我的 txt 文件中的行,对于 Ticket Amounts 文件中每一行的第三部分。那么我该如何展示它们呢?我已经设置了一个演示,它应该是什么样子,对于图片中的每种类型,我需要为 txt 文件中的每种类型显示类似的行。所以基本上我需要显示从 "VIP-AC, 30$, 66, 30/10/2020" 到 this 等等(下一行)

这是我到目前为止的代码:

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);
    
}
}

【问题讨论】:

    标签: java swing file user-interface jlabel


    【解决方案1】:

    读取分隔文本文件中的数据并在 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();
                }
            });
        }
    }
    

    以上示例使用“:”作为文件中字符串的单字符分隔符。

    由于您使用两个字符作为分隔符,因此您需要使用稍微复杂的正则表达式来进行拆分。因此,您的拆分语句将是:

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

    【讨论】:

    • 实际上我想使用 JField 来显示它们,就像我的票证类型是固定的一样,所以我将创建空的 JFields,它将填充我的 txt 文件中的数据 @stackoverflow.com/users/131872/camickr
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多