简体   繁体   English

在Java中使用盒子布局

[英]using a box layout in java

the following codes created a box layout conviniently but the problem i have is the textfields occupy the entire rows. 下面的代码很方便地创建了一个框布局,但是我遇到的问题是文本字段占据了整行。 which is supposed to asume the parameter length in which it was specified. 应该假定它是在其中指定的参数长度。

 public void makeControlpanel(){
    JPanel controlpanel = new JPanel();
     //SET PANEL LAYOUT MANAGERS
    controlpanel.setLayout(new BoxLayout(controlpanel,BoxLayout.PAGE_AXIS));

    controlpanel.setBorder(BorderFactory.createTitledBorder("Create Control file")); 


    filenameC   = new JLabel("Filename");
    filenameBad   = new JLabel("Bad Filename");
    filenameDis = new JLabel("Discard Filename");

    // fields
    fileField = new JTextField(1);
    badfileField = new JTextField(7);
    discardfileField = new JTextField(7);

The layout manager decides the size of the components. 布局管理器决定组件的大小。 You have options to define the bounds of a component to the layout manager using 您可以使用以下选项来定义组件与布局管理器的界限:

comp.setMinimumSize(new Dimension(w, h));
comp.setPreferredSize(new Dimension(w, h));
comp.setMaximumSize(new Dimension(w, h));

When you give setPreferredSize layout manager will try to give that size. 当您提供setPreferredSize布局管理器将尝试提供该大小。 GridBagLayout is the msot flexible layout and you can prettymuch achieve any layout you need. GridBagLayout是msot灵活的布局,您几乎可以实现所需的任何布局。

The parameter length by defenition only defines the character you can put in the textfield. 参数length按定义)仅定义您可以在文本字段中输入的字符。

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

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