首页 > 解决方案 > Java Swing:JTextArea 填充整个分配的空间

问题描述

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

import static java.awt.GridBagConstraints.BOTH;

public class mwe extends JFrame {

    private JPanel x, y, z, u,v,w,jtext;

    private class MyBorderedPanel extends JPanel {
        MyBorderedPanel( String title ) {
            this.setBorder(BorderFactory.createTitledBorder(
                    BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title
            ));
        }
    }

    private class MyTextPanel extends JPanel {
        JTextArea textArea;
        MyTextPanel( String title ) {
            textArea= new JTextArea();
            this.setBorder(BorderFactory.createTitledBorder(
                            BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
                            title));
            this.setLayout(new BorderLayout());
            JScrollPane pane= new JScrollPane();
            pane.add(textArea);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            this.add(pane);
        }
    }

    public mwe() {
        x= new MyBorderedPanel("x");
        y= new MyBorderedPanel("y");
        z= new MyBorderedPanel("z");
        u= new MyBorderedPanel("u");
        v= new MyBorderedPanel("v");
        w= new MyBorderedPanel("w");
        jtext= new MyTextPanel("textArea");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        manageLayout();
    }

    private void manageLayout() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = BOTH;

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = constraints.weighty = 13.00;
        constraints.gridwidth = 1;
        constraints.gridheight = 4;
        this.add(x, constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = constraints.weighty = 8.00;
        constraints.gridwidth = 3;
        constraints.gridheight = 1;
        this.add(y, constraints);


        { //adding dummes: 2x2, 1x1, 1x1
            constraints.gridx = 1;
            constraints.gridy = 1;
            constraints.weightx = constraints.weighty = 2.00;
            constraints.gridwidth = 1;
            constraints.gridheight = 2;
            this.add(u, constraints);

            constraints.gridx = 2;
            constraints.gridy = 1;
            constraints.weightx = constraints.weighty = 1.00;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            this.add(v, constraints);

            constraints.gridx = 2;
            constraints.gridy = 2;
            constraints.weightx = constraints.weighty = 1.00;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            this.add(v, constraints);
        }

        constraints.gridx = 3;
        constraints.gridy = 1;
        constraints.weightx = constraints.weighty = 5.00;
        constraints.gridwidth = 1;
        constraints.gridheight = 3;
        this.add(jtext, constraints);


        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.weightx = constraints.weighty = 3.00;
        constraints.gridwidth = 2;
        constraints.gridheight = 1;
        this.add(z, constraints);
    }
    public static void main( String ... args ) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame= null;
                frame = new mwe();
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

我有上面的代码模拟了应用于 GridBagLayout() 的黄金比例的想法。输出如下: 在此处输入图像描述 现在,您可以看到 TextArea 是空的。我希望它的托管面板基本上充满了“白色”空间,即使此时文本区域是空的。我记得在 SE 读到过这BorderLayout就是要走的路,所以我试了一下。因此,基本上,我希望文本区域将其托管面板填充到边缘,并且滚动窗格也是可见的。

如何做到这一点?可能上述 MWE 最相关的代码是 MyTextPanel 类。

标签: javaswing

解决方案


现在,您可以看到 TextArea 是空的。

这是因为您实际上并未将文本区域添加到滚动窗格中。

JScrollPane pane= new JScrollPane();
pane.add(textArea);

上面的代码是错误的。您不会将组件直接“添加”到 JScrollPane。

相反,您将组件添加JViewportJScrollPane.

这是通过执行以下任一操作来完成的:

JScrollPane pane= new JScrollPane(textArea);

这会将文本区域添加到视口,或者

JScrollPane pane= new JScrollPane();
pane.setViewportView(textArea);

推荐阅读