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

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

问题描述

在 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 分隔符 = new JSeparator(JSeparator.HORIZONTAL);DefaultListModel 模型 = 新的 DefaultListModel();model.addElement("苹果");model.addElement("橙色");model.addElement("猕猴桃");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(分隔符).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(scrollPane, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE));框架.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(scrollPane)).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(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE).addContainerGap()));

如您所见,组结构比我的要复杂一些.我只想知道是我弄错了还是 Netbeans 只是不必要地添加了比需要更多的组.

解决方案

感谢您接受 NetBeans GUI 设计器作为理解 Swing 的一种手段(而不是替代).总结评论,

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

  • 有经验的开发人员明智地建议学习一种或多种流行的第三方布局,例如 MigLayoutFormLayoutDesignGridLayout,它从接受人类可读的文本参数中获得了一些力量.我在同一类别中看到 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.

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

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