简体   繁体   English

如何将多个 JTextfields 放在 BorderLayout 中心的不同行上?

[英]How to put multiple JTextfields on different lines in the center of BorderLayout?

So firstly, I added the text fields and their labels into separate panels as I wanted them to be in line with each other, I added them via Flow layout;因此,首先,我将文本字段及其标签添加到单独的面板中,因为我希望它们彼此一致,我通过 Flow 布局添加它们;

        //add them in Flow Layout
        panel.add(label_1);panel.add(file_name);
        pane2.add(label_2);pane2.add(h_link);
        pane3.add(label_3);pane3.add(pages);

What i wanted to do next is to add them using BorderLayout in a way that all of them will be in the center of the window.我接下来想做的是使用 BorderLayout 添加它们,使它们都位于窗口的中心。 Additionally, I wanted to add submit button of the details;另外,我想添加详细信息的提交按钮;

        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.CENTER, pane2);
        frame.getContentPane().add(BorderLayout.CENTER, pane3);
        frame.getContentPane().add(BorderLayout.SOUTH, submit);
        frame.setVisible(true);

As I am writing this post, I did not achived the result that i wanted , the screenshot attached shows the resulting frame which is not I wanted.在我写这篇文章时,我没有达到我想要的结果,所附的屏幕截图显示了我不想要的结果帧。 By all means, if you have also any suggestions for how to do it more efficiently please do share.无论如何,如果您对如何更有效地做到这一点也有任何建议,请分享。

在此处输入图片说明

The key to solving this is to nest JPanels , each using its own layout, thereby allowing you to effectively nest layouts:解决这个问题的关键是嵌套JPanels ,每个使用自己的布局,从而允许你有效地嵌套布局:

  • Place a JPanel in the BorderLayout.CENTER positionBorderLayout.CENTER position放置一个JPanel
  • Give the JPanel a GridLayout , one allowing multiple rows and one column, eg, new GridLayout(0, 1)JPanel一个GridLayout ,一个允许多行和一列,例如, new GridLayout(0, 1)
  • Add your JTextField s to the JPanel将您的JTextField添加到JPanel

Another approach is to simply use a JTable, one with a single column and multiple rows.另一种方法是简单地使用一个 JTable,一个单列多行。


Another approach -- if you're trying to gain input from a user in a JLabel/JTextField grid -- is to use GridBagLayout to space the components nicely together另一种方法——如果你试图在 JLabel/JTextField 网格中从用户那里获得输入——是使用 GridBagLayout 将组件很好地间隔在一起

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

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