java - JEdi​​torPane 填充 JScrollPane 中的所有可用空间

标签 java swing jscrollpane layout-manager jeditorpane

我在 JPanel 中使用 JEditorPane,在下面的示例中我将其称为 contentPane。内容 Pane 位于 JScrollPane 内。我希望编辑器 Pane 填充尽可能少的空间(只包含它的文本),内容 Pane 中可用的其余空间留空到底部,以及两侧。如果文本变得太大,编辑器 Pane 会变大,最终滚动 Pane 会创建滚动条来浏览所有内容。但是,编辑器 Pane 不是只占用其首选大小(即它包含的文本的大小),而是填满了整个内容 Pane ,后者填满了滚动 Pane 的整个视口(viewport)。下面是一些示例代码来演示我的问题:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class ScrollPaneExample extends JFrame {
    public static void main(String[] args) {
        new ScrollPaneExample();
    }
    public ScrollPaneExample() {
        super("ScrollPaneExample");

        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JEditorPane editorPane = new JEditorPane("text/plain", "");
        editorPane.setBackground(Color.YELLOW);

        for (int i = 0; i < 5; i++)
            editorPane.setText(editorPane.getText() + "Hello World\n");

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.setBackground(Color.BLUE);
        contentPane.add(editorPane);

        JScrollPane scrollPane = new JScrollPane(contentPane);

        add(scrollPane, BorderLayout.CENTER);

        setVisible(true);
    }
}

运行时,窗口如下所示:

Example

通过黄色背景,我们可以看到编辑器 Pane 占据了所有空间。这是我希望布局看起来像的(Photoshopped)示例:

Desired

蓝色背景代表内容面板,黄色代表编辑器面板。

编辑:在我的工作程序中,contentPane 中不仅仅是一个编辑器 Pane 。这就是为什么我为 contentPane 使用 BoxLayout 而不是 FlowLayout 的原因;因为需要垂直页面流布局。

最佳答案

只需为内容 Pane 使用 FlowLayoutBorderLayout 不会遵守首选大小,并且会拉伸(stretch)组件以适应。在 Laying out Components Withing a Container 查看更多关于布局管理器的信息

如果您想为编辑器 Pane 设置初始大小,就像任何其他组件一样 Scollable , 你可以覆盖 getPreferredScrollableViewportSize()设置滚动 Pane View 的大小(添加组件时)

此外,您应该将编辑器添加到滚动 Pane ,而不是面板。执行后者,编辑器将无法在滚动 Pane 中滚动。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class ScrollPaneDemo extends JFrame {
    public static void main(String[] args) {
        new ScrollPaneDemo();
    }

    public ScrollPaneDemo() {
        super("ScrollPaneExample");

        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        JEditorPane editorPane = new JEditorPane("text/plain", "") {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(200, 200);
            }
        };
        editorPane.setBackground(Color.YELLOW);

        for (int i = 0; i < 50; i++) {
            editorPane.setText(editorPane.getText()
                    + "Hello World Hello World\n");
        }

        JScrollPane scrollPane = new JScrollPane(editorPane);

        add(scrollPane);

        setVisible(true);
    }
}

更新

In my working program, there is more than just an editor pane in the contentPane. This is why I am using a BoxLayout instead of a FlowLayout for the contentPane; because the vertical page-flow layout is desired.

BoxLayout 是问题所在。您需要为编辑器 Pane 设置最大尺寸。或者一起摆脱 BoxLayout 。 FlowLayout 或 GridBagLayout 将遵循首选大小


更新 2

这是一个使用 GridBagLayout 的示例,它可能更适合您的“扩展”编辑器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class ScrollPaneExample extends JFrame {
    public static void main(String[] args) {
        new ScrollPaneExample();
    }
    public ScrollPaneExample() {
        super("ScrollPaneExample");

        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        //getContentPane().setBackground(Color.BLUE);

        final JEditorPane editorPane = new JEditorPane("text/plain", "") {
        };

        editorPane.setBackground(Color.YELLOW);

        for (int i = 0; i < 5; i++)
            editorPane.setText(editorPane.getText() + "Hello World\n");

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weighty = 1;
        contentPane.setBackground(Color.BLUE);
        contentPane.add(editorPane, gbc);

        gbc.gridy++;
        contentPane.add(new JButton("Button"), gbc);
        gbc.gridy++;
        contentPane.add(new JButton("Button"), gbc);

        JPanel panel = new JPanel();
        panel.add(contentPane);
        JScrollPane scrollPane = new JScrollPane(panel);

        add(scrollPane);

        setVisible(true);

        Timer timer = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                editorPane.setText(editorPane.getText() + "Hello World\n");
                setVisible(true);
            }
        });
        timer.start();
    }
}

enter image description here

关于java - JEdi​​torPane 填充 JScrollPane 中的所有可用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595693/

相关文章:

java - 在具有操作和鼠标监听器的 GUI 中,未为 JTable 触发 MouseClicked

jquery - jScrollPane 在 AJAX 调用后消失

java - Android中写入 "text"和Resource ID

java - GridBagLayout 内按钮的大小

java - 我应该如何声明和导出模块?

java - JOptionPane 取消时抛出错误

java - EDT 何时开始?

java - 如何实现JList滚动时加载?

java - 如何将 JPanel 添加到 JScrollPane?

java - JScrollPane 隐藏文本区域