为什么JScrollPane中的JPanel不能滚动?

时间:2012-02-07 21:45:57

标签: java swing jscrollpane

我在JPanel放置了JScrollPane(黄色)。

pic1

当我在JTextPane中输入一些文字时,它会调整大小,但垂直滚动条仍然无效。 yelowPanel.getSize()返回与之前相同的值。(您可以在redPanel上看到它。)

pic2

那么如何刷新yellowPanel?我想垂直滚动面板。 我试着:

panelCreating.revalidate();
panelCreating.invalidate();
panelCreating.repaint();

仅适用panelCreating.setPreferredSize(new Dimension(333, 777));,但我不知道要设置的大小。这取决于内容。

有一个小例子:

package swingtest;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class SwingTest extends JFrame {

    public SwingTest() {
        initComponents();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SwingTest().setVisible(true);
            }
        });
    }
    private JPanel panelCenter, panelCreating;
    private JScrollPane scrollPaneCreating, scrollPaneCenter;
    private JTextPane textPane1, textPane2;
    private JButton button1;

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(300, 300));

        panelCreating = new JPanel();
        panelCreating.setMinimumSize(new Dimension(160, 200));
        panelCreating.setPreferredSize(new Dimension(160, 200));
        scrollPaneCreating = new JScrollPane(panelCreating,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        textPane1 = new JTextPane();
        textPane1.setText("a\na");
        textPane2 = new JTextPane();
        textPane2.setText("b\nb");
        button1 = new JButton("+++");

        panelCenter = new JPanel();
        panelCenter.setBackground(Color.blue);
        scrollPaneCenter = new JScrollPane(panelCenter);



        // ----------------- Left Panel Init -----------------------

        panelCreating.setLayout(new GridBagLayout());
        panelCreating.setBackground(Color.ORANGE);
        panelCreating.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(0, 0, 4, 4);
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.weightx = c.weighty = 0;

        c.gridx = 0;
        c.gridy = GridBagConstraints.RELATIVE;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridheight = 1;
        c.fill = GridBagConstraints.BOTH;
        panelCreating.add(textPane1, c);

        button1.addActionListener(new ActionListener() {

            int height = 50;

            @Override
            public void actionPerformed(ActionEvent e) {
                textPane1.setText(textPane1.getText() + "\na");
                textPane1.setPreferredSize(new Dimension(150, height));
                textPane2.setText(textPane2.getText() + "\nb");
                textPane2.setPreferredSize(new Dimension(150, height));
                height += 30;

                panelCreating.revalidate();
                panelCreating.repaint();
                scrollPaneCreating.revalidate();
            }
        });
        panelCreating.add(button1, c);

        panelCreating.add(textPane2, c);

        // -------------------------------------------------------

        getContentPane().setLayout(new GridBagLayout());
        c = new GridBagConstraints();

        c.ipadx = c.ipady = 0;
        c.insets = new Insets(0, 0, 0, 0);
        c.weighty = 0;
        c.gridheight = 1;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.weightx = 0;
        c.fill = GridBagConstraints.BOTH;
        getContentPane().add(scrollPaneCreating, c);


        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        getContentPane().add(scrollPaneCenter, c);

    }
}

黄色面板也使用GridBagLayout。 对不起我的英文

3 个答案:

答案 0 :(得分:6)

