如何使用户可调整大小的JTextArea?

时间:2019-03-06 13:33:59

标签: java swing user-interface jtextarea

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

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

简化代码是

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

3 个答案:

答案 0 :(得分:2)

您可以使用JSplitPane来调整窗口的各个区域的大小。请尝试以下示例。在代码中也请参阅我的评论。 导入javax.swing。*; 导入java.awt。*; 公共类有问题的{   公共静态void main(String [] args){     JFrame f =新的JFrame(“ frame”);     f.setLayout(new BorderLayout());     JPanel p1 =新的JPanel();     JPanel p =新的JPanel();     //设置BorderLayout,以便滚动窗格填充面板     p.setLayout(new BorderLayout());     JButton按钮=新的JButton(“ What here here”);     JTextArea t2 =新的JTextArea(5,30);     JScrollPane scrollPane =新的JScrollPane(t2);     //不需要设置滚动窗格的大小     //scrollPane.setSize(600,400);     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS);     t2.setText(“这是一些随机的文本\ n,可能会出现很多行\ n,因此看起来很乱”);     p1.add(button);     p.add(scrollPane);     //使用JSplitPane使面板可调整大小     f.add(新的JSplitPane(JSplitPane.VERTICAL_SPLIT,p,p1),BorderLayout.CENTER);     f.setSize(600,500);     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);     f.setVisible(true);   } }

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

这是c0der的实现,但设置了更简化的生命周期。每行代码都说明发生了什么情况以及何时发生。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class TextApp implements Runnable {
    private static final String APP_NAME = "Text App";

    private JFrame frame;
    private JTextArea txtAra;
    private JButton button;

    // This is a generic action that handles clearing the text of a JTextComponent
    // It can also be a stand-alone class
    private static class ClearAction <T extends JTextComponent> implements ActionListener {
        private T txtAra;

        public ClearAction(T txtAra) {
            this.txtAra = txtAra;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            this.txtAra.setText("");
        }
    }

    public TextApp() {
        // Initialize instance fields
        frame = new JFrame(APP_NAME);
        txtAra = new JTextArea(5, 30);
        button = new JButton("Clear Text");

        // Internal panels used for layout
        JPanel mainPanel = new JPanel(new GridLayout(1, 1));
        JScrollPane scrollPane = new JScrollPane(txtAra);
        JPanel buttonPanel = new JPanel();

        // Add components to containers
        frame.setLayout(new BorderLayout());
        frame.add(mainPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.PAGE_END);
        mainPanel.add(scrollPane);
        buttonPanel.add(button);

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

        // Add listeners
        button.addActionListener(new ClearAction(txtAra));
    }

    @Override
    public void run() {
        // Set text
        txtAra.setText("this is some random text\nthat may go for many rows\nso it may look messy");

        // Prepare frame
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TextApp());
    }
}