简体   繁体   English

JTextArea滚动不起作用

[英]JTextArea scrolling not working

I am trying to make a JTextArea vertically scrollable. 我试图使JTextArea垂直滚动。 I did some research and I'm pretty sure I'm using a LayoutManager, not adding JTextArea directly to the parent panel, and is setting the preferred size of both JTextArea and JScrollPane. 我做了一些研究,我很确定自己正在使用LayoutManager,而不是直接将JTextArea添加到父面板,并且正在设置JTextArea和JScrollPane的首选大小。 Not sure what am I missing here... Here's the code: 不知道我在这里缺少什么...这是代码:

public class ConsolePane extends JDialog {
    private static final long serialVersionUID = -5034705087218383053L;

    public static final Dimension CONSOLE_DIALOG_SIZE = new Dimension(400, 445);
    public static final Dimension CONSOLE_LOG_SIZE = new Dimension(400, 400);
    public static final Dimension CONSOLE_INPUT_SIZE = new Dimension(400, 25);

    private static ConsolePane instance = new ConsolePane();

    public static ConsolePane getInstance() {
        return instance;
    }

    private JTextArea taLog;
    private JTextField tfInput;

    public ConsolePane() {
        this.setTitle("Console");
        JPanel contentPane = new JPanel();
        this.setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(createConsoleLog(), BorderLayout.CENTER);
        contentPane.add(createConsoleInput(), BorderLayout.SOUTH);
        contentPane.setPreferredSize(CONSOLE_DIALOG_SIZE);
    }

    private JComponent createConsoleLog() {
        taLog = new JTextArea();
        taLog.setLineWrap(true);
        taLog.setPreferredSize(CONSOLE_LOG_SIZE);

        ((DefaultCaret) taLog.getCaret())
                .setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        JScrollPane container = new JScrollPane(taLog);
        container
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        container
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        container.setPreferredSize(CONSOLE_LOG_SIZE);
        return container;
    }

    private JComponent createConsoleInput() {
        tfInput = new JTextField();
        tfInput.setPreferredSize(CONSOLE_INPUT_SIZE);
        tfInput.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                taLog.append(tfInput.getText() + "\r\n");
                tfInput.setText("");
            }
        });
        tfInput.requestFocus();
        return tfInput;
    }

    public static void main(String[] args) {
        ConsolePane.getInstance().pack();
        ConsolePane.getInstance().setVisible(true);
    }
}

Thx in advance! 提前谢谢!

Try the following code, It may help you 尝试以下代码,它可能会帮助您

JTextArea txt=new JTextArea();
JScrollPane pane=new JScrollPane(txt,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Figured it out myself. 我自己想通了。 taLog.setPreferredSize() is preventing the scrolling. taLog.setPreferredSize()阻止滚动。 removed that and everything worked fine. 删除了,一切正常。 Thx everyone for your help. 谢谢大家的帮助。

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

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