简体   繁体   English

如何在Java Swing中的TextArea上设置滚动条?

[英]How to set scrollbar on TextArea in Java Swing?

I am working on a project which uses the Swing library in Java. 我正在研究一个使用Java中的Swing库的项目。 Within my window, the TextArea is filled in with some content. 在我的窗口中,TextArea充满了一些内容。

However, the TextArea is static and there is no scrollbar, because of which it only shows some of the content before the content overflows. 但是,TextArea是静态的,没有滚动条,因此它仅在内容溢出之前显示某些内容。

How can I add scrollability to my TextArea and/or make it dynamic? 如何为我的TextArea添加可滚动性和/或使其动态化?

PS- I am very new to Swing and GUI in Java. PS-我对Java的Swing和GUI非常陌生。

I scavenged for a solution on the internet and found plenty, but none worked on implementation. 我在互联网上寻找解决方案并找到了很多解决方案,但都没有实施。

I tried out using a ScrollPane, a Scrollbar. 我尝试了使用ScrollPane(滚动条)。 I even set a Layout manager to BorderLayout() and GridBagLayout, but that just messed the window up and did not solve my problem of the static TextArea. 我什至设置了一个BorderManager()和GridBagLayout的布局管理器,但这只是弄乱了窗口,并没有解决我的静态TextArea问题。

package crawler;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class WebCrawler extends JFrame {

    public WebCrawler() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("WebCrawlerWindow");
        setLocationRelativeTo(null);
        setSize(800, 600);

        JTextField urlName = new JTextField();
        urlName.setName("UrlTextField");
        urlName.setBounds(50,20,600,20);
        add(urlName);

        JTextArea jlb = new JTextArea("HTML code?");//this is the TextArea that needs to be scrollable
        jlb.setName("HtmlTextArea");
        jlb.setBounds(50,45,700,1000);
        jlb.setAutoscrolls(true);
        jlb.setLineWrap(true);
        jlb.setWrapStyleWord(true);
        jlb.disable();
        add(jlb, BorderLayout.CENTER);

        JButton download = new JButton();
        download.setName("RunButton");
        download.setBounds(660,20,70,20);
        download.setText("Get Code");

        var LINE_SEPARATOR = System.getProperty("line.separator");

        download.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String url = urlName.getText().toLowerCase();/* Get url from JTextField */
                try {
                    if(!url.substring(0,7).equals("https://"))
                        url = String.join("","https://", url);

                    final InputStream inputStream = new URL(url).openStream();
                    final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                    final StringBuilder stringBuilder = new StringBuilder();

                    String nextLine;
                    while ((nextLine = reader.readLine()) != null) {
                        stringBuilder.append(nextLine);
                        stringBuilder.append(LINE_SEPARATOR);
                    }

                    final String siteText = stringBuilder.toString();
                    jlb.setText(siteText);
                } catch(Exception ex) {
                    jlb.setText("Link Not Found");
                }
            }
        });
        add(download);

        setLayout(null);
        setVisible(true);
    }
}
jlb.setAutoscrolls(true);
...
add(jlb, BorderLayout.CENTER);

The setAutoScrolls(...) method does not make the text area scrollable. setAutoScrolls(...)方法不会使文本区域可滚动。

You need to add the text area to a JScrollPane and add the scroll pane to the frame: 您需要将文本区域添加到JScrollPane并将滚动窗格添加到框架:

//jlb.setAutoscrolls(true);
...
//add(jlb, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane( jlb );
add(scrollPane, BorderLayout.CENTER);

Also you should NOT be using a null layout. 另外,您不应该使用空布局。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

add(download, BorderLayout.PAGE_START);
//add(download);
//setLayout(null);

Read the section from the Swing tutorial on How to Use Border Layout to understand how the above suggestions affect the layout. 阅读Swing教程中有关如何使用边框布局的部分,以了解上述建议如何影响布局。

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

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