简体   繁体   English

使JFxt中的JTextArea或JEditorPane可滚动

[英]Making the JTextArea or JEditorPane in a JFrame scrollable

I've been doing some research about Swing in order to build a css editor with Java. 我一直在研究Swing,以便用Java构建一个css编辑器。 I'm stuck trying to export CSS and HTML in JTextArea's ( I'll after create .css document. ) Here is the GridLayout that my main layout calls after clicking "Build" menu item. 我一直试图在JTextArea中导出CSS和HTML(我将在创建.css文档之后。)这是我的主要布局在单击“Build”菜单项后调用的GridLayout。

package csseditor_gui_built;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.text.DefaultCaret;
import java.awt.Font;
import java.awt.Color;


public class ExportGridLayout extends JFrame {
    public ExportGridLayout(String HTML, String CSS){


        GridLayout layout = new GridLayout(1,2,2,2);
        setLayout(layout);

        JTextArea textAreaHtml = new JTextArea();
        JTextArea textAreaCss = new JTextArea();

        //Creating a new font.
        Font fontumuz = new Font("Courier New", Font.PLAIN, 12);

        // Setting constructor strings
        textAreaHtml.setText(HTML);
        textAreaCss.setText(CSS);

        //Additional details..
        textAreaHtml.setEditable(false);
        textAreaCss.setEditable(false);

        //Appending font to the textArea's
        textAreaHtml.setFont(fontumuz);
        textAreaCss.setFont(fontumuz);

        // Adding the objects to JFrame
        add(textAreaHtml);
        add(textAreaCss);

    }
}

It's pretty straight forward. 这很直接。 Just help me adding scroll bars or panes to these textArea's. 只是帮我添加滚动条或窗格到这些textArea的。 Any other suggestions in the website do not work. 网站上的任何其他建议都不起作用。

Its this way... 这样......

JTextArea text = new JTextArea();

JScrollPane scroll = new JScrollPane(text);

Edited part 编辑部分

add(scroll);

Here is one working code for your help : 以下是一个可供您帮助的工作代码:

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

public class JTextAreaExample
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("JTextArea Scrollable");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(1, 2, 2, 2));

        JTextArea tArea1 = new JTextArea();
        tArea1.setLineWrap(true);
        JTextArea tArea2 = new JTextArea();
        tArea2.setLineWrap(true);
        tArea1.setText("I got a long long line of text in my JTextArea");
        tArea2.setText("I got a long long line of text in my JTextArea");

        JScrollPane scroller1 = new JScrollPane();
        JScrollPane scroller2 = new JScrollPane();
        scroller1.setViewportView(tArea1);
        scroller2.setViewportView(tArea2);

        contentPane.add(scroller1);
        contentPane.add(scroller2);

        frame.setContentPane(contentPane);
        frame.setSize(100, 100);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JTextAreaExample().createAndDisplayGUI();
            }
        });
    }
}

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

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