gpt4 book ai didi

java - JTable对象[][]数据

转载 作者:行者123 更新时间:2023-12-01 10:44:46 25 4
gpt4 key购买 nike

我正在尝试根据我的试用类(class)创建一个表。当我尝试运行我的表类时,表会打印正确的标题,但它不会在表正文中打印任何内容。我应该如何修复 for 循环以便数据进入表?我需要向构造函数添加参数吗?

我知道 getter 和 setter 的数量超出了必要的范围。我只是想以不同的方式进行 for 循环。

public class Trial extends JFrame{

static int counter = 0;
int laps = 0;
String lapTime;
double currentTime = 0;
String difference;
int lapsCount = 0;
String[] array1;
double[] array2;
double[] array3;

public int getLapsCount() {
return lapsCount;
}

public void setLapsCount(int lapsCount) {
this.lapsCount = lapsCount;
}

public static int getCounter() {
return counter;
}

public static void setCounter(int counter) {
Trial.counter = counter;
}

public int getLaps() {
return laps;
}

public void setLaps(int laps) {
this.laps = laps;
}

public String getLapTime() {
return lapTime;
}

public void setLapTime(String lapTime) {
this.lapTime = lapTime;
}

public double getCurrentTime() {
return currentTime;
}

public void setCurrentTime(double currentTime) {
this.currentTime = currentTime;
}

public String getDifference() {
return difference;
}

public void setDifference(String difference) {
this.difference = difference;
}

public String[] getArray1() {
return array1;
}

public void setArray1(String[] array1) {
this.array1 = array1;
}

public double[] getArray2() {
return array2;
}

public void setArray2(double[] array2) {
this.array2 = array2;
}

public double[] getArray3() {
return array3;
}

public void setArray3(double[] array3) {
this.array3 = array3;
}

public static void main(String[] args) {

//drop down for number of laps
String lapsString;
String[] selections = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
lapsString = (String) JOptionPane.showInputDialog(null,
"Choose a number of laps:",
"Lap Counter",
JOptionPane.INFORMATION_MESSAGE,
null, /* icon */
selections,
"0");
System.out.printf("Number of laps = \"%s\"\n", lapsString);

int lapsCount = Integer.parseInt(lapsString);
String[] array1 = new String[lapsCount+1];
double[] array2 = new double[lapsCount+1];
double[] array3 = new double[lapsCount+1];

// showInputDialog with for lap time
for (int i = 1; i <= lapsCount; i++){
String lapTime;
lapTime =
JOptionPane.showInputDialog("Enter lap " + i + " total time.");
System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
array1[i] = lapTime;
double currentTime = inputConvert(array1[i]);
array2[i] = currentTime;
array3[i] = (array2[i] - array2[i-1]);
counter++;

//confirm each time
int result;
String[] results = { "Yes", "No", "Cancel" };

result = JOptionPane.showConfirmDialog(null,
"Please confirm lap " + i + " is " + lapTime);
if (result == -1)
System.out.printf("user closed window\n");
else if (result == 0)
System.out.printf("result = %d, user pressed \"%s\"\n", result, results[result]);
else {
lapTime =
JOptionPane.showInputDialog("Please re-enter lap " + i);
System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
counter++;
}
}
}

public static double inputConvert(String s) {
double convert = 0.0;
int minutes = Integer.parseInt(s.substring(0, 1));
double seconds = Double.parseDouble(s.substring(2, s.length()));
minutes = minutes * 60;
convert = minutes + seconds;
return convert;

}

private static String getDifference(int j, double sign) {
String difference = "";
if(j == 0) {
difference = "---";
}
else if(sign > 0.0) {
difference = "+";
}
return difference;
}

public static String displayTime(double time) {
String result = null;
int minutes = (int) (time/60);
double seconds = (time % 60);
seconds = Math.round(seconds*100.0)/100.0;
if(seconds < 10){
result = minutes + ":0" + seconds;
}
else{
result = minutes + ":" + seconds;
}

return result;
}
}

这是我的表类出现问题的地方。

public class table { 

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new table();
}
});
}

public table() {
JFrame guiFrame = new JFrame();

guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Table to record lap times");
guiFrame.setSize(700,200);

guiFrame.setLocationRelativeTo(null);

JTable table = new JTable(new ExampleTableModel());

table.setAutoCreateRowSorter(true);

table.setGridColor(Color.YELLOW);
table.setBackground(Color.CYAN);

JScrollPane tableScrollPane = new JScrollPane(table);

guiFrame.add(tableScrollPane);
guiFrame.setVisible(true);

}


