我正在尝试制作一个表格,您可以为其中定义的每一列输入值,MyTableModel
然后使用角色的选择来根据游戏规则的定义进行攻击。
我想:
- 一旦每回合使用了一个动作,单元格就无法编辑和无法使用的单元格
- 为每个怪物保留一个回合计数器和一个威胁表
JTable
在系统运行攻击时使用 Java swings显示和收集数据- 尝试配置通过游戏文本显示战斗进展的显示器。
这是我到目前为止的代码。我想知道为什么我会收到不兼容类型的错误-当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();
}
});
}
}