将ScrollPane添加到JTextArea

将ScrollPane添加到JTextArea,第1张

将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 :



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5487880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存