3

我想在 jpanel 中添加多个 jpanel。所以我在 jpanel 中添加了一个根面板。然后将所有单独的 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();
}
4

2 回答 2

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 以便它显示您的问题。

于 2012-06-19T14:44:57.897 回答
3

你看不到你的主要原因JPanel是你使用FlowLayoutLayoutManagerrootPanel. 并且由于您JPanel添加到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));
    }
}
于 2012-06-19T15:12:57.267 回答