JScrollPane中的JPanel被切断了

时间:2014-04-26 02:45:20

标签: java swing jscrollpane

所以我在JScrollPane中有一个JPanel,它包含一个填充了其他面板的GridLayout(这样我就可以将它们的大小设置为绝对值)。每个面板和滚动窗格的大小都是使用SetPreferredSize()定义的,并且滚动有效。但是,初始视图下方的所有内容都被截断,使向下滚动无用。这是我的意思的图片: enter image description here

下面应该有其他字段。我如何确保它们出现?

编辑:代码段:

public Volume_Stacks_Builder2 () {
    super("Volume Stacks Builder");
    instance = this;
    addKeyListener(IJ.getInstance());
    setLayout(new FlowLayout());
    panel = new Panel();
    wList = WindowManager.getIDList();
    if (wList==null) {
        IJ.noImage();
        //return false;
    }
    panel.setLayout(new GridLayout(wList.length * 3 + 3, 1));
    framesC = new int[wList.length];
    framesD = new int[wList.length];
    frameBoxes = new JComboBox[wList.length];
    diastoleSlices = new JFormattedTextField[wList.length];
    cystoleSlices = new JFormattedTextField[wList.length];
    String[] titles = new String[wList.length];
    for (int i=0; i<wList.length; i++) {
        ImagePlus imp = WindowManager.getImage(wList[i]);
        titles[i] = imp!=null?imp.getTitle():"";
    }

    int firstStackSize = WindowManager.getImage(wList[0]).getStack().getSize();
    for (int i = 0; i < wList.length; i++) {
        JPanel fPanel = new JPanel();
        fPanel.setMinimumSize(new Dimension(450, 25));
        fPanel.setPreferredSize(new Dimension(450, 25));
        fPanel.setMaximumSize(new Dimension(450, 25));
        JLabel flabel = new JLabel("Frame " + (i + 1) +  " image:");
        flabel.setPreferredSize(new Dimension(200, 25));
        fPanel.add(flabel);
        frameBoxes[i] = new JComboBox(titles);
        frameBoxes[i].setSelectedIndex(i);
        frameBoxes[i].setPreferredSize(new Dimension(200, 25));
        fPanel.add(frameBoxes[i]);
        panel.add(fPanel);

        JPanel dPanel = new JPanel();
        dPanel.setMinimumSize(new Dimension(450, 25));
        dPanel.setPreferredSize(new Dimension(450, 25));
        dPanel.setMaximumSize(new Dimension(450, 25));
        JLabel dLabel = new JLabel("End disastole slice: ");
        dLabel.setPreferredSize(new Dimension(200, 25));
        dPanel.add(dLabel);
        diastoleSlices[i] = new JFormattedTextField(sliceFormat);
        diastoleSlices[i].setValue(new Integer(1));
        diastoleSlices[i].setPreferredSize(new Dimension(200, 25));
        dPanel.add(diastoleSlices[i]);
        panel.add(dPanel);

        JPanel sPanel = new JPanel();
        sPanel.setMinimumSize(new Dimension(450, 25));
        sPanel.setPreferredSize(new Dimension(450, 25));
        sPanel.setMaximumSize(new Dimension(450, 25));
        JLabel sLabel = new JLabel("End systole slice: ");
        sLabel.setPreferredSize(new Dimension(200, 25));
        sPanel.add(sLabel);
        cystoleSlices[i] = new JFormattedTextField(sliceFormat);
        cystoleSlices[i].setValue(new Integer(firstStackSize / 2));
        cystoleSlices[i].setPreferredSize(new Dimension(200, 25));
        sPanel.add(cystoleSlices[i]);
        panel.add(sPanel);
    }
    JPanel dtPanel = new JPanel();
    dtPanel.setMinimumSize(new Dimension(450, 25));
    dtPanel.setPreferredSize(new Dimension(450, 25));
    dtPanel.setMaximumSize(new Dimension(450, 25));
    JLabel dLabel = new JLabel("Diastole stack title:");
    dLabel.setPreferredSize(new Dimension(200, 25));
    dtPanel.add(dLabel);
    dStackTitle = new JTextArea(titleD);
    dStackTitle.setPreferredSize(new Dimension(200, 25));
    dtPanel.add(dStackTitle);
    panel.add(dtPanel);

    JPanel stPanel = new JPanel();
    stPanel.setMinimumSize(new Dimension(450, 25));
    stPanel.setPreferredSize(new Dimension(450, 25));
    stPanel.setMaximumSize(new Dimension(450, 25));
    JLabel sLabel = new JLabel("Systole stack title:");
    sLabel.setPreferredSize(new Dimension(200, 25));
    stPanel.add(sLabel);
    sStackTitle = new JTextArea(titleC);
    sStackTitle.setPreferredSize(new Dimension(200, 25));
    stPanel.add(sStackTitle);
    panel.add(stPanel);

    JPanel subPanel = new JPanel();
    subPanel.setMinimumSize(new Dimension(450, 25));
    subPanel.setPreferredSize(new Dimension(450, 25));
    subPanel.setMaximumSize(new Dimension(450, 25));
    JButton submit = new JButton("Submit");
    submit.addActionListener(this);
    submit.setPreferredSize(new Dimension(450, 25));
    subPanel.add(submit);
    panel.add(subPanel);

    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setPreferredSize(new Dimension(450, 400));
    scrollPane.setMaximumSize(new Dimension(450, 400));
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    this.add(scrollPane);
    pack();
    GUI.center(this);
    setVisible(true);
}

1 个答案:

答案 0 :(得分:0)

您的问题可能是因为您正在设置尺寸或首选尺寸,尤其是JScrollPane视口中保存的JPanel。不要设置它的大小。此外,您还需要避免设置子组件的绝对大小。

为了获得更好的帮助,请显示代码,最好是minimal example program


修改

  • 此外,还有其他更好的方法来指示所需的大小,包括使用JTextField的列属性。
  • 另外,我想知道您是否真的想使用JTable来显示表格数据。看起来好像很自然。