简体   繁体   English

如何在JList中存储和显示用户输入?

[英]How to store and display user input in JList?

I don't know what I am doing wrong. 我不知道我在做什么错。 I am trying to take a JTextField user input to be stored and displayed in a JList, but every time the button is pressed to store the user input the JList remains blank. 我试图将JTextField用户输入存储并显示在JList中,但是每次按下按钮以存储用户输入时,JList都为空白。 Any help would be greatly appreciated. 任何帮助将不胜感激。

DefaultListModel<String> model = new DefaultListModel<String>();
        menuList = new JList<String>(model);
        menuList.setBounds(500, 65, 300, 400);
        menuList.setSelectionBackground(Color.LIGHT_GRAY);
        menuList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        btnCreateMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                childFrame = new JFrame("New Menu");
                childFrame.setBounds(340, 300, 400, 200);
                childFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                childFrame.getContentPane().setLayout(null);
                childFrame.setVisible(true);

                lblNewMenu = new JLabel("Menu Name:");
                lblNewMenu.setBounds(30, 60, 200, 20);
                childFrame.getContentPane().add(lblNewMenu);

                input = new JTextField();
                String userInput = input.getText();
                input.setBounds(lblNewMenu.getX() + 80, lblNewMenu.getY(), 250, 30);
                childFrame.getContentPane().add(input);


                btnMenuInput = new JButton("Create New Menu");
                btnMenuInput.setBounds(120, 100, 200, 30);
                btnMenuInput.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){ 

                        model.addElement(userInput);
                        menuList.setModel(model);
                        childFrame.setVisible(false);

                        Entree selectedEntree = (Entree)cboEntrees.getSelectedItem();
                        Side selectedSide = (Side)cboSides.getSelectedItem();
                        Salad selectedSalad = (Salad)cboSalads.getSelectedItem();
                        Dessert selectedDessert = (Dessert)cboDesserts.getSelectedItem();


                        Menu menu = new Menu(userInput, selectedEntree, selectedSide, selectedSalad, selectedDessert);
                        menuArray.add(menu);
                    }
                });
                childFrame.getContentPane().add(btnMenuInput);  
            }
        });

        mainframe.setVisible(true);

This line 这条线

userInput = input.getText();

needs to be called first in the ActionListener. 需要在的ActionListener调用。 Otherwise you never get the latest String from the text field. 否则,您永远不会从文本字段中获取最新的字符串。

eg, 例如,

public void actionPerformed(ActionEvent e){ 
    userInput = input.getText();
    model.addElement(userInput);
    //menuList.setModel(model); // not needed

Also, as mentioned by camickr in comment, avoid using null layouts and setBounds as this fights against the Swing GUI library rather than working with it, making it much harder to create flexible easy to update and edit GUI's. 另外,正如camickr在评论中提到的那样,避免使用null布局和setBounds,因为这会与Swing GUI库进行斗争,而不是与之打交道,这使得创建灵活,易于更新和编辑GUI的工作变得更加困难。

Also, the childFrame top-level window should be a JDialog and not a second JFrame. 此外,childFrame顶级窗口应该是JDialog,而不是第二个JFrame。 Please see The Use of Multiple JFrames: Good or Bad Practice? 请参阅使用多个JFrame:良好或不良做法? for more on this. 有关此的更多信息。

Also note that you should create the entire dialog or Frame before you make it visible, or else some of the items may not be visible at first. 还要注意,在显示对话框或框架之前,应先创建整个对话框,否则某些项目一开始可能不可见。

Another problem is the use of JFrame.HIDE_ON_CLOSE. 另一个问题是使用JFrame.HIDE_ON_CLOSE。 You should probably be using DISPOSE_ON_CLOSE instead. 您可能应该使用DISPOSE_ON_CLOSE。 Otherwise the frame will just be hidden, but will still exist, possibly for the life of the program. 否则,框架将被隐藏,但仍然存在,可能会在程序的整个生命周期内消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM