我想获取输入到文本字段中的字符串并将它们转换为数组的新元素,但是每当我尝试将它们打印出来时,它们仍然注册为 null。如果您可以尝试忽略与问题无关的内容,因为我正在尝试它们,但我无法向数组添加新元素。
这是创建和初始化数组的地方。
public class AssignSeat {
String[] arrangement = new String[12];
public String[] SeatStart() {
arrangement[0] = "Collins";
arrangement[2] = "Faivre";
arrangement[3] = "Kinnard";
arrangement[6] = "Morgans";
arrangement[7] = "Rohan";
arrangement[8] = "Shatrov";
arrangement[9] = "Sword";
arrangement[11] = "Tuckness";
System.out.format("%-15s%-15s%n", "seat", "passenger");
for (int i=0; i<arrangement.length; i++) {
System.out.format("%-15s%-15s%n", i+1, arrangement[i]);
}
System.out.println();
return arrangement;
}
public String[] getArrangement() {
return arrangement;
}
public void setArrangement(String[] arrangement) {
this.arrangement = arrangement;
}
}
这是我试图添加数组元素的地方。特别是第二、第五、第六和第十一。
public void actionPerformed(ActionEvent event) {
//String name = null;
AssignSeat seat = new AssignSeat();
seat.SeatStart();
if(event.getSource() instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("2")) {
entername.setVisible(true);
seatnum.setVisible(true);
confirmed.setVisible(true);
inputline.setVisible(true);
outputline.setVisible(true);
inputline.setEditable(true);
inputline.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String name = null;
AssignSeat seat = new AssignSeat();
seat.SeatStart();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
name = inputline.getText();
seat.arrangement[1] = name;
System.out.println(seat.arrangement[1]);
inputline.setEditable(false);
outputline.setText("2");
two.setForeground(Color.black);
for( ActionListener al : two.getActionListeners() ) {
two.removeActionListener( al );
}
}
}
});
} else if(buttonText.equals("5")) {
entername.setVisible(true);
seatnum.setVisible(true);
confirmed.setVisible(true);
inputline.setVisible(true);
outputline.setVisible(true);
inputline.setEditable(true);
inputline.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String name = null;
AssignSeat seat = new AssignSeat();
seat.SeatStart();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
name = inputline.getText();
seat.arrangement[4] = name;
System.out.println(seat.arrangement[4]);
inputline.setEditable(false);
outputline.setText("5");
five.setForeground(Color.black);
for( ActionListener al : five.getActionListeners() ) {
five.removeActionListener( al );
}
}
}
});
} else if (buttonText.equals("6")) {
entername.setVisible(true);
seatnum.setVisible(true);
confirmed.setVisible(true);
inputline.setVisible(true);
outputline.setVisible(true);
inputline.setEditable(true);
inputline.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String name = null;
AssignSeat seat = new AssignSeat();
seat.SeatStart();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
name = inputline.getText();
seat.arrangement[5] = name;
System.out.println(seat.arrangement[5]);
inputline.setEditable(false);
outputline.setText("6");
six.setForeground(Color.black);
for( ActionListener al : six.getActionListeners() ) {
six.removeActionListener( al );
}
}
}
});
} else if (buttonText.equals("11")) {
entername.setVisible(true);
seatnum.setVisible(true);
confirmed.setVisible(true);
inputline.setVisible(true);
outputline.setVisible(true);
inputline.setEditable(true);
inputline.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String name = null;
AssignSeat seat = new AssignSeat();
seat.SeatStart();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
name = inputline.getText();
seat.arrangement[10] = name;
System.out.println(seat.arrangement[10]);
inputline.setEditable(false);
outputline.setText("11");
eleven.setForeground(Color.black);
for( ActionListener al : eleven.getActionListeners() ) {
eleven.removeActionListener( al );
}
}
}
});
}
}
}
}