class ExampleTableModel extends AbstractTableModel{

int laps = 0;
String lapTime;
double currentTime = 0;
String difference;

Trial t = new Trial();

//Two arrays used for the table data
private String[] columnNames = {"Lap #", "Total Time", "Lap Time" , "Difference" };
private Object[][] data;

public ExampleTableModel(){

data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = t.getArray1() [i];
data[i][2] = t.getArray2()[i];
data[i][3] = t.getArray3()[i];
}
}

@Override public int getRowCount() {
return data.length;
}

@Override public int getColumnCount() {
return columnNames.length;
}

@Override public Object getValueAt(int row, int column) {
return data[row][column];
}


@Override public String getColumnName(int column) {
return columnNames[column];
}

@Override public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

@Override public boolean isCellEditable(int row, int column) {
if (column == 0 || column == 1) {
return false;
}
else {
return true;
}
}
}
}

我做了这些更改,但我需要我的输出看起来像这样圈数:总时间 单圈时间差第一圈:2:10 2:10 ---

我修复了圈数,使其从 1 开始。我进行这些更改是希望能够修复圈数时间和差异单元格,但它给了我一个错误。

公共(public)ExampleTableModel(试用版){

        int laps = trial.getLapsCount();
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
if (i == 0){
data[i][0] = "Lap " + (i + 1);
data[i][1] = trial.getArray1() [i + 1];
data[i][2] = trial.getArray2() [i + 1];
data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i]));
}

else {
data[i][0] = "Lap " + (i + 1);
data[i][1] = trial.getArray1() [i + 1];
data[i][2] = trial.displayTime(trial.getArray2() [i + 1] - trial.getArray2() [i]);
data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i])) + (trial.getArray3()[i + 1] - trial.getArray3()[i]);

}
}
}

最佳答案

编辑:组合 Trial.mainTable.main 方法中的代码

为了使 Trial 类中的代码更容易在 Table 类中使用,您可以进行以下更改。这样您就可以创建一个 Trial 对象并调用 getUserInput 让用户初始化它:

public static void main(String[] args) {
new Trial().getUserInput();
}

public void getUserInput() {
// [Code from the old main method...]

// Use the fields of the class instead of local variables:
lapsCount = Integer.parseInt(lapsString);
array1 = new String[lapsCount+1];
array2 = new double[lapsCount+1];
array3 = new double[lapsCount+1];

// [Code from the old main method...]
}

现在您可以像这样修改Table模型:

public Table() {
// [...]

Trial trial = new Trial();
trial.getUserInput();
JTable table = new JTable(new ExampleTableModel(trial));

// [...]
}

并让 ExampleTableModel 构造函数使用 trail 参数(不再使用 lapst 字段):

public ExampleTableModel(Trial trial){
int laps = trial.getLapsCount();
data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = trial.getArray1()[i];
data[i][2] = trial.getArray2()[i];
data[i][3] = trial.getArray3()[i];
}
}

getColumnClass 方法也存在问题:

@Override public Class getColumnClass(int column) {
//return getValueAt(0, column).getClass();
return (column <= 1) ? String.class : Double.class;
}

现在表格如下所示:

Screenshot of table

输出是:

Number of laps = "2"
Lap 1 = "2:10"
result = 0, user pressed "Yes"
Lap 2 = "2:20"
result = 0, user pressed "Yes"

旧答案

似乎有一些问题导致表格无法填充:

ExampleTableModel 类中,laps 字段应大于零(以便构造函数填充 data 数组):

class ExampleTableModel extends AbstractTableModel {

//int laps = 0;
int laps = 6;

ExampleTableModel类的构造函数中:

        public ExampleTableModel(){

data = new Object[laps][4];
for(int i = 0; i < laps; i++){
data[i][0] = "Lap " + i;
data[i][1] = t.getArray1()[i];
data[i][2] = t.getArray2()[i];
data[i][3] = t.getArray3()[i];
}
}

t.getArray1t.getArray2t.getArray3 方法的调用返回 nullTrial 对象似乎还没有数据。

您希望程序同时执行 Trial.main 方法和 Table.main 方法的代码吗?

关于java - JTable对象[][]数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com