添加填充有文本的 JTextArea/JTextPane 时,防止 JSscrollPane 调整大小

问题描述

互联网上的你好

我的问题是,当我在 setViewPortView(textArea) 上调用 JScrollPane 并且 JTextArea 已经包含文本时,JScrollPane 会调整大小以适应 JTextArea 的整个文本{1}}。我希望滚动窗格保持相同的大小,同时将其余文本隐藏在下面的滚动窗格中,以便我可以向下滚动以查看其余文本。

请参阅以下应该可以运行的示例:

  1. 点击“编辑”按钮。 JTextPane 现在替换为可编辑的 JTextArea
  2. 在按钮上方输入 JTextArea。输入足够多的文本,使其开始按预期向下滚动,多行文本。
  3. 再次单击现在标题为“保存”的同一个按钮。现在,不可编辑的 JTextPane 将显示在 JScrollPane 而不是 JTextArea
  4. 看看 JSCrollPane 如何调整大小?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
    private final JScrollPane scrollPane;
    private final JTextArea textArea;
    private final JTextPane textPane;
    private final JButton editButton;
    private String text;

    private boolean isEditing = false;


    public Main() {
        setLayout(new GridBagLayout());
        textArea = new JTextArea();
        textArea.setVisible(false);
        textPane = new JTextPane();
        textPane.setEditable(false);
        scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setViewportView(textPane);
        editButton = new JButton("Edit");
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isEditing = !isEditing;
                textPane.setVisible(!isEditing);
                textArea.setVisible(isEditing);
                if(isEditing) {
                    scrollPane.setViewportView(textArea);
                    editButton.setText("Save");
                }
                else {
                    scrollPane.setViewportView(textPane);
                    editButton.setText("Edit");
                    text = textArea.getText();
                }
                textPane.setText(text);
            }
        });

        GridBagConstraints bag = new GridBagConstraints();
        bag.gridx = 0;
        bag.gridy = 0;
        bag.gridwidth = 3;
        bag.gridheight = 1;
        bag.weightx = 1.0;
        bag.weighty = 1.0;
        bag.fill = GridBagConstraints.BOTH;
        bag.anchor = GridBagConstraints.NORTH;
        bag.insets = new Insets(5,5,5);
        add(scrollPane,bag);

        bag = new GridBagConstraints();
        bag.gridx = 1;
        bag.gridy = 1;
        bag.gridwidth = 1;
        bag.gridheight = 1;
        bag.weightx = 1.0;
        bag.weighty = 1.0;
        add(editButton,bag);

        setSize(1000,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Main ui = new Main();
        });
    }
}

PS:我正在切换到 JTextPane,因为我打算呈现我已排除的降价文本以保持示例简单。我正在使用 JDK 8u231

我尝试保存原始尺寸并在换出它们时使用 setPreferredSizesetMaximumSize 进行设置,但它没有解决我的问题。我还想在窗口调整大小时保持可调整大小。

更新:我需要使用 GridBag 布局,因为我有一个 JPanel,我通过覆盖它上方的 paint() 函数在其上进行绘制

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)