栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将ScrollPane添加到JTextArea

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将ScrollPane添加到JTextArea

当您的文本超出查看区域的范围时,滚动条就会出现。不要使用绝对定位,因为手头这么闲聊,总是喜欢布局管理器,请务必阅读第一个链接的第一段,以了解使用布局管理器的优势。

您只需要做的就是使用这个东西:

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 theren",   "How you doing ?n",   "This is a very long text that might won't fit in a single line :-)n",   "Okay just to occupy more space, it's another line.n",   "Don't read too much of the messages, instead work on the solution.n",   "Byee byee :-)n",   "Cheersn"          };    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(); }        });    }}

Here is the outcome of the same :



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号