如何制作用户可调整大小的 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);

    }

}


拉丁的传说
浏览 181回答 3
3回答

慕的地6264312

您可以使用JSplitPane使窗口的各个区域可调整大小。试试下面的例子。请参阅我在代码中的评论。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();    // Set BorderLayout so that scroll pane fills the panel    p.setLayout(new BorderLayout());    JButton button = new JButton("Whatever here");    JTextArea t2 = new JTextArea(5, 30);    JScrollPane scrollPane = new JScrollPane(t2);    // Setting scroll pane size is not necessary    //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);    // Use JSplitPane to make the panels resizable    f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);    f.setSize(600, 500);    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);    f.setVisible(true);  }}

慕婉清6462132

作为第一个改进,您可以进行一些小的布局更改,这将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);    }}

慕的地10843

这是 c0der 的实现,但设置了更简化的生命周期。每一行都解释了正在发生的事情和时间。import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.JTextComponent;public class TextApp implements Runnable {&nbsp; &nbsp; private static final String APP_NAME = "Text App";&nbsp; &nbsp; private JFrame frame;&nbsp; &nbsp; private JTextArea txtAra;&nbsp; &nbsp; private JButton button;&nbsp; &nbsp; // This is a generic action that handles clearing the text of a JTextComponent&nbsp; &nbsp; // It can also be a stand-alone class&nbsp; &nbsp; private static class ClearAction <T extends JTextComponent> implements ActionListener {&nbsp; &nbsp; &nbsp; &nbsp; private T txtAra;&nbsp; &nbsp; &nbsp; &nbsp; public ClearAction(T txtAra) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.txtAra = txtAra;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.txtAra.setText("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public TextApp() {&nbsp; &nbsp; &nbsp; &nbsp; // Initialize instance fields&nbsp; &nbsp; &nbsp; &nbsp; frame = new JFrame(APP_NAME);&nbsp; &nbsp; &nbsp; &nbsp; txtAra = new JTextArea(5, 30);&nbsp; &nbsp; &nbsp; &nbsp; button = new JButton("Clear Text");&nbsp; &nbsp; &nbsp; &nbsp; // Internal panels used for layout&nbsp; &nbsp; &nbsp; &nbsp; JPanel mainPanel = new JPanel(new GridLayout(1, 1));&nbsp; &nbsp; &nbsp; &nbsp; JScrollPane scrollPane = new JScrollPane(txtAra);&nbsp; &nbsp; &nbsp; &nbsp; JPanel buttonPanel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; // Add components to containers&nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; frame.add(mainPanel, BorderLayout.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(buttonPanel, BorderLayout.PAGE_END);&nbsp; &nbsp; &nbsp; &nbsp; mainPanel.add(scrollPane);&nbsp; &nbsp; &nbsp; &nbsp; buttonPanel.add(button);&nbsp; &nbsp; &nbsp; &nbsp; // Additional setup&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setSize(600, 400);&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);&nbsp; &nbsp; &nbsp; &nbsp; // Add listeners&nbsp; &nbsp; &nbsp; &nbsp; button.addActionListener(new ClearAction(txtAra));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; // Set text&nbsp; &nbsp; &nbsp; &nbsp; txtAra.setText("this is some random text\nthat may go for many rows\nso it may look messy");&nbsp; &nbsp; &nbsp; &nbsp; // Prepare frame&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(new TextApp());&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java