我正在尝试将鲜花添加到flowerpack
并且没有添加它们。第一部分是一个GUI,但一旦"输入新花"检查它切换到控制台。所有控制台打印行都会出现,我输入信息,但是我在flowerpack.add
处放置了一个断点,但它永远不会到达,所以我的flowerpack
始终为空。我在这里做错了什么?
private static void addFlower(ArrayList<Plant> flowerPack) {
Scanner in = new Scanner(System.in);
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel flowerLabel = new JLabel("Enter new flower: ");
frame.getContentPane().add(flowerLabel);
JCheckBox checkFlower = new JCheckBox();
frame.getContentPane().add(checkFlower);
checkFlower.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("Please enter flower ID: ");
int plantID = in.nextInt();
in.nextLine();
System.out.print("Please enter flower Name: ");
String flowerName = in.nextLine();
System.out.print("Please enter flower color: ");
String flowerColor = in.nextLine();
System.out.print("Does flower have thorns? Yes or no: ");
String flowerThorns = in.nextLine();
System.out.print("Does flower smell? Yes or no: ");
String flowerSmell = in.nextLine();
flowerPack.add(new Flower(plantID, flowerName,
flowerColor, flowerThorns, flowerSmell));
}
});