GUI框架庞大,与我在java中的设计不符

时间:2013-12-05 01:50:10

标签: java swing netbeans

对不起蹩脚的头衔,想不出更好的方式来说出来。基本上,我正在学习java中的GUI并使用NetBeans,我遇到了一个我无法搜索的问题,而且我的google fu失败了。基本上,当我设计一个新窗口时,我可以准确地得到它我想要它

enter image description here

然后当我去运行它时,它会变得很大但它没有最大化,只是一个巨大的框架

enter image description here

我试过操纵听起来像没有运气的帮助的属性。有人可以提供一些建议吗?提前谢谢!

编辑:想要添加,这不是特定于这个程序,它是用我试图制作的任何GUI来做的,这让我相信它是Netbeans中的一些设置我无法找到答案。

这是GUI代码:

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    calculateButton = new javax.swing.JButton();
    exitButton = new javax.swing.JButton();
    customerTypeTxt = new javax.swing.JTextField();
    subtotalTxt = new javax.swing.JTextField();
    discountPercentTxt = new javax.swing.JTextField();
    discountAmountTxt = new javax.swing.JTextField();
    totalTxt = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Customer Type:");

    jLabel2.setText("Subtotal:");

    jLabel3.setText("Discount Percent:");

    jLabel4.setText("Discount Amount:");

    jLabel5.setText("Total:");

    calculateButton.setText("Calculate");
    calculateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            calculateButtonActionPerformed(evt);
        }
    });

    exitButton.setText("Exit");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitButtonActionPerformed(evt);
        }
    });

    discountPercentTxt.setEditable(false);
    discountPercentTxt.setFocusable(false);

    discountAmountTxt.setEditable(false);
    discountAmountTxt.setFocusable(false);

    totalTxt.setEditable(false);
    totalTxt.setFocusable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(discountAmountTxt)
                            .addComponent(totalTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(discountPercentTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addComponent(subtotalTxt)
                            .addComponent(customerTypeTxt))))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(exitButton)))
            .addGap(18, 18, 18))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(customerTypeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(subtotalTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(discountPercentTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(discountAmountTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(21, 21, 21)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(totalTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(calculateButton)
                .addComponent(exitButton))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>    

更多请求的代码:

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new InvoiceForm().setVisible(true);
        }
    });
}

2 个答案:

答案 0 :(得分:1)

如果我是你,我会忘记NetBeans设计师,我会用DesignGridLayout编写GUI代码;只需替换以下行(包含)之间的所有代码块:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

和以下行(不包括在内,必须保留):

pack();

使用以下代码块:

DesignGridlLayout layout = new DesignGridLayout(getContentPane());
layout.row().grid(jLabel1).add(customerTypeTxt);
layout.row().grid(jLabel2).add(subtotalTxt);
layout.row().grid(jLabel3).add(discountPercentTxt);
layout.row().grid(jLabel4).add(discountAmountTxt);
layout.row().grid(jLabel5).add(totalTxt);
layout.emptyRow();
layout.row().center().add(calculateButton, exitButton);

你应该得到你想要的UI,扩大应该按预期工作。

不要为DesignGridLayout搜索一个grphical设计器,你会发现没有,因为这是一个可以通过编码直接使用的布局管理器,并且使用它进行编码非常简单。

答案 1 :(得分:0)

您应该发布一些代码,但无论如何我都会采取刺激:在GUI代码的末尾,请调用:

pack();

...根据首选大小和组件布局调整框架大小。