2013-01-08 65 views
-1

我正在嘗試製作一個表,輸入MyTableModel中定義的每列的值,然後使用角色選擇按遊戲規則進行定義的攻擊。JTable特定遊戲計算器

我想:

  • 細胞所困擾的細胞無法修改,無法使用,一旦它每轉
  • 一轉計數器和威脅表保存每個怪物使用它的一個動作
  • 使用Java搖擺JTable顯示和收集數據,因爲系統通過攻擊
  • 運行,試圖配置顯示通過遊戲文本的戰鬥進展的顯示。

這是我到目前爲止的代碼。我想知道爲什麼我收到不兼容類型的錯誤 - 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(); 
      } 
     }); 
    } 
} 
+2

哦,我的上帝:請格式化您的源代碼,以減少眼睛的痛苦。另外,請嘗試將您的問題重新格式化爲2-3段。這會有很大幫助。 – andr

回答

3

檢查你的這種說法在的actionPerformed方法

int HP = table.getValueAt(, 3);

我想你錯過了什麼之前 「」;

並經getValueAt方法建議@Hovercraft你是返回一個Object,所以你需要將它轉換回整數。

編輯:

不關你得到的錯誤,但只是一個忠告

你爲什麼要重新聲明的變量(一些關鍵點已被@Hovercraft告知)在actionPerformed裏面?只聲明一次。

3

1+ to sansix for catch。

此外,

  • 如果表格單元格持有整數類型,那麼你就需要轉換的getValue(...)返回整數對象。
  • 但你的JTable的模式持有Integer對象,而是一個String對象,所以你的企圖迫使它進入一個int會失敗不管。你會想解決這個問題。
  • 永遠不要將字符串與==進行比較。使用equals(...)equalsIgnoreCase(...)方法,這樣你就可以對內容相等比較字符串不反對平等(同一個String 對象
  • 您的代碼應遵循Java的命名標準,其中的類名稱以大寫字母開頭的變量名,方法等以小寫字母開頭。當您希望讓其他人更容易理解您的代碼時,這一點非常重要 - 例如us
+0

用於查看「getValueAt」的返回類型更深的+1以及對OP的很好的建議 – exexzian

+0

您能解釋爲什麼我上面描述的錯誤不能更詳細地工作嗎?我將getValueAt的返回值作爲整數進行轉換,並且它仍然返回相同的錯誤。謝謝你的時間。 – user1956201

+0

@ user1956201:再次,它不是*整數。查看您要添加到JTable的數據類型 - 我可以看到的只有Strings,如果將String轉換爲Integer會發生什麼 - 您將得到一個轉換異常。考慮通過'Integer.parseInt(...)'將字符串解析爲int。 –