gpt4 book ai didi

java - AbstractTableModel 中的对象初始化

转载 作者:行者123 更新时间:2023-12-01 09:41:59 25 4
gpt4 key购买 nike

我正在开发一个小程序,它应该在 JTable 中显示食谱。一切似乎都工作正常,我遇到的唯一问题是我无法初始化应该正确保存数据的对象。这是让我头疼的类(class):

class RecipeTableModel extends AbstractTableModel {

private String[] columnNames = {"Number", "Name", "Difficulty",
"Preparation", "PreparationTime", "Quantity", "Incredients"};
Recipe recipe1 = new Recipe("1", "SecretRecipe", "easy", "microwave", "20min", "100gr", "flour");
Recipe recipe2 = new Recipe("2", "SuperRecipe", "medium", "microwave", "30min", "100gr", "Salt");
**RecipeDB recipeDB = new RecipeDB();
recipeDB.addRecipe (Recipe recipe1);**


@Override
public int getColumnCount() {
return 7;
}

public int getRowCount() {
//return recipeDB.getRecipeDBSize();
return 15;
}

@Override
public Object getValueAt(int row, int col) {

/* Recipe recipe = recipeDB.getRecipe(row);
switch (col){
case 0:
return recipe.getNumber();
case 1:
return recipe.getName();
case 2:
return recipe.getPrep();
case 3:
return recipe.getPrepTime();
case 4:
return recipe.getDifficulty();
case 5:
return recipe.getIngredients();
}
*/
return null;
}

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

当我尝试将食谱添加到 recipeDB 时,出现语法错误;当我调试时,我看到 recipeDB 被初始化为 null 值。这怎么可能?

这是我的另一个类:

import java.util.ArrayList;

public class RecipeDB {

public ArrayList<Recipe> recipeArraylist;

public RecipeDB() {
recipeArraylist = new ArrayList<Recipe>();
}

public void addRecipe(Recipe recipe) {
recipeArraylist.add(recipe);
}

public int getRecipeDBSize() {
return recipeArraylist.size();
}

public Recipe getRecipe(int i) {
return recipeArraylist.get(i);
}

public ArrayList getDBArrayList() {
return recipeArraylist;
}
}

最佳答案

语法错误来自于尝试调用 class body declaration 中的 recipeDB 上的实例方法。 RecipeTableModel 部分。

RecipeDB recipeDB = new RecipeDB();  // legal declaration and initialization
recipeDB.addRecipe (Recipe recipe1); // illegal method invocation

该方法调用在 instance initializer 中是合法的或构造函数。在下面的示例中,两者都不是必需的;使用 addRecipe() 方法将 Recipe stub 的实例添加到循环中。

image

import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

class RecipeTableModel extends AbstractTableModel {

private final String[] columnNames = {"Number", "Name", "Difficulty",
"Preparation", "Prep Time", "Quantity", "Ingredients"};
private final RecipeDB recipeDB = new RecipeDB();

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

@Override
public int getRowCount() {
return recipeDB.getRecipeDBSize();
}

@Override
public Object getValueAt(int row, int col) {
return "(" + row + ", " + col + ")";
}

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

private static class RecipeDB {

public ArrayList<Recipe> recipeArraylist = new ArrayList<Recipe>();

public void addRecipe(Recipe recipe) {
recipeArraylist.add(recipe);
}

public int getRecipeDBSize() {
return recipeArraylist.size();
}

public Recipe getRecipe(int i) {
return recipeArraylist.get(i);
}

public ArrayList getDBArrayList() {
return recipeArraylist;
}
}

private static class Recipe {}

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
RecipeTableModel model = new RecipeTableModel();
for (int i = 0; i < 42; i++) {
model.recipeDB.addRecipe(new Recipe());
}
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(new JTable(model)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

关于java - AbstractTableModel 中的对象初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382470/

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