GroupLayout在NetBeans中自动生成代码 [英] GroupLayout autogenerated code in NetBeans

查看:116
本文介绍了GroupLayout在NetBeans中自动生成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java编程工作了近4年之后,我决定自己学习如何编写GUI类,因为到目前为止我一直使用NetBeans GUI编辑器(我并不为此感到特别自豪,但它确实很好地避免了我担心关于组件布局)。



事情是我正在关注:

  public static void main(String [] args){
JLabel label = new JLabel(这是一个测试);
label.setFont(new Font(Segoe UI Semibold,Font.BOLD | Font.ITALIC,24));

JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);

DefaultListModel model = new DefaultListModel();
model.addElement(Apple);
model.addElement(Orange);
model.addElement(Kiwi);
model.addElement(西瓜);

JList list = new JList(model);
list.setPreferredSize(new Dimension(400,300));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(list);

JFrame frame = new JFrame(Test);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


容器contentPane = frame.getContentPane();
GroupLayout layout = new GroupLayout(contentPane);
layout.setAutoCreateContainerGaps(true);
contentPane.setLayout(layout);

layout.setHorizo​​ntalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(标签,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
.addComponent(separator)
.addComponent(scrollPane)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(标签)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(分离器,的GroupLayout。 DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(滚动面板,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE,Short.MAX_VALUE)
)的
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

如你所见,我只将一个并列组定义为水平组和顺序组作为垂直组。但Netbeans会自动生成此代码:

  layout.setHorizo​​ntalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment。 LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
。 addComponent(标签,javax.swing.GroupLayout.DEFAULT_SIZE,300,Short.MAX_VALUE)
.addComponent(隔膜)
.addComponent(滚动面板))
.addContainerGap(javax.swing.GroupLayout。 DEFAULT_SIZE,Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(标签)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(分离器,javax.swing.GroupLayout.PREFERRED_SIZE,10,javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(滚动面板,javax.swing.GroupLayout.DEFAULT_SIZE,224,Short.MAX_VALUE)
.addContainerGap())
);

正如您所看到的,组结构比我的更复杂。我只是想知道我是否错了,或者Netbeans只是不必要地添加了超过需要的组。

解决方案

赞赏NetBeans的荣誉GUI设计师作为一种手段 - 而不是替代 - 理解Swing。总结评论,




  • 虽然 GroupLayout 是为自动代码生成而设计的,它可以有效地手动使用,如此处此处。它也可以集成到此处建议的混合开发方法中。


  • 有经验的开发人员明智地建议学习一个或多个流行的第三方布局,例如 MigLayout FormLayout DesignGridLayout ,从接受人类可读的文本参数中获得一些力量。我在同一类别中看到 GroupLayout ,但只是有一个流畅的界面


  • 在您的示例中,两个布局具有不同的调整大小行为,这可能会影响其他选择。谨防这种常见的陷阱



After almost 4 years in java programming I decided to learn how to write GUI classes by myself since until now I've always used NetBeans GUI Editor (I'm not particularly proud of it but it has worked pretty well avoiding me worry about the components layout).

The thing is I'm following How to Use GroupLayout tutorial to learn about this layout manager that I find very powerful. Now I made a little example by my own and then try to do the same in Netbeans GUI Editor and I found some differences between both codes and I'd like to know if I'm missing something or NetBeans just adds useless code in GroupLayout definition.

This is my target:

This is my SSCCE:

public static void main(String[] args) {        
    JLabel label = new JLabel("This is a test");
    label.setFont(new Font("Segoe UI Semibold", Font.BOLD | Font.ITALIC, 24));

    JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);

    DefaultListModel model = new DefaultListModel();
    model.addElement("Apple");
    model.addElement("Orange");
    model.addElement("Kiwi");
    model.addElement("Watermelon");

    JList list = new JList(model);
    list.setPreferredSize(new Dimension(400, 300));
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(list);

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    Container contentPane = frame.getContentPane();        
    GroupLayout layout = new GroupLayout(contentPane);
    layout.setAutoCreateContainerGaps(true);
    contentPane.setLayout(layout);

    layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                .addComponent(separator)
                .addComponent(scrollPane)
            );        
    layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addComponent(label)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(separator, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)               
        );        
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

As you can see I have only defined a paralell group as horizontal group and a sequential group as vertical group. But Netbeans autogenerate this code:

    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                .addComponent(separator)
                .addComponent(scrollPane))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(label)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
            .addContainerGap())
    );

As you can see the group structure is a little more complex than mine. I just want to know if I'm mistaken or Netbeans just unnecessarily adds more groups than needed.

解决方案

Kudos for embracing the NetBeans GUI designer as a means to—and not a substitute for—understanding Swing. Summarizing the comments,

  • While GroupLayout was designed for automated code generation, it can usefully be used manually, as shown here and here. It can also be integrated into the mixed development approach suggested here.

  • Experienced developers wisely suggest learning one or more popular third-party layouts such as MigLayout, FormLayout or DesignGridLayout, which derive some power from accepting human-readable text parameters. I see GroupLayout in the same category, but simply having a fluent interface.

  • In your example, the two layouts have differing resize behavior, which may impact other choices. Beware of this common pitfall.

这篇关于GroupLayout在NetBeans中自动生成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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