简体   繁体   English

不可编辑的自动滚动JTextArea

[英]NON-editable autoscrolling JTextArea

I have a JScrollPane with a JTextArea added to it. 我有一个添加了JTextArea的JScrollPane。 I then add the ScrollPane to the panel created for the gui. 然后我将ScrollPane添加到为gui创建的面板中。 when running everything works as planned. 当运行一切按计划工作。 The text that is typed into the input section is removed from input and added to the output. 键入输入部分的文本将从输入中删除并添加到输出中。 However when the text exceeds the size of the JTextArea it refuses to scroll after it. 但是当文本超过JTextArea的大小时,它拒绝在它之后滚动。 The code i will be providing is a lot as i do not know where the issue is or how to fix it. 我将提供的代码很多,因为我不知道问题出在哪里或如何解决它。

I have browsed StackOverflow and have tried many different things i will give some links. 我浏览过StackOverflow并尝试了很多不同的东西,我会给出一些链接。 How to set AUTO-SCROLLING of JTextArea in Java GUI? 如何在Java GUI中设置JTextArea的AUTO-SCROLLING? . as well as i have used the oracle website. 以及我使用过oracle网站。 https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html . https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html but none of them have helped me. 但他们都没有帮助过我。

public Sork()
{
    txtara = new JTextArea("");
    panel = new JPanel();
    txtfld = new JTextField(""); 
    sb = new JScrollBar();
    scrollBar = new JScrollPane(txtara, 
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    txtfld.setPreferredSize(new Dimension(740, 20));
    txtfld.setLocation(new Point (0, 510));
    txtfld.setBackground(Color.BLACK);
    txtfld.setForeground(Color.WHITE);

    txtara.setPreferredSize(new Dimension(740, 510));
    txtara.setBackground(Color.BLACK);
    txtara.setForeground(Color.WHITE);
    txtara.setEditable(false);

    panel.setPreferredSize(new Dimension(750, 575));
    panel.setForeground(Color.BLACK);
    panel.setBackground(Color.BLACK);

    panel.add(scrollBar);
    panel.add(txtfld);

    txtfld.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {               
            txtara.append("\n" + txtfld.getText());;
            txtfld.setText("");
            txtfld.grabFocus(); 
        }});
    }

I want the JTextArea to auto scroll once the text reaches the bottom. 我希望JTextArea在文本到达底部时自动滚动。 I should also mention i would like the actual scrollbar to not be visible, this is being made for a text based adventure. 我还应该提一下,我希望实际的滚动条不可见,这是基于文本的冒险。 therefore the less showing the better. 因此越少越好。

The following works for me. 以下适用于我。 It uses hardly any of your code because I felt it would be more beneficial to you if I started from scratch. 它几乎不使用你的任何代码,因为如果我从头开始,我觉得它会对你更有益。 The code below displays a JTextField where you enter text. 下面的代码显示了一个输入文本的JTextField Below the JTextField is a (non-editable) JTextArea . JTextField下面是一个(不可编辑的) JTextArea Hitting the Enter key when keyboard focus is in the JTextField will append its text to the JTextArea . 当键盘焦点位于JTextField时按Enter键会将其文本附加到JTextArea I recommend you make sure you thoroughly understand the code as I think it will push you along the Swing learning curve. 我建议你确保你完全理解代码,因为我认为它将推动你沿着Swing学习曲线。

For your information the below code requires at least Java 7 since it uses multi-catch . 为了您的信息,以下代码至少需要Java 7,因为它使用multi-catch

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

public class Sork implements ActionListener, Runnable {
    private JButton exitButton;
    private JFrame frame;
    private JTextArea txtara;
    private JTextField txtfld;

    @Override // java.awt.event.ActionListener
    public void actionPerformed(ActionEvent actnEvnt) {
        Object src = actnEvnt.getSource();
        if (src == exitButton) {
            System.exit(0);
        }
        else if (src == txtfld) {
            txtara.append("\n");
            txtara.append(txtfld.getText());
            txtfld.setText("");
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    private JPanel createButton() {
        JPanel panel = new JPanel();
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        panel.add(exitButton);
        return panel;
    }

    private JScrollPane createTextArea() {
        txtara = new JTextArea(2, 10);
        JScrollPane scrollPane = new JScrollPane(txtara);
        txtara.setLineWrap(true);
        txtara.setWrapStyleWord(true);
        txtara.setEditable(false);
        return scrollPane;
    }

    private JPanel createTextField() {
        JPanel panel = new JPanel();
        txtfld = new JTextField(10);
        txtfld.addActionListener(this);
        panel.add(txtfld);
        return panel;
    }

    private void showGui() {
        frame = new JFrame("Sork");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTextField(), BorderLayout.PAGE_START);
        frame.add(createTextArea(), BorderLayout.CENTER);
        frame.add(createButton(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        String slaf = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(slaf);
        }
        catch (ClassNotFoundException |
               InstantiationException |
               IllegalAccessException |
               UnsupportedLookAndFeelException x) {
            System.err.println("WARN (ignored): Failed to set [System] look-and-feel.");
            x.printStackTrace();
        }
        Sork instance = new Sork();
        EventQueue.invokeLater(instance);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM