将 ScrollPane 添加到 JTextArea [英] Adding ScrollPane to JTextArea

查看:28
本文介绍了将 ScrollPane 添加到 JTextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的大学课程设计一个项目.我只是想知道是否有人知道如何将滚动条添加到 JTextArea.目前我的 GUI 布局正确,唯一缺少的是滚动条.

I am working on a project for my college course. I was just wondering if anyone knew how to add a scrollBar to a JTextArea. At present I have the GUI laid out correctly, the only thing missing is the scroll bar.

这就是 GUI 的样子.正如您在第二个 TextArea 上看到的,我想添加滚动条.

This is what the GUI looks like. As you can see on the second TextArea I would like to add the Scrollbar.

这是我创建窗格的代码.但似乎什么也没发生... t2 是我想将其添加到的 JTextArea.

This is my code where I create the pane. But nothing seems to happen... t2 is the JTextArea I want to add it to.

scroll = new JScrollPane(t2);
    scroll.setBounds(10,60,780,500);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

任何帮助都会很棒,谢谢!

Any help would be great, thanks!

推荐答案

当您的文本超出您的视图区域的边界时,滚动条就会出现.不要使用绝对定位,在这样的闲聊中手,总是喜欢 布局管理器,请阅读第一段第一个链接,了解使用布局管理器的优势.

The Scroll Bar comes when your text goes beyond the bounds of your view area. Don't use Absolute Positioning, for such a small talk at hand, always prefer Layout Managers, do read the first para of the first link, to know the advantage of using a Layout Manager.

你只需要使用这个东西:

What you simply need to do is use this thingy :

JTextArea msgArea = new JTextArea(10, 10);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);      

JScrollPane msgScroller = new JScrollPane();        
msgScroller.setBorder(
    BorderFactory.createTitledBorder("Messages"));
msgScroller.setViewportView(msgArea);

panelObject.add(msgScroller);

这里有一个小程序供大家理解:

Here is a small program for your understanding :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextAreaScroller
{
    private JTextArea msgArea;
    private JScrollPane msgScroller;
    private JTextArea logArea;
    private JScrollPane logScroller;
    private JButton sendButton;
    private JButton terminateButton;
    private Timer timer;
    private int counter = 0;
    private String[] messages = {
                                    "Hello there
",
                                    "How you doing ?
",
                                    "This is a very long text that might won't fit in a single line :-)
",
                                    "Okay just to occupy more space, it's another line.
",
                                    "Don't read too much of the messages, instead work on the solution.
",
                                    "Byee byee :-)
",
                                    "Cheers
"
                                };

    private ActionListener timerAction = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (counter < messages.length)
                msgArea.append(messages[counter++]);
            else
                counter = 0;
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Chat Messenger Dummy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(0, 1, 5, 5));

        logArea = new JTextArea(10, 10);
        logArea.setWrapStyleWord(true);
        logArea.setLineWrap(true);      

        logScroller = new JScrollPane();        
        logScroller.setBorder(
            BorderFactory.createTitledBorder("Chat Log"));
        logScroller.setViewportView(logArea);

        msgArea = new JTextArea(10, 10);
        msgArea.setWrapStyleWord(true);
        msgArea.setLineWrap(true);      

        msgScroller = new JScrollPane();        
        msgScroller.setBorder(
            BorderFactory.createTitledBorder("Messages"));
        msgScroller.setViewportView(msgArea);

        centerPanel.add(logScroller);
        centerPanel.add(msgScroller);

        JPanel bottomPanel = new JPanel();

        terminateButton = new JButton("Terminate Session");
        terminateButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (timer.isRunning())
                    timer.stop();
                else
                    timer.start();
            }
        });
        sendButton = new JButton("Send");

        bottomPanel.add(terminateButton);
        bottomPanel.add(sendButton);

        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(bottomPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new JTextAreaScroller().displayGUI();
            }
        });
    }
}

这是相同的结果:

这篇关于将 ScrollPane 添加到 JTextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