我是Java的新手。我创建了一个用户表单,一个骰子对象和一个包含dicetypes的xml文件。我能够解析XML并将类型存储在列表中,但无法弄清楚如何使用dicetype名称填充JCombox。我想我正在寻找一些关于我所缺少的东西的解释。
以下是我的代码:
package dicebag;
import java.awt.EventQueue;
import dicebag.ui.userInterface;
import dicebag.util.parseXML;
/**
* @author troy.a.pilewski
*/
public class mainApp {
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
parseXML parseXML = new parseXML();
parseXML.parseXML();
parseXML.displayDice();
System.out.println(parseXML.getDice());
parseXML.getDice();
userInterface frame = new userInterface();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
package dicebag.util;
import java.util.Random;
/**
* @author troy.a.pilewski
*/
public class dice {
String type;
int sides;
/**
* @param type
* @param sides
*/
public dice(String type, int sides) {
this.type = type;
this.sides = sides;
}
/**
* Random Generator for rolling one dice
*
* @param sides
* @return value
*/
public int roll(int sides) {
Random randomGen = new Random();
int value = randomGen.nextInt(sides) + 1;
return value;
}
public String toString() {
return type;
}
}
package dicebag.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author troy.a.pilewski
*/
public class parseXML {
/**
* Variable declarations
*/
public static final String xmlFilePath = "./src/dicebag/util/bag.xml";
@SuppressWarnings("javadoc")
public static List<dice> dice = new ArrayList<dice>();
/**
*
*/
public void parseXML() {
try {
File xmlFile = new File(xmlFilePath);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmlDoc = db.parse(xmlFile);
System.out.println("Root Element: " + xmlDoc.getDocumentElement().getNodeName());
System.out.println("===============================");
if (xmlDoc.hasChildNodes()) {
printNodeList(xmlDoc.getChildNodes());
}
} catch (Exception e) {
System.out.println(e);
}
}
private static void printNodeList(NodeList nodeList) {
// TODO Auto-generated method stub
for (int count = 0; count < nodeList.getLength(); count++) {
Node elemNode = nodeList.item(count);
if (elemNode.getNodeType() == Node.ELEMENT_NODE) {
// Get node name and value
System.out.println("\nNode Name: " + elemNode.getNodeName());
System.out.println("Node Content: " + elemNode.getTextContent());
if (elemNode.hasAttributes()) {
NamedNodeMap nodeMap = elemNode.getAttributes();
Node xmlNode = nodeMap.item(0);
System.out.println("Attribute Name: " + xmlNode.getNodeName());
System.out.println("Attribute Value: " + xmlNode.getNodeValue());
String type = xmlNode.getNodeValue();
String temp = elemNode.getTextContent().substring(elemNode.getTextContent().lastIndexOf('e') + 1);
int sides = Integer.parseInt(temp.replaceAll("\\n\\t", ""));
dice.add(new dice(type, sides));
}
if (elemNode.hasChildNodes()) {
// Recursive call if there are child nodes
printNodeList(elemNode.getChildNodes());
}
}
}
}
/**
*
*/
public void displayDice() {
for (int count = 0; count < dice.size(); count++) {
if (count < dice.size() - 1) {
System.out.print(dice.get(count) + ", ");
} else {
System.out.println(dice.get(count));
}
}
}
/**
* @param dice
* @return dice
*/
public List<dice> getDice() {
return dice;
}
}
package dicebag.ui;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import dicebag.util.dice;
import net.miginfocom.swing.MigLayout;
/**
* @author troy.a.pilewski
*/
@SuppressWarnings("serial")
public class userInterface extends JFrame {
private static final String dice = null;
private JPanel contentPane;
private JTextField txtDiceQty;
private JTextField txtRollMod;
private JTextField txtRollTotal;
private JTable tblRolls;
/**
* Create the frame.
*/
public userInterface() {
setTitle("Dicebag");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[:75px:75px][:75px:75px][:75px:75px][:175px:175px]",
"[][][][][][][][][][][][][][][]"));
JLabel lblRollTotal = new JLabel("Roll Total: ");
contentPane.add(lblRollTotal, "cell 2 0,alignx leading,growy");
txtRollTotal = new JTextField();
txtRollTotal.setEditable(false);
contentPane.add(txtRollTotal, "cell 3 0,growx");
txtRollTotal.setColumns(10);
JLabel lblDiceQty = new JLabel("Dice Qty: ");
contentPane.add(lblDiceQty, "cell 0 1,alignx leading,growy");
txtDiceQty = new JTextField();
contentPane.add(txtDiceQty, "cell 1 1,growx");
txtDiceQty.setColumns(10);
tblRolls = new JTable();
tblRolls.setRowSelectionAllowed(false);
contentPane.add(tblRolls, "cell 2 1 2 14,grow");
JLabel lblDiceType = new JLabel("Dice Type: ");
contentPane.add(lblDiceType, "cell 0 2,alignx leading,growy");
JComboBox cboDiceType = new JComboBox();
List<dice> dice = new ArrayList<dice>();
for (dice d : dice) {
cboDiceType.addItem(d);
}
contentPane.add(cboDiceType, "cell 1 2,growx");
JLabel lblRollModifier = new JLabel("Roll Modifier: ");
contentPane.add(lblRollModifier, "cell 0 3,alignx leading,growy");
txtRollMod = new JTextField();
contentPane.add(txtRollMod, "cell 1 3,growx");
txtRollMod.setColumns(10);
JButton btnRoll = new JButton("Roll Dice");
contentPane.add(btnRoll, "cell 0 4 2 1,grow");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<bag>
<dice name="D3"><sides>3</sides>
</dice>
<dice name="D4"><sides>4</sides>
</dice>
<dice name="D6"><sides>6</sides>
</dice>
<dice name="D8"><sides>8</sides>
</dice>
<dice name="D10"><sides>10</sides>
</dice>
<dice name="D12"><sides>12</sides>
</dice>
<dice name="D20"><sides>20</sides>
</dice>
<dice name="D%"><sides>100</sides>
</dice>
</bag>
有人可以给我一点指示吗?另外,我正在使用Eclipse IDE。