TabbedPane迭代for循环

时间:2015-05-17 12:51:58

标签: java swing jpanel jscrollpane jtabbedpane

我在循环迭代中收到:

Tab 1
Tab 2

在开始代码我有面板,tabbedpane和scrollpane:

JPanel panel = new JPanel();
JTabbedPane tabbedPane = new JTabbedPane();
JScrollPane scrollPanel = new JScrollPane();

然后使用Event JList通过迭代接收并接收Tab 1Tab 2,但无法添加两个标签。所以这里是迭代并将Tab 1Tab 2接收到tab_name变量中:

String tab_name = ap_data_array[2]; // Tab 1, Tab 2

tabbedPane.addTab(tab_name, panel); // Here add only once tab Tab 1
this.jPanel1.add(tabbedPane, BorderLayout.CENTER);

scrollPanel.setViewportView(new JLabel(ap_data_array[2]));
tabbedPane.setBounds(0, 0, 530, 500);

this.jPanel1.setSize(530,500);

System.out.println(tab_name); // Tab 1 then Tab 2

结果只会呈现1个标签Tab 1

以下是完整代码:

String lines[] = loaded_data.split("\\r?\\n");
for (String item : lines)
{
String pattern = "AP.*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(item);

// Added
JPanel panel = new JPanel();
JTabbedPane tabbedPane = new JTabbedPane();
JScrollPane scrollPanel = new JScrollPane();

if (m.find())
{
    String ap_data = m.group(0);
    String[] ap_data_array = ap_data.split("\\|", -1);



    if (ap_data_array[0].equals("AP"))
    {
        JList source = (JList)evt.getSource();
        String selected = source.getSelectedValue().toString();

        // type selected
        if (ap_data_array[1].equals(selected))
        {
            // tabs name
            //ap_data_array[2]  // "Tab 1" "Tab 2"

            String tab_name = ap_data_array[2];

            tabbedPane.addTab(tab_name, panel);
            this.jPanel1.add(tabbedPane, BorderLayout.CENTER);

            scrollPanel.setViewportView(new JLabel(ap_data_array[2]));
            tabbedPane.setBounds(0, 0, 530, 500);



            this.jPanel1.setSize(530,500);

            System.out.println(tab_name); // "Tab 1" "Tab 2"
     }
     }
}
}

SOLUTION:

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    

    if (evt.getValueIsAdjusting())
    {
        JTabbedPane tabbedPane = new JTabbedPane();

        this.jPanel1.removeAll(); // REMOVE ALL DATA IN PANEL TO RELOAD NEW TAB PANEL IN LOOP

        String lines[] = loaded_data.split("\\r?\\n");
        for (String item : lines)
        {
            String pattern = "AP.*";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(item);

            JPanel panel = new JPanel();

            if (m.find())
            {
                String ap_data = m.group(0);
                String[] ap_data_array = ap_data.split("\\|", -1);



                if (ap_data_array[0].equals("AP"))
                {
                    JList source = (JList)evt.getSource();
                    String selected = source.getSelectedValue().toString();

                    // type selected
                    if (ap_data_array[1].equals(selected))
                    {
                        String tab_name = ap_data_array[2]; // STRING "Tab 1" and "Tab 2" loop


                        this.jPanel1.add(tabbedPane, BorderLayout.CENTER);
                        tabbedPane.add(tab_name, new JLabel(tab_name));

                        tabbedPane.setBounds(0, 0, 530, 500);

                        this.jPanel1.setSize(530,500);
                    }
                }
            }
        }
    }

    }            

1 个答案:

答案 0 :(得分:2)

如果我正确理解你的代码(不保证,因为它不是我可以运行和测试的minimal example program),在for循环内你创建了一个新的JTabbedPane 迭代,向其添加单个选项卡,然后将其添加到BorderLayout中 - 使用BorderLayout.CENTER位置中的JPanel。因此,如果迭代两次,您将创建两个JTabbedPanes,每个只有一个选项卡,第二个将完全覆盖第一个。如果这确实是您问题的根源,那么解决方案很简单:只创建一个 JTabbedPane,在之前执行for循环,并在其中添加标签(JPanels) for循环。

其他问题包括您使用setBounds(...)。不要这样做。