如何从具有BoxLayout的容器内的组件中删除多余的空白?

时间:2014-09-18 06:12:57

标签: java swing jpanel jlabel boxlayout

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());

            // 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);
                            }
                            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.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);
                    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);
                }
            }



        }
        setTitle( "Rote Learner Form" );
        setSize(500, 500);
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add( topPanel );
        // Create the tab pages
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        topPanel.add( tabbedPane, BorderLayout.CENTER );
    }

panel1是包含三个内部jpanel的容器,即transientpanel,nonTransientPanel和按钮。
当我运行代码时,它看起来如何。 When I run the code this how it looks 我也希望将所有标签对齐到左侧,现在它位于中心位置。我该怎么做?

0 个答案:

没有答案
相关问题