我正在创建一个游戏,我正在做一个字符选择屏幕,其中有一个用于输入用户名的 JTextField,屏幕还有一个 JButton“创建字符”,当按下它时将解析 JTextField,如果有任何问题(空格在名称,以空格开头,等等。)它会将一些文本放在 JTextField 旁边的 JLabel 中。
我将 JButton 连接到一个 actionPerformed 方法,该方法调用一个解析数据的函数。我的问题是每次按下按钮时,都会在原始 JLabel 之上放置一个新标签。
我这样创建 JLabel:
public static JLabel label;
label = new JLabel();
// some properties
我这样调用解析方法:
parseCharacterSelection(nameInput, label);
其中 nameInput 是 JTextField。我的问题是,为什么每次按下 JButton 时都会创建一个新标签
public void parseCharacterCreation(JTextField input, JLabel label) {
// Variable that hold whether or not
// the User Name is acceptable
player_name = input.getText();
// Make some initial checks on the
// input string
//----------------------------------
if( player_name.isEmpty() ) {
label.setText("You need to pick a username");
} else if( player_name.startsWith(" ") ) {
label.setText("Name Cannot start with a space");
} else {
pass = true;
}
// Attempt to write to file
// Catch some errors
//----------------------------------
if(pass == true) {
try {
if( player_name.contains(" ") ) {
player_name = player_name.replaceAll("\\s", "_");
player_file = new File("src/Resources/characters/" + player_name + ".properties");
}
if(!player_file.exists()) {
player_file.createNewFile();
}
FileWriter fw = new FileWriter(player_file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(player_name);
bw.close();
} catch( IOException e ) {
e.printStackTrace();
} finally {
System.out.println("DONE");
}
}
}
// ScreenCharacter 类
public class ScreenCharacter extends JPanel {
public static JButton create;
public static JTextField nameInput;
public static JLabel errorLabel;
public ScreenCharacter() {
this.setLayout(null);
this.add(createUserName());
this.add(createMainArea());
}
private static JPanel createUserName() {
MatteBorder matteBorder = new MatteBorder(1, 0, 1, 0, Color.decode("#222222"));
EmptyBorder emptyBorder = new EmptyBorder(10, 75, 10, 10);
CompoundBorder border = new CompoundBorder(matteBorder, emptyBorder);
JPanel userNameArea = new JPanel();
userNameArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
userNameArea.setBounds(0, 35, Engine.screenX, 50);
userNameArea.setBorder(border);
userNameArea.setBackground(new Color(255, 255, 255, 70));
JLabel nameLabel = new JLabel();
nameLabel.setText("Enter Your Username: ");
nameInput = new JTextField();
nameInput.setForeground(Color.black);
nameInput.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
nameInput.setPreferredSize(new Dimension(200, 25));
errorLabel = new JLabel();
errorLabel.setText("This is a test label");
errorLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
errorLabel.setPreferredSize(new Dimension(200, 25));
errorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
userNameArea.add(nameLabel);
userNameArea.add(nameInput);
userNameArea.add(errorLabel);
return userNameArea;
}
private static JPanel createMainArea() {
JPanel resetCreatePanel = new JPanel(new GridLayout(1, 2));
resetCreatePanel.setPreferredSize(new Dimension(300, 30));
resetCreatePanel.setBackground(Color.black);
create = new JButton("Create Character");
create.setBorder(BorderFactory.createLineBorder(Color.decode("#171717")));
create.setFont(new Font("Dialog", 1, 11));
create.setBackground(Color.black);
create.setForeground(Color.white);
create.setFocusPainted(false);
create.setContentAreaFilled(false);
create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(!errorLabel.getText().isEmpty()) {
errorLabel.setText("");
} else {
Engine.p_parser.parseCharacterCreation(nameInput, errorLabel);
}
}
});
resetCreatePanel.add(create);
}
}
// 引擎
public class Engine {
public static PlayerParser p_parser;
public Engine() {
p_parser = new PlayerParser();
}
}