不要在panelCreating上设置首选尺寸,而是在scrollPaneCreating上设置。并且不要在文本组件上设置首选大小,它们会随着向其添加新行文本而增长。我们的想法是让滚动窗格内的面板增大到所需的大小,并且只限制滚动窗格本身的大小。

    // [...]

    panelCreating = new JPanel();
    //panelCreating.setMinimumSize(new Dimension(160, 200));
    //panelCreating.setPreferredSize(new Dimension(160, 200));
    scrollPaneCreating = new JScrollPane(panelCreating,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPaneCreating.setMinimumSize(new Dimension(160, 200));
    scrollPaneCreating.setPreferredSize(new Dimension(160, 200));

    // [...]

        @Override
        public void actionPerformed(ActionEvent e) {
            textPane1.setText(textPane1.getText() + "\na");
            //textPane1.setPreferredSize(new Dimension(150, height));
            textPane2.setText(textPane2.getText() + "\nb");
            //textPane2.setPreferredSize(new Dimension(150, height));
            //height += 30;

            // This isn't necessary either
            //panelCreating.revalidate();
            //panelCreating.repaint();
            //scrollPaneCreating.revalidate();
        }

编辑添加:另一种方法是在附加到滚动窗格的JViewport上设置大小。视口是显示内容的位置。您可以将滚动窗格视为由视口和滚动条组成。如果滚动窗格设置为固定大小,则通过减去滚动条大小来确定视口大小。但是,如果视口设置为固定大小,则通过将滚动条大小添加到视口大小来确定滚动窗格大小。如果要精确控制屏幕上应显示的内容量,最好在视口上设置固定大小,因为滚动条的大小可能因操作系统而异。

scrollPaneCreating.getViewport().setMinimumSize(new Dimension(160, 200));
scrollPaneCreating.getViewport().setPreferredSize(new Dimension(160, 200));

答案 1 :(得分:4)

JPanel的默认布局为FlowLayout。请尝试使用GridLayout。有一个相关的例子here。例如,

panelCreating = new JPanel(new GridLayout());
scrollPaneCreating = new JScrollPane(panelCreating);

附录:还要考虑嵌套布局。以下示例使用BoxLayout作为左侧面板。

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

/** @see https://stackoverflow.com/questions/9184476 */
public class SwingTest extends JFrame {

    private static final int N = 8;

    public SwingTest() {
        initComponents();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SwingTest().setVisible(true);
            }
        });
    }
    private JPanel panelCenter, panelCreating;
    private JScrollPane scrollPaneCreating, scrollPaneCenter;

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panelCreating = new JPanel();
        scrollPaneCreating = new JScrollPane(panelCreating,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        panelCenter = new JPanel();
        panelCenter.setBackground(Color.blue);
        scrollPaneCenter = new JScrollPane(panelCenter);

        // ----------------- Left Panel Init -----------------------
        panelCreating.setLayout(new BoxLayout(panelCreating, BoxLayout.Y_AXIS));
        panelCreating.setBackground(Color.orange);
        panelCreating.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));

        panelCreating.add(createTextPane());
        panelCreating.add(Box.createVerticalStrut(N));
        panelCreating.add(createTextPane());
        panelCreating.add(Box.createVerticalStrut(N));
        panelCreating.add(createTextPane());

        // -------------------------------------------------------
        setLayout(new GridLayout(1, 0));
        add(scrollPaneCreating);
        add(scrollPaneCenter);
        pack();
    }

    private JTextPane createTextPane() {
        JTextPane pane = new JTextPane();
        pane.setText(""
            + "Twas brillig and the slithy toves\n"
            + "Did gyre and gimble in the wabe;\n"
            + "All mimsy were the borogoves,\n"
            + "And the mome raths outgrabe.");

        pane.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
        return pane;
    }
}

答案 2 :(得分:0)

我修正了你添加1行的问题。

panelCreating.setPreferredSize(new Dimension((int) panelCreating.getPreferredSize().getWidth(),
                    (int)(panelCreating.getPreferredSize().getHeight()+30)));

必须在以下行之后插入该行;

@Override
        public void actionPerformed(ActionEvent e) {
            textPane1.setText(textPane1.getText() + "\na");
            textPane1.setPreferredSize(new Dimension(150, height));
            textPane2.setText(textPane2.getText() + "\nb");
            textPane2.setPreferredSize(new Dimension(150, height));
            height += 30;

说明: 当您在其中设置textPanes的首选大小时,网格包布局不会设置它的窗格首选大小,但scrollPane仅根据其中窗格的首选大小进行滚动。因此,每次更改窗格中组件的大小时,都必须设置窗格的新首选大小,然后scrollPane将确切知道他必须做什么。这就是我做的,添加一行增加了createPanel的首选大小,这是scrollPanel内的一个