如何在JTabbedPane中使用JScrollPane?

时间:2014-09-18 10:22:02

标签: java swing jscrollpane jtabbedpane

public RoteLearnerTabbedPanePage() throws IOException, SAXException, ErrorCondition
        {
            JFrame frm=new JFrame();


            tabbedPane=new JTabbedPane();
            Document doc=rx.readXmlFile();
            if(!rx.validateClassIds())
            {
                JOptionPane.showMessageDialog(panel1,"Check Xml file. Duplicate 'Class Ids' are present","Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0); 
            }
            if(!rx.validateAttributeIds())
            {
                JOptionPane.showMessageDialog(panel1,"Check Xml file. Duplicate 'Attribute Ids' are present","Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);     
            }
            if(!rx.isValidMainClass())
            {
                JOptionPane.showMessageDialog(panel1,"Check Xml File. Main-class declaration for one of the domain classes seems invalid","Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
            Hashtable<String,String> mainclasnames=rx.getMainClassNames(doc, doc.getChildNodes());

            Set<XmlClass> guiAttrVals=rx.getGuiAttributeValues(doc, doc.getChildNodes());
            int j=0;
            int i=0;
            Iterator<Entry<String, String>> mainclass_it = mainclasnames.entrySet().iterator();
            while(mainclass_it.hasNext())
            {

                Entry<String, String> classDet = mainclass_it.next();
                String classname = classDet.getValue();
                final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
                JButton buttonLearn = new JButton("Learn");
                panel1= new JPanel();
                panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
                transientPanel=new JPanel(new GridBagLayout());

                // Create a sequential group for the vertical axis.
                nonTransientPanel=new JPanel(new GridBagLayout());
                JScrollPane scroll1 = new JScrollPane(panel1);
                tabbedPane.add(scroll1);
                // Create a sequential group for the horizontal axis.

                GridBagConstraints constraints = new GridBagConstraints();
                GridBagConstraints nontranconstraints = new GridBagConstraints();
                Iterator<XmlClass> guiItr=guiAttrVals.iterator();
                while(guiItr.hasNext())
                {       
                    XmlClass xmlclasss=guiItr.next();
                    if(classname.equals(xmlclasss.getName()))
                    {
                        Set<XmlAttributes> xmlAttrs=xmlclasss.getAttributes();
                        Iterator<XmlAttributes> xmlattrItr=xmlAttrs.iterator();
                        while(xmlattrItr.hasNext())
                        {

                            XmlAttributes xa=xmlattrItr.next(); 
                            String attrname=xa.getName();
                            String isTransient=xa.isTransientAttr();
                            if(isTransient.equals("false"))
                            {
                                nontranconstraints.insets = new Insets(10, 10, 10, 10);
                                nontranconstraints.gridx = 0;
                                nontranconstraints.gridy = i;
                                JLabel name = new JLabel(attrname);
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                panel1.add((nonTransientPanel), BorderLayout.CENTER);
                                nonTransientPanel.setPreferredSize(new Dimension(200,100));
                                nonTransientPanel.add(name,nontranconstraints);
                                nontranconstraints.gridx = 1;
                                JTextField value = new JTextField(20);
                                nonTransientPanel.add(value,nontranconstraints);
                                i++;
                                uiParams.put(name, value);
                            }
                            else
                            {
                                constraints.insets = new Insets(10, 10, 10, 10);
                                constraints.gridx = 0;
                                constraints.gridy = j;
                                JLabel name = new JLabel(attrname);
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                transientPanel.setPreferredSize(new Dimension(200,100));
                                transientPanel.add(name,constraints);
                                constraints.gridx = 1;
                                JTextField value = new JTextField(20);
                                transientPanel.add(value,constraints);
                                j++;
                                uiParams.put(name, value);
                            }
                        }

                        nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));

                        panel1.add(nonTransientPanel);
                        panel1.setPreferredSize(new Dimension(300,300));
                        transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
                        panel1.add(transientPanel);         
                        constraints.gridx = 0;
                        constraints.gridy = ++j;
                        //constraints.gridwidth = 2;
                        constraints.anchor = GridBagConstraints.SOUTH;
                        //buttons.add(buttonLearn,constraints);
                        buttonLearn.setAlignmentX(Component.CENTER_ALIGNMENT);
                        panel1.add(buttonLearn);
                        //panel1.setLayout(fl);
                        tabbedPane.addTab(classname, panel1);
                    }
                }

这是我的代码。我真的很陌生。我不确切知道如何将JScrollPane与JTabbedPane一起使用。我正在使用JtabbedPane,其中有一个外部JPanel,其中包含3个内部jPanels即。 transientPanel,非瞬态和按钮面板。

1 个答案:

答案 0 :(得分:1)

尝试将滚动条添加到标签:

                    // panel1.setLayout(fl);
                    tabbedPane.addTab(classname, scroll1);

而不是你的panel1:

                // panel1.setLayout(fl);
                tabbedPane.addTab(classname, panel1);