0

第一次在这里发帖。这是我的问题:我无法保存或加载配置文件。我正在尝试根据我的代码中指定的玩家数量来保存玩家名称。有谁知道出了什么问题以及如何解决?配置文件:

public void savePlayerConfiguration(String key, int value) {
    String path = "config.xml";
    try {
        File file = new File(path);
        boolean exists = file.exists();
        if (!exists) {
            file.createNewFile();
        }
        OutputStream write = new FileOutputStream(path);
        properties.setProperty(key, Integer.toString(value));
        properties.storeToXML(write, key);
    } catch (Exception e) {

    }
}

public void loadPlayerConfiguration(String path) {
    try {
        InputStream read = new FileInputStream(path);
        properties.loadFromXML(read);
        String player1 = properties.getProperty("1");
        String player2 = properties.getProperty("2");
        String player3 = properties.getProperty("3");
        String player4 = properties.getProperty("4");
        String player5 = properties.getProperty("5");
        String player6 = properties.getProperty("6");
        read.close();

    } catch (FileNotFoundException e) {
        savePlayerConfiguration("1", 1);
        savePlayerConfiguration("2", 2);
        savePlayerConfiguration("3", 3);
        savePlayerConfiguration("4", 4);
        savePlayerConfiguration("5", 5);
        savePlayerConfiguration("6", 6);
        loadConfiguration(path);
    } catch (IOException e) {

    }
}

选项文件:

 private int width = Main.width;
private int height = Main.height;

private String player1 = "Player1", player2 = "Player2",
        player3 = "Player3", player4 = "Player4", player5 = "Player5",
        player6 = "Player6";
private String[] playerNames = { player1, player2, player3, player4,
        player5, player6 };
private int[] player = { 1, 2, 3, 4, 5, 6 };
private int playerTotal;
private JButton OK;
private JTextField input1, input2, input3, input4, input5, input6;
private JTextField[] playerNameInput = { input1, input2, input3, input4,
        input5, input6 };
private JLabel playerName;
private Rectangle rOK, rPlayerAmount;
private Choice playerAmount = new Choice();
Configuration config = new Configuration();

private int button_width = 80;
private int button_height = 40;

JPanel window = new JPanel();

public Database() {
    setTitle("Database - Excelteor Launcher");
    setSize(new Dimension(width, height));
    add(window);
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
    window.setLayout(null);
    drop();

    drawButtons();
    window.repaint();
}

private void drawButtons() {
    OK = new JButton("OK");
    rOK = new Rectangle((width - 100), (height - 70), button_width,
            button_height);
    OK.setBounds(rOK);
    window.add(OK);

    rPlayerAmount = new Rectangle(30, 130, 80, 25);
    playerAmount.setBounds(rPlayerAmount);
    playerAmount.add("1");
    playerAmount.add("2");
    playerAmount.add("3");
    playerAmount.add("4");
    playerAmount.add("5");
    playerAmount.add("6");
    playerAmount.select(1);
    window.add(playerAmount);

    playerName = new JLabel("Player Names:");
    playerName.setBounds(30, 110, 120, 20);
    window.add(playerName);

    for (int i = 0; i < playerTotal; i++) {
        playerNameInput[i] = new JTextField();
        playerNameInput[i].setBounds(80, 150 + i * 20, 60, 20);
        window.add(playerNameInput[i]);
    }

    OK.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            dispose();
            System.out.println("test");
            for (int i = 0; i < playerTotal; i++) {
                config.savePlayerConfiguration(parsePlayers(), player[i]);
                System.out.println(player[i]);
            }
        }
    });

}

private void drop() {
    int selection = playerAmount.getSelectedIndex();
    if (selection == 0) {
        playerTotal = 1;
    }
    if (selection == 1 || selection == -1) {
        playerTotal = 2;
    }
    if (selection == 2) {
        playerTotal = 3;
    }
    if (selection == 3) {
        playerTotal = 4;
    }
    if (selection == 4) {
        playerTotal = 5;
    }
    if (selection == 5) {
        playerTotal = 6;
    }

}

private String parsePlayers() {
    try {
        for (int i = 0; i < playerTotal; i++) {
            playerNames[i] = playerNameInput[i].toString();
            return playerNames[i];
        }
    } catch (NumberFormatException e) {
        drop();
        return player1;
    }
    return player1;
}
4

1 回答 1

0

我的猜测是您的 config.xml 的路径,即传递给 loadPlayerConfiguration(String path) 的路径是错误的。尝试以下选项来解决您的问题。

  1. 确保“路径”正确。
  2. 使用 File API 的 exists() 方法确保“路径”存在。
  3. 如果路径是相对的,则确认路径结构是否已添加到类路径中。否则,请尝试将其放在项目的根目录中。

如果您在尝试这些步骤后仍然遇到问题,请分享您的项目结构。

干杯,马杜。

** 编辑 ** @user2399785 :通过您的 config.xml 后对您的问题有了清晰的认识。

<properties>
<comment>javax.swing.JTextField[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@69ecade2,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]</comment>
<entry key="javax.swing.JTextField[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@69ecade2,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]">2</entry>
<entry key="javax.swing.JTextField[,80,150,60x20,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@2904a7cf,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]">2</entry>
</properties>

如果您注意到您的 .xml,则 KEY 包含某种较长的 javax swing 详细信息。但是 loadPlayerConfiguration(String path) 使用properties.getProperty("2")它将返回一个 KEY 为“2”的值,反之亦然。由于没有带有“2”的 KEY,因此您将返回值为 NULL。使用 .xml 中的 KEY 尝试相同的代码,你应该没问题。

干杯,马杜。

于 2013-05-19T21:54:46.360 回答