简体   繁体   English

添加jpanel后的java boxlayout空间

[英]java boxlayout space after adding jpanel

I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. 我有一个容器jpanel,在其中使用了boxlayout管理器,我要做的是在其中添加另一个面板,其中使用flowlayout管理器在添加的面板中包含标签和文本字段。 everytime i add a panel inside it creates an annoying big space after another added panel. 每次我在其中添加一个面板时,都会在另一个添加的面板之后创建一个烦人的大空间。 I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. 我想减少使用setsize和setpreferredsize方法调整的面板的间距。 Here is my code: 这是我的代码:

  JPanel global = new JPanel();
  global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
  global.setPreferredSize(new Dimension(500,312));
  global.setSize(500,312);
  global.setBounds(8, 5, 500, 312);
  global.setBorder(BorderFactory.createLineBorder(Color.black));
  global.setBackground(Color.white);
  //Elements of global

  JLabel label1 = new JLabel("Global Settings");
  label1.setAlignmentX(Component.CENTER_ALIGNMENT);
  label1.setFont(new Font("tahoma", Font.BOLD, 17));
  global.add(label1);
  global.add(new JSeparator());


  //Name Field
  JPanel c = new JPanel();
  c.setSize(100, 1);
  c.setPreferredSize(new Dimension(100,1));
  c.setLayout(new FlowLayout());
  JLabel label = new JLabel("Display Name");
  JTextField text = new JTextField(20);
  text.setPreferredSize(new Dimension(20,25));
  c.add(label);
  c.add(text);
  global.add(c);

  //Hostname Field
  JPanel c1 = new JPanel();
  c1.setSize(100, 1);
  c1.setPreferredSize(new Dimension(100,1));
  c1.setLayout(new FlowLayout());
  JLabel label2 = new JLabel("Host Name");
  JTextField text1 = new JTextField(20);
  text1.setPreferredSize(new Dimension(20,25));
  c1.add(label2);
  c1.add(text1);
  global.add(c1);

BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. BoxLayout是一个非常激进的LayoutManager ,并不总是遵循其中的组件的首选大小。 Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. 相反,我们必须设置BoxLayout组件的最大大小,以防止它们被拉伸。 Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components). 此外,我们还需要添加Box通过Box.createVerticalGlue() -这是被拉长特殊部件(而非其他部件)。

Here is the rewritten code: 这是重写的代码:

    JPanel global = new JPanel();
    global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
    global.setPreferredSize(new Dimension(500, 312));
    global.setSize(500, 312);
    global.setBounds(8, 5, 500, 312);
    global.setBorder(BorderFactory.createLineBorder(Color.black));
    global.setBackground(Color.white);
    // Elements of global

    JLabel label1 = new JLabel("Global Settings");
    label1.setAlignmentX(Component.CENTER_ALIGNMENT);
    label1.setFont(new Font("tahoma", Font.BOLD, 17));
    global.add(label1);
    JSeparator sep = new JSeparator();
    sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
    global.add(sep);

    // Name Field
    JPanel c = new JPanel();
    c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
    JLabel label = new JLabel("Display Name");
    JTextField text = new JTextField(20);
    text.setPreferredSize(new Dimension(20, 25));
    c.add(label);
    c.add(text);
    global.add(c);

    // Hostname Field
    JPanel c1 = new JPanel();
    c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
    JLabel label2 = new JLabel("Host Name");
    JTextField text1 = new JTextField(20);
    text1.setPreferredSize(new Dimension(20, 25));
    c1.add(label2);
    c1.add(text1);
    global.add(c1);
    global.add(Box.createVerticalGlue());

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

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