-1

MyTableModelで定義された各列に値を入力し、選択したキャラクターを使用して、ゲームのルールで定義された攻撃を行うテーブルを作成しようとしています。

私が欲しい:

  • セルは、ターンごとに 1 つのアクションを使用すると、セルを編集および使用できなくなります
  • 各モンスターのターン カウンターと脅威テーブル
  • JavaスイングJTableを使用して、システムが攻撃を実行するときにデータを表示および収集する
  • ゲームのテキストを通じて戦闘の進行状況を示す表示を構成しようとします。

これは私がこれまでに持っているコードです。found java.lang.Object but expected int互換性のない型のエラーが表示HPされるのはなぜだろうと思っactionPerformed()ていました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

彼のキャッチで sansix に 1+。

また、

  • テーブル セルが Integer 型を保持している場合は、返されたオブジェクトを Integer にキャストする必要がありますgetValue(...)
  • しかし、JTable のモデルInteger オブジェクトではなく String オブジェクトを保持しているため、強制的に int にしようとすると、何があっても失敗します。これを修正する必要があります。
  • 文字列を と比較しないでください==equals(...)またはメソッドを使用して、equalsIgnoreCase(...)オブジェクトの等価性ではなくコンテンツの等価性について文字列を比較できるようにします (同じ Stringオブジェクト)
  • コードは、クラス名が大文字で始まり、変数やメソッドなどの名前が小文字で始まる Java 命名標準に準拠する必要があります。これは主に、私たちのように他の人にあなたのコードをより簡単に理解してもらいたい場合に重要です。
于 2013-01-08T00:30:11.630 に答える
3

actionPerformedメソッドで this ステートメントを確認してください

int HP = table.getValueAt(, 3);

" , "の前に何かを見逃していたようです。

メソッドで@Hovercraftが示唆しているようgetValueAtに、を返しているObjectため、整数にキャストする必要があります。

編集:

発生するエラーとは関係ありませんが、単なるアドバイスです(いくつかの重要なポイントは@Hovercraftによって伝えられています)

内部で変数を再宣言するのはなぜactionPerformedですか? 一度だけ宣言してください。

于 2013-01-08T00:24:03.497 に答える