Java无法加载XML数据

时间:2017-02-23 03:24:50

标签: java xml java-8 netbeans-8

我有一个Java应用程序,它运行XML文件并将数据加载到组合框中。当应用程序通过NetBeans 8.2运行时,所有代码都可以正常运行,但执行完清理和构建并从JAR文件组合框运行后,该代码为空。请帮我找出错误。以下是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dicebag;

import java.awt.EventQueue;
import dicebag.gui.userInterface;
import dicebag.util.parseXML;

/**
 *
 * @author Troy Pilewski
 */
public class main {

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(userInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    parseXML parseXML = new parseXML();
                    parseXML.parseXML();
                    parseXML.displayDice();
                    System.out.println(parseXML.getDice());
                    new userInterface().setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

        });

    }

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dicebag.gui;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import dicebag.util.dice;
import dicebag.util.parseXML;
import dicebag.util.rolls;


/**
 *
 * @author Troy Pilewski
 */
public class userInterface extends javax.swing.JFrame {

    /**
     * Creates new form userInterface
     */
    public userInterface() {
        initComponents();
    }



    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        pnlLeft = new javax.swing.JPanel();
        lblDiceQty = new javax.swing.JLabel();
        txtDiceQty = new javax.swing.JTextField();
        lblDiceType = new javax.swing.JLabel();
        cboDiceType = new javax.swing.JComboBox<>();
        lblRollMod = new javax.swing.JLabel();
        txtRollMod = new javax.swing.JTextField();
        btnRoll = new javax.swing.JButton();
        pnlRight = new javax.swing.JPanel();
        lblRollTotal = new javax.swing.JLabel();
        txtRollTotal = new javax.swing.JTextField();
        srlPaneRolls = new javax.swing.JScrollPane();
        tblRolls = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("d20 Dicebag");
        setName("frameMain"); // NOI18N

        pnlLeft.setName("pnlLeft"); // NOI18N
        pnlLeft.setPreferredSize(new java.awt.Dimension(225, 0));

        lblDiceQty.setText("Dice Qty:");
        lblDiceQty.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        lblDiceQty.setName(""); // NOI18N

        txtDiceQty.setText("1");
        txtDiceQty.setName("txtDiceQty"); // NOI18N

        lblDiceType.setText("Dice Type:");
        lblDiceType.setName("lblDiceType"); // NOI18N

        cboDiceType.setEditor(null);
        cboDiceType.setName("cboDiceType"); // NOI18N
        parseXML parseXML = new parseXML();
        List<dice> bag = parseXML.getDice();
        for (dice d : bag) {
            cboDiceType.addItem(d.toString());
        }

        lblRollMod.setText("Roll Modifier:");
        lblRollMod.setName("lblRollMod"); // NOI18N

        txtRollMod.setText("0");
        txtRollMod.setName("txtRollMod"); // NOI18N

