1

我创建了一个JTable具有定制模型和特征的模型。我有一个很长的书面课程,可以正确执行设置和渲染。我看到该.getSelectedRows()方法不起作用,并且永远不会评估为值。我试图获取选择的第一行和最后一行的索引。这是我的代码。

public void actionPerformed(ActionEvent event) {

    String command = event.getActionCommand();

    rows = table.getSelectedRows();

    firstRow = rows[0];

    int rowCount = rows.length;

    lastRow = rows[(rowCount - 1)];

    if (command.equals("Run Threw")) {

    }else if (command.equals("Shield Bash")) {
        this.attack(firstRow, lastRow, command);
    }

        public boolean block (int defendersRow) {
}

public boolean fumble (int attackersRow) {   
}

public boolean dodge (int defendersRow) {
}

public boolean critical (int attackersRow, int attackRoll) {
}


public void attack(int firstRow, int lastRow, String command) {
    command = this.command;
    firstRow = this.firstRow;
    lastRow = this.lastRow;

    if (command == "Bludgeon" || command == "React" || command == "ShieldBash") {
        attackersRow = this.lastRow;
        defendersRow = this.firstRow;
    }else if(command == "Attack" || command == "Skill") {
        attackersRow = this.firstRow;
        defendersRow = this.lastRow;
    }else {

    }
    table.setValueAt(true, attackersRow, 16);
    fumbled = this.fumble(attackersRow);
    if (fumbled == true) {
        outputString = "fumbled";
    }
    Object maxDamageObject = table.getValueAt(attackersRow, 10);
    int maxDamage = (Integer) maxDamageObject;
    attackRoll = generator.nextInt(100) + 1;
    this.critical(attackersRow, attackRoll);
    if (criticaled == true) {
        outputString = "criticaled";
    }
    dodged = this.dodge(defendersRow);
    if (dodged == true) {
        outputString = "dodged";
    }
    blocked = this.block(defendersRow);
    if (blocked == true) {
        outputString = "blocked";
    }
    int defenseRoll = generator.nextInt(100) + 1;
    Object attackBaseObject = table.getValueAt(attackersRow, 6);
    Object defenseBaseObject = table.getValueAt(defendersRow, 11);
    int attackBase = (Integer) attackBaseObject;
    int defenseBase = (Integer) defenseBaseObject;
    int attack = attackRoll + attackBase;
    int defense = defenseRoll + defenseBase;
    Object minDamageObject = table.getValueAt(attackersRow, 9);
    int minDamage = (Integer) minDamageObject;
    damage = generator.nextInt((maxDamage - minDamage))+minDamage;

    if (criticaled == true) {
        damage = maxDamage * 2;
    }else if (attack >= (defense + 50)) {
        damage = damage * 2;
    }else if (attack >= defense) {
        damage = damage;
    }else {
        damage = 0;
    }

    this.outputSelection(outputString, attackersRow, defendersRow, command, damage);
    this.damage(defendersRow, damage);
}

private void damage(int defendersRow, int damage) {
}

private void outputSelection(String outputString, int attackersRow, int defendersRow, String command, int damage) {


}
4

2 回答 2

2

我没有看到代码有问题。也许您可以交叉检查以下几点:

  1. 你能选择单行/多行吗?或者检查 jTableObject.getRowSelectionAllowed() 返回什么
  2. 仅在选择行后才调用您的代码。您的代码应在选择某些行后执行,以便它可以返回您的预期值。
  3. 调用上述代码的监听器是否添加到表中。

小点但最好交叉检查:)。

于 2013-01-16T05:47:28.820 回答
2

如这个工作示例所示,您可以将 a 添加ListSelectionListener到您的表格中ListSelectionModel,看看是否有任何结果。它可能有助于确定出问题的地方:例如,table遮蔽预期引用的本地引用。

于 2013-01-16T06:58:24.440 回答