簡體   English   中英

TextField和JScrollPane的JList不顯示/ Java Swing

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

我正在嘗試創建一個顯示textFields動態列表的窗口,如果textfields的數量很大,那么我想添加一個滾動條。 我正在使用GridLayout。 問題是我添加了Jlist和滾動條的面板沒有顯示任何內容,列表和滾動條也沒有。 在下面,您將找到我的代碼的一部分。

                   //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不能以這種方式工作。 如果您確實需要TextFieldsJList ,則應該使用ListCellRenderer (可能不需要,請參閱第3頁)。

  2. 您將textFieldList都添加到listScrollerthirdPanel 可能您應該替換thirdPanel.add(textFieldList); 通過thirdPanel.add(listScroller);

  3. thirdPanel使用GridLayout ,但僅向其中添加了一個控件。 您應該將TextField直接添加到thirdPanel (更簡便的方法),或者讓JList管理它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM