-1

我正在尝试制作一个表格,您可以为其中定义的每一列输入值,MyTableModel然后使用角色的选择来根据游戏规则的定义进行攻击。

我想:

  • 一旦每回合使用了一个动作,单元格就无法编辑和无法使用的单元格
  • 为每个怪物保留一个回合计数器和一个威胁表
  • JTable在系统运行攻击时使用 Java swings显示和收集数据
  • 尝试配置通过游戏文本显示战斗进展的显示器。

这是我到目前为止的代码。我想知道为什么我会收到不兼容类型的错误-当found java.lang.Object but expected int我认为我应该有一个. 让我知道你的想法,如果你有任何建议可以帮助我完成我的工作。谢谢你的时间。HPactionPerformed()int

import javax.swing.*; 
import javax.swing.table.AbstractTableModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class EncounterCalculator extends JPanel 
                            implements ActionListener { 
    private JTable table;
    private JRadioButton Attack, Skill, Skulk;
    private JTextArea output;
    private ButtonGroup Act;
    int rowSelected;
    int ID;
    String Name;
    int maxHP;
    int HP;
    int maxMP;
    int MP;
    int attackBase;
    int attack;
    int fumbleChance;
    int criticalChance;
    int damage;
    int minDamage;
    int maxDamage;
    int defense;
    int defenseBase;
    int dodge;
    int dodgeRoll;

    public EncounterCalculator() {
        super();
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        table.getSelectionModel().addListSelectionListener(new RowListener());
        table.getColumnModel().getSelectionModel().
            addListSelectionListener(new ColumnListener());
        add(new JScrollPane(table));


        JRadioButton Attack = new JRadioButton("Attack");
        JRadioButton Skill = new JRadioButton("Skill");
        JRadioButton Skulk = new JRadioButton("Skulk");

        add(new JLabel("Act"));
        Act = new ButtonGroup();
        Act.add(Attack);
        Act.add(Skill);
        Act.add(Skulk);

        JPanel actPanel = new JPanel(new GridLayout(0, 1));
        actPanel.add(Attack);
        actPanel.add(Skill);
        actPanel.add(Skulk);
        add(actPanel);

    }

    private JRadioButton addRadio(String text) {
        JRadioButton b = new JRadioButton(text);
        b.addActionListener(this);
        Act.add(b);
        add(b);
        return b;
    }


    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        int rowsSelected[] = table.getSelectedRows();
        int attackersRowSelected = rowsSelected[0];
        int defendersRow = rowsSelected.length - 1;
        int defendersRowSelected = rowsSelected[defendersRow];
        if ("Attack" == command) {
        int HP = table.getValueAt(, 3);
        int damage;
        int attackBase = table.getValueAt(attackersRowSelected, 6);
        int attackRoll = generator.nextInt(100) + 1;
        int fumbleChance = table.getValueAt(attackersRowSelected, 7);
        int criticalChance = table.getValueAt(attackersRowSelected, 8);
            if (attackRoll < fumbleChance){
                damage = 0;
            } else if (attackRoll > criticalChance){
                damage = maxDamage * 2;
            } else {
                damage = generator.nextInt((maxDamage - minDamage)) + minDamage;
            }
        int attack = attackBase + attackRoll;
        int dodge = table.getValueAt(defendersRowSelected, 12);
        int dodgeRoll = generator.nextInt(100) + 1;
        if (dodge > dodgeRoll){
            damage = 0;
        }
        int defenseBase = table.getValueAt(defendersRowSelected, 11);
        int defenseRoll = generator.nextInt(100) + 1;
        int defense = defenseBase + defenseRoll;
        if (attack >= defense){
            HP = HP - damage;
            table.setValueAt(HP,defendersRowSelected, 4);


        }

        } else if ("Skill" == command) {

        } else if("Skulk" == command) {

        } 
    }

    private void outputSelection() {
    }

    private class RowListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting()) {
                return;
            }
        }
    }

    private class ColumnListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting()) {
                return;
            }
        }
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"ID",
                                        "Name",
                                        "maxHP",
                                        "HP",
                                        "maxMP",
                                        "MP",
                                        "Att",
                                        "Fumble",
                                        "Crit", 
                                        "minDam",
                                        "maxDam",
                                        "Def",
                                        "Dodge",
                                        "Foc",
                                        "Res"};
        private Object[][] data = {
            {"1", "Character1", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"2", "Character2", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"3", "Character3", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"4", "Character4", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"5", "Character5", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"6", "Character6", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"7", "Character7", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"8", "Character8", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"9", "Character9", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"10", "Character10", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"11", "Character11", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"12", "Character12", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"13", "Character13", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"14", "Character14", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"15", "Character15", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"16", "Character16", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"17", "Character17", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"18", "Character18", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"19", "Character19", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            {"20", "Character20", "", "", "", "", "", "", "", "", "", "", "", "", ""}
          };

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

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

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

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Disable boldface controls.
        UIManager.put("swing.boldMetal", Boolean.FALSE); 

        //Create and set up the window.
        JFrame frame = new JFrame("EncounterCalculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        EncounterCalculator newContentPane = new EncounterCalculator();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
4

2 回答 2

3

1+ 到 sansix 的收获。

还,

  • 如果表格单元格是整数类型,那么您需要将返回的对象getValue(...)转换为整数。
  • 但是您的 JTable 模型包含 Integer 对象,而是包含 String 对象,因此您将其强制为 int 的尝试无论如何都会失败。你会想要解决这个问题。
  • 永远不要将字符串与==. 使用equals(...)orequalsIgnoreCase(...)方法,以便您可以比较字符串的内容相等性而不是对象相等性(相同的字符串对象
  • 您的代码应遵循 Java 命名标准,其中类名以大写字母开头,变量、方法等的名称以小写字母开头。当您希望其他人更容易理解您的代码时,这一点非常重要——比如我们
于 2013-01-08T00:30:11.630 回答
3

在actionPerformed方法中检查你的这个语句

int HP = table.getValueAt(, 3);

我猜你之前漏掉了一些东西 " , ";

正如@Hovercraft在您的getValueAt方法中所建议的那样,您将返回一个Object,因此您需要将其转换回整数。

编辑:

与您遇到的错误无关,而只是一条建议(@Hovercraft已告知了一些关键点)

你为什么要在里面重新声明你的变量actionPerformed?只声明一次。

于 2013-01-08T00:24:03.497 回答