如何实现JScrollPane子行为?

时间:2014-06-13 10:41:40

标签: java swing jpanel jscrollpane preferredsize

said in manual,如果孩子没有实施Scrollable,则JScrollPane依赖于preferredSize内容的属性。

显然这对我来说不是这样。我正在增加首选高度,但JScrollPane对此没有感觉或反应。

package tests;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Try01_JScrollPane extends JFrame {

    private static final long serialVersionUID = 4123186105171813186L;

    private static final Logger log = LoggerFactory.getLogger(Try01_JScrollPane.class);

    JPanel yellowPanel = new JPanel();
    {
        yellowPanel.setPreferredSize(new Dimension(200,50));
        yellowPanel.setSize(new Dimension(200,50));
        yellowPanel.setBackground(Color.yellow);
    }

    JScrollPane scrollPane = new JScrollPane(yellowPanel);
    {
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    }

    AbstractAction increaseAction = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            yellowPanel.setPreferredSize(new Dimension(yellowPanel.getPreferredSize().width, yellowPanel.getPreferredSize().height+100));
            log.debug("preferred height is now {}", yellowPanel.getPreferredSize().height);
        }
    };

    Timer increaseTimer = new Timer(1000, increaseAction);

    {
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 400);

        setTitle("Try01_JScrollPane");

        increaseTimer.start();
        setVisible(true);
    }

    public static void main(String[] args) {

        new Try01_JScrollPane();

    }


}

2 个答案:

答案 0 :(得分:2)

JPanel也是容器和JComponent,对于JViewport的任何更改,你必须通知JScrollPane: - )

enter image description here

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Try01_JScrollPane extends JFrame {

    private static final long serialVersionUID = 4123186105171813186L;
    private JFrame frame = new JFrame("Try01_JScrollPane");
    private JPanel yellowPanel = new JPanel();

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

    {
        yellowPanel.setBackground(Color.yellow);
    }
    private JScrollPane scrollPane = new JScrollPane(yellowPanel);

    {
        scrollPane.setPreferredSize(new Dimension(400, 300));
    }
    private AbstractAction increaseAction = new AbstractAction() {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            yellowPanel.setPreferredSize(
                    new Dimension(yellowPanel.getPreferredSize().width + 100,
                    yellowPanel.getPreferredSize().height + 100));
            yellowPanel.revalidate();
            yellowPanel.repaint();
        }
    };
    private Timer increaseTimer = new Timer(1000, increaseAction);

    public Try01_JScrollPane() {
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
        increaseTimer.start();
        increaseTimer.setRepeats(true);
    }

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

答案 1 :(得分:1)

JScrollPane从支持内容中剪切视图端口具有滚动窗格布局。 getPreferredSize上的部分引用了此布局。它只是说JScrollPane / view port矩形不受后备内容的影响,反之亦然:内容是根据其首选大小进行的。

因此,首选大小的更改需要新的布局。更多的意思是:

  • 使用setPreferredSize进行初始化。
  • 然后致电setSize调整大小。