有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java根面板中不显示所有单独的面板

我想向jpanel添加多个jpanel。所以我在jscrollpane中添加了一个根面板。然后将所有单独的JPanel添加到此根面板。我根据需要制定了jscrollpane的滚动策略。i、 水平滚动条根据需要,垂直滚动条根据需要。 但问题是所有单独的面板都没有显示在根面板中

代码:

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区域消失。 我无法看到所有单独的面板, 请推荐我

编辑:来自double post的代码段:

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) 个答案

  1. # 1 楼答案

    你看不到你的JPanel的主要原因是你使用FlowLayout作为rootPanelLayoutManager。因为你的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));
        }
    }
    
  2. # 2 楼答案

    不要对根面板使用FlowLayout。取而代之的是考虑使用{a1}:

    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,使其显示您的问题