如何调整 JTextArea 的大小以适应 JFrame 的百分比 [英] How to resize JTextArea to fit percentage of JFrame

查看:26
本文介绍了如何调整 JTextArea 的大小以适应 JFrame 的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这绝对是一个菜鸟问题.如何调整两个 JTextArea 面板的大小,使它们看起来像这样:

This is definitely a noob question. How do I resize two JTextArea panels so they look something like this:

aaaaaaaaaaaaaa
a   a        a
a   a        a
a   a        a
aaaaaaaaaaaaaa

第一个区域的宽度约为第二个区域的十分之一.我还必须将它包含在一个新的滚动窗格中,但我已经处理好了.调整大小功能似乎不起作用.

With the first area about a tenth of the width of the second. I must also enclose this in a new scroll pane, but I've taken care of that. the resize function doesn't seem to be working.

推荐答案

您可以使用 JSplitPane 水平(或垂直)拆分两个组件(文本区域).这种方法让用户可以自由地将分隔符(即两个区域的垂直分隔符)移动到他/她喜欢的位置.

You can use a JSplitPane to split horizontally (or vertically) the two components (text areas). This approach lets the user to freely move the divider (ie the vertical separator of the two areas) to a location he/she prefers.

关于各个组件空间分配的偏好,可以使用setResizeWeight 方法,它将根据值分配为拆分窗格(每次用户调整窗口大小)分配的新空间你指定.例如,使用 0.5 的值调用此方法会将新大小平均分配给左右组件.0 的值将在正确的组件中提供所有额外空间.1 的值会将其赋予左侧组件.1.0/3.0 的值会将新空间拆分为三个,然后将前三分之一保留到左侧组件,将其他三分之二保留到右侧组件.等等……

As for the preference on the allocation of space of each component, you can use the setResizeWeight method which will distribute the new space allocated for the split pane (each time the user resizes the window) according to the value you specify. For example calling this method with a value of 0.5 will distribute the new size equally to both left and right components. A value of 0 will give all the extra space at the right component. A value of 1 will give it to the left component. A value of 1.0 / 3.0 will split the new space to three and then reserve the first third to the left component and the other two thirds to the right component. And so on...

这应该是很好的用户体验,尽管如果您不希望用户自己重新定位分隔线,请使用 camickr 的答案.

This should be good user-experience, although if you don't want the user to relocate the divider by himself/herself, then use camickr's answer.

这是一个工作示例:

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Main {
    private static Component buildTextAreaContainer() {
        final JTextArea txt = new JTextArea();
        final JScrollPane scroll = new JScrollPane(txt);
        return scroll;
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, buildTextAreaContainer(), buildTextAreaContainer());
            //split.setContinuousLayout(true);
            //split.setOneTouchExpandable(true);
            split.setResizeWeight(1d / 3d); //one third for left component, two thirds for right component.
            split.setPreferredSize(new Dimension(1000, 600));

            final JFrame frame = new JFrame("Splitted text areas test.");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(split);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

要了解有关如何使用 JSplitPane 的更多信息,您可以阅读 相应的教程或文档本身.

To learn more about how to use the JSplitPane you can read the corresponding tutorial or the docs themselves.

这篇关于如何调整 JTextArea 的大小以适应 JFrame 的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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