我正在开发一个小型程序,该程序应在JTable
中显示收件人。
似乎一切正常,我唯一的问题是我无法初始化应该正确保存数据的对象。这是让我头疼的课程:
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;
}
}
最佳答案
语法错误来自尝试在recipeDB
的class body declaration部分中的RecipeTableModel
上调用实例方法。
RecipeDB recipeDB = new RecipeDB(); // legal declaration and initialization
recipeDB.addRecipe (Recipe recipe1); // illegal method invocation
方法调用在instance initializer或构造函数中是合法的。在下面的示例中,两者都不是必需的;使用
Recipe
方法在循环中添加addRecipe()
存根的实例。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);
});
}
}