第一次在这里发帖。继承人我的问题:我无法保存或加载我的配置文件。我试图根据我的代码中指定的玩家数量来保存玩家名称。任何人都知道错误以及如何解决? 配置文件:
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;
}
答案 0 :(得分:0)
我的猜测是你的config.xml路径,传递给loadPlayerConfiguration(String路径)的路径是错误的。请尝试以下选项来解决您的问题。
如果在尝试这些步骤后仍然遇到问题,请分享您的项目结构。
干杯, 马杜。
**编辑** @ 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")
,它将返回一个VALUE,其KEY为“2”,反之亦然。由于没有带“2”的KEY,因此返回的值为NULL。尝试使用与.xml中的KEY相同的代码,你应该没问题。
干杯, 马杜。