如何使用户可调整大小的JTextArea? [英] How to make user-resizable JTextArea?

查看:147
本文介绍了如何使用户可调整大小的JTextArea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎唯一的选择是设置行数,但是我需要为用户调整文本区域的大小. JScrollPane有帮助,但是当文本很多时,我想让用户自己调整区域的大小.

Seems like the only option is to set number of rows, but I need my text area to be resizable for user. JScrollPane helps, but when there's a lot of text, I want to let user resize the area itself.

我该怎么做?如果我可以为此目的使用另一个类,那么我会完全同意的.

How do I do this? If I can use another class for this purpose, I'll be totally ok with it.

简化代码是

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

public class Problematic {

    public static void main(String[] args) {
        JFrame f = new JFrame("frame");
        f.setLayout(new BorderLayout());

        JPanel p1 = new JPanel();
        JPanel p = new JPanel();

        JButton button = new JButton("Whatever here");
        JTextArea t2 = new JTextArea(5, 30);

        JScrollPane scrollPane = new JScrollPane(t2);

        scrollPane.setSize(600, 400);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");

        p1.add(button);
        p.add(scrollPane);

        f.add(p, BorderLayout.NORTH);
        f.add(p1, BorderLayout.CENTER);

        f.setSize(600, 500);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

推荐答案

作为第一个改进,您可以进行一些小的布局更改,以使JTextArea占据整个空间并随着框架调整大小:

As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class Problematic {

    public static void main(String[] args) {
        JFrame f = new JFrame("frame");
        f.setLayout(new BorderLayout()); 

        JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel 
        JTextArea t2 = new JTextArea(5, 30);
        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");

        JScrollPane scrollPane = new JScrollPane(t2);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        p.add(scrollPane);
        f.add(p, BorderLayout.CENTER); //place text area panel in center position 

        JPanel p1 = new JPanel();
        JButton button = new JButton("Whatever here");
        p1.add(button);
        f.add(p1, BorderLayout.PAGE_END);

        f.setSize(600, 500);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

为获得更大的灵活性,您可以添加JSplitPane:

for additional flexibility you can add a JSplitPane :

public class Problematic {

    public static void main(String[] args) {
        JFrame f = new JFrame("frame");
        f.getContentPane().setLayout(new BorderLayout());

        JPanel p1 = new JPanel();
        JButton button = new JButton("Whatever here");
        p1.add(button);

        JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
        JTextArea t2 = new JTextArea(5, 30);
        t2.setText("this is some random text\nthat may go for many rows\nso it may look messy");

        JScrollPane scrollPane = new JScrollPane(t2);       scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        p.add(scrollPane);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
        f.getContentPane().add(splitPane, BorderLayout.CENTER);

        f.setSize(600, 500);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

这篇关于如何使用户可调整大小的JTextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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