日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了如何從txt文件中讀取單獨的部分以在Java圖形用戶界面中顯示?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

因此here like this picture我需要在txt文件中顯示我擁有的票證的統計信息。我在我的文件中使用","分隔了4個字段,但我不知道如何在圖形用戶界面中分別顯示它們,即,對于Ticket Price下的4個字段,我需要顯示價格,這是我的txt文件中每行的第二部分,對于票據金額,我需要顯示文件中每行的第三部分。那么我該怎么向他們展示呢?我已經設置了一個演示它應該是什么樣子,對于圖片中的每一種類型,我需要為txt文件中的每一種類型顯示類似的線條。因此,基本上我需要顯示From&Quot;VIP-AC,30$,66,30/10/2020&Quot;tothis,以此類推(對于下一行)

這是我到目前為止的代碼:

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

推薦答案

讀取分隔文本文件中的數據并在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
" +
            "A:1
" +
            "B:2
" +
            "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") );

這篇關于如何從txt文件中讀取單獨的部分以在Java圖形用戶界面中顯示?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:txt 單獨 圖形 文件 顯示 用戶界面 讀取
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定