所有单独的面板都不会显示在根面板内

时间:2012-06-19 14:43:12

标签: java swing jpanel layout-manager flowlayout

我想向jpanel添加多个jpanel。所以我在jscrollpane中添加了一个根面板,然后将所有单独的jpanel添加到这个根面板。我根据需要制作了jscrollpane的滚动策略。我是HORIZONTAL_SCROLLBAR_​​AS_NEEDED,VERTICAL_SCROLLBAR_​​AS_NEEDED。 但问题是所有单独的面板都没有显示在根面板内。

代码:

JScrollPane scPanel=new JScrollPane();

JPanel rootPanel=new JPanel();
rootPanel.setLayout(new FlowLayout());

JPanel indPanel = new JPanel();
rootPanel.add(indPanel);

JPanel indPanel2 = new JPanel();
rootPanel.add(indPanel2);

//.....like this added indPanals to rootPanel.
scPanel.setViewPortView(rootPanel);
//scPanel.setHorizontalScrollPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);

还有一件事是,当我滚动滚动条时,面板将离开jscrollpane区域。 我无法看到所有单独的面板, 请建议我。

编辑:来自双重帖子的代码段:

MosaicFilesStatusBean mosaicFilesStatusBean = new MosaicFilesStatusBean();
DefaultTableModel tableModel = null;
tableModel = mosaicFilesStatusBean.getFilesStatusBetweenDates(startDate, endDate);
if (tableModel != null) {
    rootPanel.removeAll();        
    rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));      
    for (int tempRow = 0; tempRow < tableModel.getRowCount(); tempRow++) {

        int fileIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 0).toString());
        String dateFromTemp = tableModel.getValueAt(tempRow, 3).toString();
        String dateToTemp = tableModel.getValueAt(tempRow, 4).toString();
        int processIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 5).toString());
        int statusIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 6).toString());
        String operatingDateTemp = tableModel.getValueAt(tempRow, 7).toString();                
        MosaicPanel tempPanel =           
           new MosaicPanel(fileIdTemp, dateFromTemp, dateToTemp, processIdTemp, statusIdTemp, operatingDateTemp);             
        rootPanel.add(tempPanel);             
    }
    rootPanel.revalidate();
}

2 个答案:

答案 0 :(得分:3)

不要将FlowLayout用于rootPanel。而是考虑使用BoxLayout

JPanel rootPanel=new JPanel();
// if you want to stack JPanels vertically:
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS)); 

编辑1
这是SSCCE,它基于您发布的最新代码:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Random;

import javax.swing.*;

@SuppressWarnings("serial")
public class PanelsEg extends JPanel {
   private static final int MAX_ROW_COUNT = 100;
   private Random random = new Random();
   private JPanel rootPanel = new JPanel();

   public PanelsEg() {
      rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));
      JScrollPane scrollPane = new JScrollPane(rootPanel);
      scrollPane.setPreferredSize(new Dimension(400, 400)); // sorry kleopatra

      add(scrollPane);

      add(new JButton(new AbstractAction("Foo") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            foo();
         }
      }));
   }

   public void foo() {
          rootPanel.removeAll();        
          // rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS)); // only need to set layout once 
          int rowCount = random.nextInt(MAX_ROW_COUNT);
         for (int tempRow = 0; tempRow < rowCount ; tempRow++) {

              int fileIdTemp = tempRow;
              String data = "Data " + (tempRow + 1);
              MosaicPanel tempPanel =           
                 new MosaicPanel(fileIdTemp, data);             
              rootPanel.add(tempPanel);             
          }
          rootPanel.revalidate();
          rootPanel.repaint(); // don't forget to repaint if removing
   }

   private class MosaicPanel extends JPanel {

      public MosaicPanel(int fileIdTemp, String data) {
         add(new JLabel(data));
      }

   }

   private static void createAndShowGui() {
      PanelsEg mainPanel = new PanelsEg();

      JFrame frame = new JFrame("PanelsEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }  
}

此SSCCE可以正常工作,因为它可以轻松地显示删除JPanel并将其添加到由JScrollPane持有的另一个JPanel。如果您仍然遇到问题,则应修改此SSCCE,以便它显示您的问题。

答案 1 :(得分:3)

主要原因是,为什么您无法看到JPanel FlowLayoutLayoutManager作为rootPanel的{​​{1}}。由于您添加到此JPanel的{​​{1}}内部没有任何内容,因此它的大小分别为rootPanel,宽度和高度分别为0, 0。虽然使用GridLayout这种情况不应该来。看看附带的代码示例:

import java.awt.*;
import javax.swing.*;

public class PanelAddition
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Panel Addition Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 1));        
        JScrollPane scroller = new JScrollPane();

        CustomPanel panel = new CustomPanel(1);
        contentPane.add(panel);
        scroller.setViewportView(contentPane);
        frame.getContentPane().add(scroller, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        for (int i = 2; i < 20; i++)
        {
            CustomPanel pane = new CustomPanel(i);
            contentPane.add(pane);
            contentPane.revalidate();
            contentPane.repaint();
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PanelAddition().createAndDisplayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{

    public CustomPanel(int num)
    {
        JLabel label = new JLabel("" + num);
        add(label);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(200, 50));
    }
}