Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 窗格的自动旋转_Java_Text Editor_Jeditorpane - Fatal编程技术网

Java 窗格的自动旋转

Java 窗格的自动旋转,java,text-editor,jeditorpane,Java,Text Editor,Jeditorpane,我使用JEditorPane作为编辑器在应用程序中编写注释。内容类型设置为“文本/普通”。当我在其中写入文本时,文本会填满可用空间,然后我继续键入,文本不会向上移动以显示光标。所以我不知道我在哪里打字,我在打什么,因为它是可见的 你能告诉我如何通过向上移动上面的文字来始终显示插入符号吗 相反,如果我可以在打字时自动调整编辑器的大小,效果会更好。JEditorPane在JPanel中,所以我也必须调整它的大小。有什么想法吗?您需要将编辑器放在JScrollPane中。滚动窗格将自动添加滚动条并消除

我使用JEditorPane作为编辑器在应用程序中编写注释。内容类型设置为“文本/普通”。当我在其中写入文本时,文本会填满可用空间,然后我继续键入,文本不会向上移动以显示光标。所以我不知道我在哪里打字,我在打什么,因为它是可见的

你能告诉我如何通过向上移动上面的文字来始终显示插入符号吗


相反,如果我可以在打字时自动调整编辑器的大小,效果会更好。JEditorPane在JPanel中,所以我也必须调整它的大小。有什么想法吗?

您需要将编辑器放在JScrollPane中。滚动窗格将自动添加滚动条并消除调整编辑器大小的需要。

您需要将编辑器放在JScrollPane中。滚动窗格将自动添加滚动条,无需调整编辑器大小。

编辑以添加完整解决方案

您必须首先添加一个JScrollPane。然后,如果不希望滚动条可见,但希望文本区域自动滚动,请设置

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
在滚动窗格上。这将隐藏滚动条,但为您提供自动滚动

下面是如何使用滚动窗格实现自动滚动,并自动调整大小到给定的最大值

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class SPTest extends JFrame {

    private static final long serialVersionUID = 1L;

    private JEditorPane editor;
    private JScrollPane scrollPane;
    private JPanel topPanel;
    private JLabel labelTop;

    public SPTest() {
        super("Editor test");
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initComponents() {
        editor = new JEditorPane("text/plain", null);
        scrollPane = new JScrollPane(editor);
        topPanel = new JPanel();
        labelTop = new JLabel("main contents here");
        topPanel.add(labelTop);

        setSize(600, 400);
        editor.setMinimumSize(new Dimension(100, 30));
        editor.setPreferredSize(new Dimension(100, 60));
        scrollPane.setPreferredSize(new Dimension(600, 60));
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setMinimumSize(new Dimension(100, 30));

        final int MAX_HEIGHT_RSZ = 120;
        editor.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ);
                Dimension preferredSize = scrollPane.getPreferredSize();
                preferredSize.height = height;
                scrollPane.setPreferredSize(preferredSize);
                SPTest.this.validate();
            }
        });

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new SPTest();
    }

}

您可以调整大小。您可以使用此JScrollPane而不是JPanel作为编辑器的容器。

编辑以添加完整的解决方案

您必须首先添加一个JScrollPane。然后,如果不希望滚动条可见,但希望文本区域自动滚动,请设置

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
在滚动窗格上。这将隐藏滚动条,但为您提供自动滚动

下面是如何使用滚动窗格实现自动滚动,并自动调整大小到给定的最大值

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class SPTest extends JFrame {

    private static final long serialVersionUID = 1L;

    private JEditorPane editor;
    private JScrollPane scrollPane;
    private JPanel topPanel;
    private JLabel labelTop;

    public SPTest() {
        super("Editor test");
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initComponents() {
        editor = new JEditorPane("text/plain", null);
        scrollPane = new JScrollPane(editor);
        topPanel = new JPanel();
        labelTop = new JLabel("main contents here");
        topPanel.add(labelTop);

        setSize(600, 400);
        editor.setMinimumSize(new Dimension(100, 30));
        editor.setPreferredSize(new Dimension(100, 60));
        scrollPane.setPreferredSize(new Dimension(600, 60));
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setMinimumSize(new Dimension(100, 30));

        final int MAX_HEIGHT_RSZ = 120;
        editor.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ);
                Dimension preferredSize = scrollPane.getPreferredSize();
                preferredSize.height = height;
                scrollPane.setPreferredSize(preferredSize);
                SPTest.this.validate();
            }
        });

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new SPTest();
    }

}

您可以调整大小。您可以使用此JScrollPane而不是JPanel作为编辑器的容器。

否我不能使用scollpane。至少我不能显示滚动条…:(不,我不能使用scollpane。至少我不能显示滚动条…:(不,我不能使用scollpane。至少我不能显示滚动条…:(不,我不能使用scollpane。至少我不能显示滚动条…:(只需执行
myJEditorPane.setCaretPosition(myJEditorPane.getDocument().getLength())如何)每次更新文本时;
如何?每次更新文本时,只需执行
myJEditorPane.setCaretPosition(myJEditorPane.getDocument().getLength());