        btnRoll.setText("Roll Dice");
        btnRoll.setName("btnRoll"); // NOI18N
        btnRoll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRollActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pnlLeftLayout = new org.jdesktop.layout.GroupLayout(pnlLeft);
        pnlLeft.setLayout(pnlLeftLayout);
        pnlLeftLayout.setHorizontalGroup(
            pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlLeftLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(btnRoll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, pnlLeftLayout.createSequentialGroup()
                        .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblRollMod, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, lblDiceQty, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, lblDiceType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)))
                        .add(10, 10, 10)
                        .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(txtRollMod)
                            .add(cboDiceType, 0, 123, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, txtDiceQty))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlLeftLayout.setVerticalGroup(
            pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlLeftLayout.createSequentialGroup()
                .add(37, 37, 37)
                .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblDiceQty)
                    .add(txtDiceQty, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblDiceType)
                    .add(cboDiceType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlLeftLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblRollMod)
                    .add(txtRollMod, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnRoll)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblDiceQty.getAccessibleContext().setAccessibleDescription("");

        pnlRight.setName("pnlRight"); // NOI18N

        lblRollTotal.setText("Roll Total:");
        lblRollTotal.setName("lblRollTotal"); // NOI18N

        txtRollTotal.setEditable(false);
        txtRollTotal.setName("txtRollTotal"); // NOI18N

        srlPaneRolls.setToolTipText("");
        srlPaneRolls.setName("srlPaneRolls"); // NOI18N

        tblRolls.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Roll", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblRolls.setToolTipText("");
        tblRolls.setName("tblRolls"); // NOI18N
        tblRolls.getTableHeader().setReorderingAllowed(false);
        srlPaneRolls.setViewportView(tblRolls);
        if (tblRolls.getColumnModel().getColumnCount() > 0) {
            tblRolls.getColumnModel().getColumn(0).setResizable(false);
            tblRolls.getColumnModel().getColumn(1).setResizable(false);
        }

        org.jdesktop.layout.GroupLayout pnlRightLayout = new org.jdesktop.layout.GroupLayout(pnlRight);
        pnlRight.setLayout(pnlRightLayout);
        pnlRightLayout.setHorizontalGroup(
            pnlRightLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlRightLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlRightLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlRightLayout.createSequentialGroup()
                        .add(srlPaneRolls, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 238, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(pnlRightLayout.createSequentialGroup()
                        .add(lblRollTotal)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(txtRollTotal)))
                .addContainerGap())
        );
        pnlRightLayout.setVerticalGroup(
            pnlRightLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlRightLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlRightLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblRollTotal)
                    .add(txtRollTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(srlPaneRolls, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 317, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(pnlLeft, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlRight, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlRight, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlLeft, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void btnRollActionPerformed(java.awt.event.ActionEvent evt) {                                        
        String dieType = cboDiceType.getSelectedItem().toString();
        int diceSideQty;
        int rollTotal = 0;
        int sumTotal = 0;
        ArrayList<rolls> rollList = new ArrayList<rolls>();
        DefaultTableModel dtm = (DefaultTableModel) tblRolls.getModel();
        if (Integer.parseInt(txtDiceQty.getText()) == 1) {
            try {
                diceSideQty = Integer.parseInt(cboDiceType.getSelectedItem().toString().substring(1));
//                System.out.println(dieType + " Has " + diceSideQty + " sides.");
                int dieRoll = dice.roll(diceSideQty);
//                System.out.println(dieRoll);
                rollTotal = dieRoll + Integer.parseInt(txtRollMod.getText());
                txtRollTotal.setText(Integer.toString(rollTotal));
                rollList.add(new rolls("Roll 1", dieRoll));                
                if (dtm.getRowCount() > 0) {
                    dtm.setRowCount(0);
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
                        String rollQty = rollList.get(rlCount).getRollQty();
                        int rollValue = rollList.get(rlCount).getRollValue();

                        Object[] roll = {rollQty, rollValue};

                        dtm.addRow(roll);

                    }

                } else {
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
            String rollQty = rollList.get(rlCount).getRollQty();
                        int rollValue = rollList.get(rlCount).getRollValue();

                        Object[] roll = {rollQty, rollValue};

                        dtm.addRow(roll);
                    }

                }

            } catch (Exception exe) {
                diceSideQty = 100;
//      System.out.println(dieType + " Has " + diceSideQty + " sides.");
                int dieRoll = dice.roll(diceSideQty);
//                System.out.println(dieRoll);
                rollTotal = dieRoll + Integer.parseInt(txtRollMod.getText());
                txtRollTotal.setText(Integer.toString(rollTotal));
                rollList.add(new rolls("Roll 1", dieRoll));
                if (dtm.getRowCount() > 0) {
                    dtm.setRowCount(0);
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
                        String rollQty = rollList.get(rlCount).getRollQty();
            int rollValue = rollList.get(rlCount).getRollValue();

            Object[] roll = { rollQty, rollValue };

            dtm.addRow(roll);

                    }
                } else {
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
            String rollQty = rollList.get(rlCount).getRollQty();
                        int rollValue = rollList.get(rlCount).getRollValue();

            Object[] roll = { rollQty, rollValue };

            dtm.addRow(roll);
                    }

                }

            }

        } else if (Integer.parseInt(txtDiceQty.getText()) > 1) {
            try {
                diceSideQty = Integer.parseInt(cboDiceType.getSelectedItem().toString().substring(1));
//      System.out.println(dieType + " Has " + diceSideQty + " sides.");
        for (int rlCount = 0; rlCount < Integer.parseInt(txtDiceQty.getText()); rlCount++) {
                    rollList.add(new rolls("Roll " + (rlCount + 1), dice.roll(diceSideQty)));
                    System.out.print("Roll " + rlCount + ": ");
                    System.out.println(Integer.toString(rollList.get(rlCount).getRollValue()));
                    sumTotal = 0;
                    for (int val = 0; val < rollList.size(); val++) {
                        sumTotal += rollList.get(val).getRollValue();

                    }
                    System.out.print("The total for all rolls is: ");
                    System.out.println(sumTotal);

                }
                if (dtm.getRowCount() > 0) {
                    dtm.setRowCount(0);
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
                        String rollQty = rollList.get(rlCount).getRollQty();
                        int rollValue = rollList.get(rlCount).getRollValue();

                        Object[] roll = { rollQty, rollValue };

                        dtm.addRow(roll);

                    }

                } else {
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
                        String rollQty = rollList.get(rlCount).getRollQty();
                        int rollValue = rollList.get(rlCount).getRollValue();

                        Object[] roll = { rollQty, rollValue };

                        dtm.addRow(roll);
                    }

                }
                rollTotal = sumTotal + Integer.parseInt(txtRollMod.getText());
        txtRollTotal.setText(String.valueOf(rollTotal));
            } catch (Exception exe) {
                diceSideQty = 100;
//      System.out.println(dieType + " Has " + diceSideQty + " sides.");
        for (int rlCount = 0; rlCount < Integer.parseInt(txtDiceQty.getText()); rlCount++) {
                    rollList.add(new rolls("Roll " + (rlCount + 1), dice.roll(diceSideQty)));
                    System.out.print("Roll " + rlCount + ": ");
                    System.out.println(Integer.toString(rollList.get(rlCount).getRollValue()));
                    sumTotal = 0;
                    for (int val = 0; val < rollList.size(); val++) {
            sumTotal += rollList.get(val).getRollValue();

                    }

                    System.out.print("The total for all rolls is: ");
                    System.out.println(sumTotal);

                }
                if (dtm.getRowCount() > 0) {
                    dtm.setRowCount(0);
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
                        String rollQty = rollList.get(rlCount).getRollQty();
            int rollValue = rollList.get(rlCount).getRollValue();

            Object[] roll = { rollQty, rollValue };

            dtm.addRow(roll);

                    }
                } else {
                    for (int rlCount = 0; rlCount < rollList.size(); rlCount++) {
            String rollQty = rollList.get(rlCount).getRollQty();
            int rollValue = rollList.get(rlCount).getRollValue();

            Object[] roll = { rollQty, rollValue };

            dtm.addRow(roll);

                    }

                }
                rollTotal = sumTotal + Integer.parseInt(txtRollMod.getText());
        txtRollTotal.setText(String.valueOf(rollTotal));
            }

        }

    }                                       

    // Variables declaration - do not modify                     
    public javax.swing.JButton btnRoll;
    public javax.swing.JComboBox<String> cboDiceType;
    public javax.swing.JLabel lblDiceQty;
    public javax.swing.JLabel lblDiceType;
    public javax.swing.JLabel lblRollMod;
    public javax.swing.JLabel lblRollTotal;
    public javax.swing.JPanel pnlLeft;
    public javax.swing.JPanel pnlRight;
    public javax.swing.JScrollPane srlPaneRolls;
    public javax.swing.JTable tblRolls;
    public javax.swing.JTextField txtDiceQty;
    public javax.swing.JTextField txtRollMod;
    public javax.swing.JTextField txtRollTotal;
    // End of variables declaration                   
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dicebag.util;

