在JTabbedPane中拆分标签

时间:2015-02-03 11:26:48

标签: java swing split layout-manager jtabbedpane

我需要在JTabbedPane上更改我的标签分发。

我有这个:

enter image description here 我想这样做: enter image description here 有人能帮助我吗?

我发布以下代码:

 tabbedResultsPane = new JTabbedPane(SwingConstants.TOP);

     JPanel featurePanel = new JPanel(new GridLayout(TOTAL_FEATURES, 2, 3, 3));  
     estadoScroll = new JScrollPane(featurePanel,
                      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        lblFeatureHdr = new JLabel[TOTAL_FEATURES];
        lblFeature = new JLabel[TOTAL_FEATURES];
        for(int i=0; i<TOTAL_FEATURES; i++)
        {
           lblFeatureHdr[i] = new JLabel(strHeader[i], JLabel.RIGHT);
           lblFeatureHdr[i].setOpaque(true);  
           lblFeatureHdr[i].setBackground(new Color(220,255,220));//.lightGray);

           lblFeature[i] = new JLabel("", JLabel.LEFT);
           lblFeature[i].setForeground(Color.blue);// black); 

           featurePanel.add(lblFeatureHdr[i]);
           featurePanel.add(lblFeature[i]);                
        }            

1 个答案:

答案 0 :(得分:2)

定义4列GridLayout(而不是2列)。

并更正您的代码,每行再添加2个标签。

for(int i=0; i<TOTAL_FEATURES; i++)
    {
       lblFeatureHdr[i] = new JLabel(strHeader[i], JLabel.RIGHT);
       lblFeatureHdr[i].setOpaque(true);  
       lblFeatureHdr[i].setBackground(new Color(220,255,220));//.lightGray);

       lblFeature[i] = new JLabel("", JLabel.LEFT);
       lblFeature[i].setForeground(Color.blue);// black); 

       featurePanel.add(lblFeatureHdr[i]);
       featurePanel.add(lblFeature[i]);  
       // add 2 more lables to the same row    
       JLabel l=new JLabel(strHeader[i], JLabel.RIGHT);
       l.setBackground(new Color(220,255,220));//.lightGray);          
       featurePanel.add(l);
       featurePanel.add(new JLabel("", JLabel.LEFT));                
    }