3

複数の jpanel を jpanel に追加したいので、ルート パネルを jscrollpane. に追加し、すべての個々の jpanel をこのルート パネルに追加しました。 jscrollpane のスクロール ポリシーを必要に応じて作成しました。しかし問題は、すべての個々のパネルがルート パネル内に表示されないことです。

コード:

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);

もう1つ、スクロールバーをスクロールすると、パネルが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

あなたがあなたを見ることができなかった主な理由は、あなたがのためにJPanel使用FlowLayoutしているということです。また、これに追加したものには内部に何も含まれていないため、幅と高さはそれぞれ、のサイズになります。そのような状況を使用することは来るべきではありませんが。添付のこのコード例をご覧ください:LayoutManagerrootPanelJPanelrootPanel0, 0GridLayout

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 に答える
3

rootPanel に FlowLayout を使用しないでください。代わりに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 は、JScrollPane によって保持されている別の JPanel への JPanel の削除と追加を簡単に示すという点で機能します。それでも問題が解決しない場合は、この SSCCE を変更して、問題が表示されるようにする必要があります。

于 2012-06-19T14:44:57.897 に答える