import java.util.Random;

/**
 *
 * @author Troy Pilewski
 */
public class dice {

    private String type;
    private int sides;

    public dice(String type, int sides) {
        this.type = type;
        this.sides = sides;

    }

    public static int roll(int sides) {
        Random randomGen = new Random();
        int value = randomGen.nextInt(sides) + 1;
        return value;

    }

    @Override
    public String toString() {
        return type;

    }

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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;
import dicebag.util.messageBox;


/**
 *
 * @author Troy Pilewski
 */
public class parseXML {

    public static final String xmlFilePath = "./src/res/bag.xml";
    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);
            if (xmlDoc.hasChildNodes()) {
                printNodeList(xmlDoc.getChildNodes());

            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static void printNodeList(NodeList nodeList) {
        for(int count = 0; count < nodeList.getLength(); count++) {
            Node elemNode = nodeList.item(count);
            if(elemNode.getNodeType() == Node.ELEMENT_NODE) {
                if(elemNode.hasAttributes()) {
                    NamedNodeMap nodeMap = elemNode.getAttributes();
                    Node xmlNode = nodeMap.item(0);
                    String type = xmlNode.getNodeValue();
                    String inputString = elemNode.getTextContent().substring(elemNode.getTextContent().lastIndexOf("e") + 1);
//                    inputString = inputString.replaceAll("\\t", "");
//                    inputString = inputString.replaceAll("\\n", "");
//                    char[] inputCharacters = inputString.toCharArray();
//                    for (char c : inputCharacters) {
//                        System.out.print(c);
//                    }

                    int sides = Integer.parseInt(inputString.replaceAll("[\\t\\n\\s]", ""));
                    dice.add(new dice(type, sides));

                }
                if (elemNode.hasChildNodes()) {
                    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));

            }

        }

    }

    public List<dice> getDice() {
        return dice;

    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dicebag.util;

/**
 *
 * @author Troy Pilewski
 */
public class rolls {
    private String rollQty;
    private int rollValue;

    public rolls(String rollQty, int rollValue) {
        this.rollQty = rollQty;
        this.rollValue = rollValue;

    }

    public String getRollQty() {
        return rollQty;

    }

    public int getRollValue() {
        return rollValue;

    }

}


感谢您的帮助......

0 个答案:

没有答案