如何在JTextField输入中存储新的Object

时间:2017-11-25 22:45:17

标签: java swing jtextfield

我正在为用户创建一个GUI,用于从包含Entrees,Sides,Salads和Desserts的四个组合框中选择创建一个Menu。 "创建菜单"按钮提示用户输入菜单名称并将新的Menu对象存储在输入中。如何存储"菜单"类型的对象?包含Entrees,Sides,Salads和Desserts的用户输入,然后存储在JTextArea(我的menuList变量)中?我还想将新菜单名称存储到新菜单对象中。任何帮助都会很棒,谢谢!

        btnMenuInput = new JButton("Create New Menu");
        btnMenuInput.setBounds(120, 100, 200, 30);
        btnMenuInput.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){ 
                Entree selectedEntree = (Entree)cboEntrees.getSelectedItem();
                Side selectedSide = (Side)cboSides.getSelectedItem();
                Salad selectedSalad = (Salad)cboSalads.getSelectedItem();
                Dessert selectedDessert = (Dessert)cboDesserts.getSelectedItem();

                Menu menu = new Menu("", selectedEntree, selectedSide, selectedSalad, selectedDessert);
                if(menuList.getText().length() == 0) {
                    menuList.setText(input.getText().toString());
                }
                else menuList.setText(menuList.getText() + "\n" + input.getText());
                childFrame.setVisible(false);
            }
        });
        childFrame.getContentPane().add(btnMenuInput);  
    }
});

0 个答案:

没有答案