如何在我使用JTabbedPane和面板布局的swing应用程序中对齐JLabel是FlowLayout

时间:2014-09-17 05:09:40

标签: java swing gridbaglayout jtabbedpane flowlayout

public class RoteLearnerTabbedPanePage extends JFrame{
        private static final long serialVersionUID = 1L;
        private JTabbedPane tabbedPane;
        private JPanel panel1;
        private JPanel transientPanel;
        private JPanel nonTransientPanel;
        ReadXml rx=new ReadXml();
        Set<String> primaryKeys = rx.getPrimaryKeysOfGuiAttributes();
        public RoteLearnerTabbedPanePage() throws IOException, SAXException, ErrorCondition
        {
            tabbedPane=new JTabbedPane();
            FlowLayout fl=new FlowLayout();
            Document doc=rx.readXmlFile();
            Hashtable<String,String> mainclasnames=rx.getMainClassNames(doc, doc.getChildNodes());
            Set<XmlClass> guiAttrVals=rx.getGuiAttributeValues(doc, doc.getChildNodes());
            int j=-1;
            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>();
                JPanel buttons=new JPanel();
                JButton buttonLearn = new JButton("Learn");
                panel1= new JPanel();
                transientPanel=new JPanel(new GridBagLayout());
                nonTransientPanel=new JPanel(new GridBagLayout());
                GridBagConstraints constraints = 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"))
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 15, 150, 20 );   
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                nonTransientPanel.add(name,constraints);
                                nonTransientPanel.add(value);
                                uiParams.put(name, value);
                                nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
                                nonTransientPanel.setLayout(fl);
                                panel1.add(nonTransientPanel);
                            }
                            else
                            {
                                constraints.anchor = GridBagConstraints.NORTH;
                                constraints.insets = new Insets(10, 10, 10, 10);
                                JLabel name = new JLabel(attrname);
                                //name.setBounds( 10, 15, 150, 20 );
                                JTextField value = new JTextField(20);
                                //value.setBounds( 10, 35, 150, 20 );
                                constraints.gridx = 0;
                                constraints.gridy = ++j;
                                if(primaryKeys.contains(name.getText()))
                                {
                                    name.setForeground(Color.RED);
                                }
                                else
                                {
                                    name.setForeground(Color.BLUE);
                                }
                                transientPanel.add(name,constraints);
                                transientPanel.add(value);
                                uiParams.put(name, value);
                                      transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
                                transientPanel.setLayout(fl);
                                panel1.add(transientPanel); 
                            }
                        }
                        constraints.gridx = 0;
                        constraints.gridy = ++j;
                        constraints.gridwidth = 2;
                        constraints.anchor = GridBagConstraints.SOUTH;

                        panel1.setSize(400, 400);
                        panel1.setLayout(fl);
                        tabbedPane.addTab(classname, panel1);
                    }
                }

                buttons.add(buttonLearn, constraints);
                panel1.add(buttons);
             }

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

            topPanel.add( tabbedPane, BorderLayout.CENTER );
        }

这就是我使用JTabbedPane显示两种形式的方式。

  1. 如何缩进JLabel以使其看起来像标准表单?
  2. 有人能回答这个问题吗?迫切需要..提前谢谢.. 我使用了JTabbedPane和一个外部Jpanel,里面有两个JPanels TransietnPanel和nonGTransientPanel。此外部面板已添加到JTabedPane。

    This is the output I am getting

    这是我得到的输出

    This is what I want to make it look like. 这就是我想要的样子。 --------------添加GroupLayout后的代码--------------------

    while(mainclass_it.hasNext())
                {
                    Entry<String, String> classDet = mainclass_it.next();
                    String classname = classDet.getValue();
                    final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
                    JPanel buttons=new JPanel();
                    JButton buttonLearn = new JButton("Learn");
    
                    panel1= new JPanel();
                    //GroupLayout layout = new GroupLayout(panel1);
    
                    //panel1.setLayout(layout);
                    transientPanel=new JPanel(new GridBagLayout());
                    nonTransientPanel=new JPanel(new GridBagLayout());
                    GridBagConstraints constraints = new GridBagConstraints();
                    Iterator<XmlClass> guiItr=guiAttrVals.iterator();
                    while(guiItr.hasNext())
                    {       
                        XmlClass xmlclasss=guiItr.next();
                        if(classname.equals(xmlclasss.getName()))
                        {
                            GroupLayout layout1 = new GroupLayout(nonTransientPanel);
                            layout1 = new GroupLayout(transientPanel);
                            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"))
                                {
                                    constraints.anchor = GridBagConstraints.NORTH;
                                    constraints.insets = new Insets(10, 10, 10, 10);
                                    JLabel name = new JLabel(attrname);
                                    //name.setBounds( 10, 15, 150, 20 );
                                    JTextField value = new JTextField(20);
                                    //value.setBounds( 10, 15, 150, 20 );   
                                    constraints.gridx = 0;
                                    constraints.gridy = ++j;
                                    if(primaryKeys.contains(name.getText()))
                                    {
                                        name.setForeground(Color.RED);
                                    }
                                    else
                                    {
                                        name.setForeground(Color.BLUE);
                                    }
    
                                    nonTransientPanel.setLayout(layout1);
                                    nonTransientPanel.add(name,constraints);
                                    nonTransientPanel.add(value);
                                    uiParams.put(name, value);
                                    nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
                                    nonTransientPanel.setLayout(fl);
                                    panel1.add(nonTransientPanel);
                                }
                                else
                                {
                                    constraints.anchor = GridBagConstraints.NORTH;
                                    constraints.insets = new Insets(10, 10, 10, 10);
                                    JLabel name = new JLabel(attrname);
                                    //name.setBounds( 10, 15, 150, 20 );
                                    JTextField value = new JTextField(20);
                                    //value.setBounds( 10, 35, 150, 20 );
                                    constraints.gridx = 0;
                                    constraints.gridy = ++j;
                                    if(primaryKeys.contains(name.getText()))
                                    {
                                        name.setForeground(Color.RED);
                                    }
                                    else
                                    {
                                        name.setForeground(Color.BLUE);
                                    }
    
                                    transientPanel.setLayout(layout1);
                                    transientPanel.add(name,constraints);
                                    transientPanel.add(value);
                                    uiParams.put(name, value);
                                    transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
                                    transientPanel.setLayout(fl);
                                    panel1.add(transientPanel); 
                                }
                            }
                            constraints.gridx = 0;
                            constraints.gridy = ++j;
                            constraints.gridwidth = 2;
                            constraints.anchor = GridBagConstraints.SOUTH;
    
                            panel1.setSize(400, 400);
                            panel1.setLayout(fl);
                            tabbedPane.addTab(classname, panel1);
                        }
                    }
    
                    buttons.add(buttonLearn, constraints);
                    panel1.add(buttons);
    

0 个答案:

没有答案