-4

我需要从scores.dat文件中读取数据并将其弹出到一个JTable,这里有更多信息:

  1. Scores.dat好像

    Team  Name   Score1    Score2
    Red    John    55        7
    Blue   Michael 33        6
    Green  Burrs   55        5
    

    位置 =C:\Documents\scores.dat

  2. 第一行是列名。所以需要将列名弹出到表的第一行。

  3. Scores.dat是动态的,通过用户操作添加/删除行。

  4. 另外,我需要表格下方的“Top Scorer”按钮,单击该按钮时会突出显示得分最高的行。

这是我的代码:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author singh_000
 */
public class SampleJTableDemo extends JFrame implements ActionListener {

    /**
     * Creates new form OrderMateRequestForm
     */
    public SampleJTableDemo() {
        initComponents();
    }

    private void initComponents() {
        //headers for the table
        String[] columns = new String[]{
            "Team", "Name", "Score1", "Score2"//Todo: pop names from 1st row from scores.dat
        };

        //actual data for the table in a 2d array
        Object[][] data = new Object[][]{
            {"Red", "John", 40.0, 7},
            {"Blue", "Rambo", 70.0, 6},
            {"Gree", "Zorro", 60.0, 7},
            {"Black", "Curran", 70.0, 5},};

        final Class[] columnClass = new Class[]{
            String.class, String.class, Double.class, Integer.class
        };
        //create table model with data
        DefaultTableModel model = new DefaultTableModel(data, columns) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return columnClass[columnIndex];
            }
        };

        JTable table = new JTable(model);

        Container contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());
//        contentPane.add(table);

        JScrollPane scrollPane = new JScrollPane(table);
        contentPane.add(scrollPane);

        JSeparator separator = new JSeparator();
        separator.setVisible(true);
        contentPane.add(separator);
        //add the table to the frame
//        this.add(new JScrollPane(table));
//ToDO: Add buton for top scorer        
JButton button = new JButton("Top Scorer");
        button.setVisible(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        this.setTitle("Score-List");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SampleJTableDemo();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

}
4

1 回答 1

0

当我阅读第一行时,我正在尝试设置团队参数,但由于 Team.setScore1(int) 需要整数,而来自 BufferReader 的 readLine() 包含字符串(列名)

因此,当您阅读第一行时,您会将数据解析为字符串

String[] columnNames = line.split(" ");

当您阅读附加行时,您需要将 String 数据转换为正确的数据类型:

String[] lineData = theNextLine.split(" " );
String team = lineData[0];
String name = lineData[1];
Double score1 = Double.parseDouble( lineData[2] );
Integer score2 = Integer.parseInt( lineData[3] );

现在您可以使用 DefaultTableModel 的 addRow(...) 方法将数据添加到表中。

于 2017-05-11T17:18:04.140 回答