繁体   English   中英

设置JTextArea的尺寸(数字行)(Java Swing)

[英]Set dimensions (number lines) of JTextArea (Java Swing)

这是一个愚蠢的数据输入应用程序的代码。 我希望“ oggetto” JTextArea具有一定的大小,以便用户可以输入不同的信息行:

JPanel body = new JPanel();
body.setLayout(new GridLayout(10, 1));
body.add(new JLabel("Inserimento di un nuovo protocollo"));

JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
//oggetto.setSize(100, 100);

...
body.add(oggetto);
jframe.add(body);
jframe.setVisible(true);

我已经尝试了很多方法来实现它,但是当我启动该应用程序时,我只会得到一个独特行的“ oggetto” jtextarea……您能帮我吗?

我建议您使用其他布局,例如GridBagLayoutBorderLayout其他适合此处的布局。

使用GridBagLayout可以在水平和垂直方向上按百分比指定组件的大小。

阅读有关如何使用GridBagLayout的更多信息。

注意:切勿调用组件的setSize()并将其留给布局管理器来决定组件的大小和位置。

阅读更多我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?


示例代码(使用GridBagLayout

JPanel body = new JPanel();
body.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstants.BOTH;
gc.weightx = 1.0; // 100%

gc.gridx = 0;
gc.gridy = 0;
gc.weighty = 0.1;// 10%
body.add(new JLabel("Inserimento di un nuovo protocollo"), gc);

JTextArea oggetto = new JTextArea(5, 1);
oggetto.setOpaque(true);
oggetto.setRows(10);
oggetto.setBackground(Color.cyan);

gc.gridx = 0;
gc.gridy = 1;
gc.weighty = 0.9; // 90%
body.add(oggetto, gc);

问题在于您选择的布局。 因为GridLayout将您的组件分为相等大小的块:

GridLayout类是一个布局管理器,用于将容器的组件布置在矩形网格中。 容器分为相等大小的矩形,每个矩形中放置一个组件。

您应该使用GridBagLayout以获得更大的灵活性。

       public static void main(String[] args) {

            JFrame frame = new JFrame();
            frame.setSize(new Dimension(400, 200));

            JPanel body = new JPanel();
            body.setLayout(new GridBagLayout());

            GridBagConstraints c1 = new GridBagConstraints();
            c1.fill = GridBagConstraints.HORIZONTAL;
            c1.weightx = 1.0;// 100%
            c1.gridx = 0;// column 0
            c1.gridy = 0;// row 0
            body.add(new JLabel("Inserimento di un nuovo protocollo"), c1);

            JTextArea oggetto = new JTextArea(5,1);
            oggetto.setOpaque(true);
            oggetto.setBackground(Color.cyan);

            GridBagConstraints c2 = new GridBagConstraints();
            c2.fill = GridBagConstraints.BOTH;
            c2.weightx = 1.0; // 100%
            c2.gridx = 0; // column 0
            c2.gridy = 1; // row 1

            body.add(oggetto, c2);
            frame.add(body);
            frame.setVisible(true);

        }

我建议使用TableLayout,因为它是理解和实现的最佳布局。

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(400, 200));

    JPanel body = new JPanel();

    double[][] dim = {
            {20, TableLayout.FILL, 20}, // column widths: 20 distance right/left, TableLayout.FILL usable place
            {20, 17, 5, TableLayout.FILL, 20}  // row height: 20 distance up/down, 17 place for label, 5 distance between label-textarea
            };

    body.setLayout(new TableLayout(dim));

    JTextArea oggetto = new JTextArea(5,1);
    oggetto.setOpaque(true);
    oggetto.setBackground(Color.cyan);


    body.add(new JLabel("Inserimento di un nuovo protocollo"), "1,1"); // col: 1, row: 1
    body.add(oggetto, "1,3");// col: 1(TableLayout.FILL), row: 3 (TableLayout.FILL)
    frame.add(body);
    frame.setVisible(true);
}

如要添加更多组件,请定义“ dim”,如以下示例所示:

double[][] dim = {
            {20, TableLayout.FILL, 20},
            {20, 17, 5, TableLayout.FILL /*label1,distance,component1*/, 17, 5, TableLayout.FILL/*label2,distance,component2 etc...*/, 20} 
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM