应对Netbeans生成的GridBagLayout并修改生成的代码以获得我无法让Netbeans给我的东西

时间:2014-01-04 01:42:01

标签: netbeans gridbaglayout

我毫不怀疑,只是分享了3天的挫折和最终的成功。

enter image description here

以上是我使用其GridBagLayout模式从Netbeans获得的内容。以下是我想要的。

enter image description here

我无法在合理的时间内使用Netbeans获得我想要的内容,因此我认为我可以剪切,粘贴和修改生成的代码,使表单看起来像我想要的那样。

我是对的,花在获得我想要的东西上的时间很少,使用Netbeans"大纲"我昨晚很晚才开始。

这是我的代码:

public class DoThis extends JFrame {

... (variable declarations removed)

  public DoThis() {
    initComponents();
  }

  private void initComponents() {

    GridBagConstraints gridBagConstraints;
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridBagLayout());

    pnlFileStuff        = new JPanel();
    pnlFileStuff.setBorder(BorderFactory.createEtchedBorder());
    pnlFileStuff.setLayout(new GridBagLayout());

    lblRootNode         = new JLabel("Root node:");
    gridBagConstraints  = new GridBagConstraints();
    pnlFileStuff.add(lblRootNode, gridBagConstraints);

    txtRootNode         = new JTextField("C:\\Users");
    gridBagConstraints  = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.ipadx = 520;  // One key
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    pnlFileStuff.add(txtRootNode, gridBagConstraints);

    btnBrowse           = new JButton("Browse...");
    gridBagConstraints  = new GridBagConstraints();
    pnlFileStuff.add(btnBrowse, gridBagConstraints);

    lblFilenamePattern  = new JLabel("Filename pattern:");
    gridBagConstraints  = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    pnlFileStuff.add(lblFilenamePattern, gridBagConstraints);

    txtFilenamePattern  = new JTextField("*.*");
    gridBagConstraints  = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.ipadx = 250;  // the other key
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    pnlFileStuff.add(txtFilenamePattern, gridBagConstraints);

    getContentPane().add(pnlFileStuff, new GridBagConstraints());

    pack();
  }

  public static void main(String args[]) {
    invokeLater(new Runnable() {
      public void run() {
        new DoThis().setVisible(true);
      }
    });
  }              
}

事实证明,我只需要在Netbeans中再做一件事 - 只有ipadx个实例标记为" key"在代码中。有点尴尬承认。我没有必要!

请将其称为学习经验,我决定分享,无论好坏。我认为一些新手可能会从这篇文章中获益。

"教训" "了解到":

(1)在#B; GridBagLayout模式"中使用Netbeans很难。距离WYSIWYG太远,而且远非直观。 (这是一个从未使用过GridBayLayout并且几天前从未读过它的人。)正如S.O.提出的一个链接。我跟着说过," 不幸的是,GridBagLayout在使用时非常尴尬且容易出错。"这里没有争论。

(2)花费足够的时间与Netbeans斗争是值得的,因为没有它,几乎不可能阅读教科书和教程并获得任何接近期望结果(YMMV)。

(3) Netbeans在" GridBagLayout模式下生成的代码"与通常难以理解的数百行 krazy kode (它在"自由设计"模式中生成)相比,它更接近于人类编写的代码,这实际上是不可能的遵循,更不用说剪切,粘贴和编辑(尽管我这样做的成功率很低)。 (再也不会了。)

(4)生成的GridBagLayout代码相当容易剪切,粘贴和编辑,以生成所需的结果(假设它开始时处于球场,并且忽略了它的怪癖的充足挫折)。

最重要的是(对于我来说),我终于从Netbeans中解脱出来,可能正在开发一些从头开始编写GUI代码的技能,这是我几个月来一直避免的瘟疫!

1 个答案:

答案 0 :(得分:1)

我不应该重新审视这个主题并说明如何在没有Netbeans GUI构建器的情况下学习如何使用GridBagLayout可以通过 任何人 来实现。在不到一周的时间里学习并从SO获得帮助和建议,我终于创建了一个GUI(与Netbeans生成的不同),它易于编辑和扩展 - 因为我手工编写了代码。我一直在使用的GUI和几个月的问题(例如,如何防止显示在执行期间闪烁,导致SwingWorker和其他复杂功能)已经被下面的一个取代,我在一个晚上创建并在第二天(今天)“完善”: enter image description here

我不吹牛;离得很远。我几乎不知道蹲下。就像“完美”意味着“得到输出,它不会闪现,它是可读的,但它像罪一样丑陋。”

希望为其他新手提供希望。