-1

我想通过在另一个类中将其作为实例运行来测试我的代码,但没有任何反应。我过去能够做到这一点,但这次我无法让它发挥作用。我想我只是错过了一些简单的东西,但无法弄清楚。很抱歉,这个问题可能是我独有的,但我迫切需要帮助,而且时间不多了。谢谢

这是我的主要课程的代码

package minorb;

import java.awt.BorderLayout;
import java.awt.Container;
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.JTable;
import javax.swing.table.DefaultTableModel;
import java.util.*;

public class TableAddRows extends JFrame implements ActionListener {

    // Variables -------------------------------------------------------------

    double percOne = (0.1);
    double percTwo = (0.4);
    double percThree = (0.2);
    double percFour = (0.3);
    private JTable table;
    private JButton addRow;
    StudentManagement r = new StudentManagement();
    ArrayList<String> stu = new ArrayList<String>();

    // Methods ---------------------------------------------------------------
    public void TableAddRows() {

        this.getContentPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        r.openFile();
        r.readFile();

        for(String s: stu) {
            String[] line = s.split(",");

            String[] tableCol = { "Name", "FAN", "Score 1", "Score 2", "Score 3", "Score 4", "Final Score", "Grade"  };

            Object[][] data = {
                { line[0], line[1],Double.parseDouble(line[2]),Double.parseDouble(line[3]),
                + Double.parseDouble(line[4]),Double.parseDouble(line[5]),
                    + Double.parseDouble(line[2])*(percOne) + Double.parseDouble(line[3])*(percTwo)+ Double.parseDouble(line[4])*(percThree) + Double.parseDouble(line[5])*(percFour)
                }, 
                {},
                {}
            };

            table = new JTable(new DefaultTableModel(data, tableCol));
            addRow = new JButton("Add Row");
            addRow.addActionListener(this);

            Container cp = getContentPane();
            cp.add(new JScrollPane(table));
            cp.add(addRow, BorderLayout.SOUTH);

            pack();
            this.setVisible(true);
        }
    }

    public void actionPerformed(ActionEvent ev) {
        if (ev.getSource() == addRow) {

            DefaultTableModel model = (DefaultTableModel) table.getModel();

            if (model.getRowCount() < 10) {
                model.addRow(new Object[] {});
            }
        }
    }

    public void pen () {
        new TableAddRows();        
    }

}

这是我用来尝试从其他类运行它的代码

package minorb;

public class Minorb {

    public static void main(String[] args) {

        StudentManagement r = new StudentManagement();
        TableAddRows s = new TableAddRows();

        s.TableAddRows();

    }   
}

Netbeans 发现代码没有错误此外,此应用程序的基本思想是读取 txt 文件并将该数据显示到表中

4

2 回答 2

0

TableAddRows s = new TableAddRows();

s.setVisible(true);

它的工作......但是你的jframe需要更正才能保持美丽

于 2013-11-13T12:24:20.257 回答
0

试试这个,将语句放在 for 循环之外

    for(String s: stu) {     
           pack();
        this.setVisible(true);

}
于 2013-11-13T12:22:45.673 回答