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