简体   繁体   English

TextField和JScrollPane的JList不显示/ Java Swing

[英]JList of TextFields and JScrollPane doesn't show / Java Swing

I am trying to create a window that shows a dynamic list of textFields and if the number of textfields is large then I want to add a scroller. 我正在尝试创建一个显示textFields动态列表的窗口,如果textfields的数量很大,那么我想添加一个滚动条。 I am using GridLayout. 我正在使用GridLayout。 The problem is that the panel I added the Jlist and scroller doesn't show anything, neither the list nor the scroller. 问题是我添加了Jlist和滚动条的面板没有显示任何内容,列表和滚动条也没有。 Below you will find a part of my code. 在下面,您将找到我的代码的一部分。

                   //Label 
                JLabel numberOfTxt = new JLabel("Please enter the number in every TextField");
                int n = 11; //A random number of TextFields
                firstPanel.add(numberOfTxt, BorderLayout.NORTH); //Add label to panel

                JList textFieldList = new JList(); //Create a list of TextFields
                for (int i = 0; i < n; i++) {
                    //Add TextFields to list
                    JTextField textField = new JTextField();
                    textField.setBounds(0, 0, 6, 0);

                    textFieldList.add(textField);
                    System.out.println("textFieldList" + textFieldList);
                }


                textFieldList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                textFieldList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                textFieldList.setVisibleRowCount(8);

                //Create scroller
                JScrollPane listScroller = new JScrollPane(textFieldList);
                listScroller.setBounds(0, 20, 600, 600);

                //Create layout for panel where the textfields will be added
                if (n % 2 != 0) {
                    n = n + 1;
                }
                thirdPanel.setLayout(new GridLayout(n / 2, 2, 10, 6));
                thirdPanel.add(textFieldList);
                thirdPanel.setVisible(true);

                //ContentPane has BoxLayout
                contentPane.add(firstPanel);
                contentPane.add(thirdPanel);

                contentPane.repaint();
                window.pack();
            }
            window.revalidate();
        }
    });
  1. JList does not works this way. JList不能以这种方式工作。 If you really need a JList of TextFields you should use ListCellRenderer (probably you don't, see p.3). 如果您确实需要TextFieldsJList ,则应该使用ListCellRenderer (可能不需要,请参阅第3页)。

  2. You adding textFieldList both to listScroller and thirdPanel . 您将textFieldList都添加到listScrollerthirdPanel Probably, you should replace thirdPanel.add(textFieldList); 可能您应该替换thirdPanel.add(textFieldList); by thirdPanel.add(listScroller); 通过thirdPanel.add(listScroller); .

  3. thirdPanel uses GridLayout , but only one control is ever added to it. thirdPanel使用GridLayout ,但仅向其中添加了一个控件。 You should either add TextField directly to thirdPanel (easier way), or let the JList manage them. 您应该将TextField直接添加到thirdPanel (更简便的方法),或者让JList管理它们。

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

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