使JFrame中的JTextArea或JEditorPane可滚动 [英] Making the JTextArea or JEditorPane in a JFrame scrollable

查看:281
本文介绍了使JFrame中的JTextArea或JEditorPane可滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些有关Swing的研究,以便使用Java构建CSS编辑器.我在尝试导出JTextArea的CSS和HTML时遇到了麻烦(我将在创建.css文档之后.) 这是我的主布局在单击构建"菜单项后调用的GridLayout.

I've been doing some research about Swing in order to build a css editor with Java. 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.

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);

    }
}

这很简单.只需帮助我将滚动条或窗格添加到这些textArea即可.网站上的任何其他建议均无效.

It's pretty straight forward. Just help me adding scroll bars or panes to these textArea's. Any other suggestions in the website do not work.

推荐答案

这种方式...

JTextArea text = new JTextArea();

JScrollPane scroll = new JScrollPane(text);

已编辑的部分

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();
            }
        });
    }
}

这篇关于使JFrame中的JTextArea或JEditorPane可滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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