繁体   English   中英

NetBeans中相同的摆动代码的异常行为

[英]Unusual behavior for the same swing code in netbeans

我有两个几乎相似的代码。

代码一

   JFrame pFrame=new NetBeansFrame();
   JPanel myPanel=new myPanel();
   pFrame.add(myPanel);
   Dimension windowDim=myPanel.getSize();

   pFrame.pack();
   pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
   pFrame.setVisible(true);

守则二

   JFrame pFrame=new JFrame();
   JPanel myPanel=new myPanel();
   pFrame.add(myPanel);
   Dimension windowDim=myPanel.getSize();

   pFrame.pack();
   pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
   pFrame.setVisible(true);

在代码I中,NetBeansFrame是我使用netbeans-> Jframe创建的框架,并将其命名为NetBeansFrame。它不包含任何内容。我使用codeI将面板添加到其中。

NetBeansFrame.java

public class NetBeansFrame extends javax.swing.JFrame {


public NetBeansFrame() {
    initComponents();

}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 407, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 429, Short.MAX_VALUE)
    );

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

/**
 * @param args the command line arguments
 */
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(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.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 NetBeansFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
// End of variables declaration

}

在代码II中,我从JFrame创建一个框架。逻辑上这两个代码是等效的。

但是在执行代码I时,该面板不会出现在netbeansFrame中,而对于代码II,该面板会出现。

因此,我想知道对于几乎相同的代码,这种异常行为可能是什么原因?

  1. Dimension windowDim=myPanel.getSize(); 仅返回Dimension[0, 0] ,因为从返回了Dimension

    • 已经可见的容器(在您的情况下为组件JPanel

    • 在调用pack();

  2. 然后pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50); 返回的Dimension[-100, -50]

  3. 您可以

    • 如果JPanel为空,则返回其PreferredSize

    • 如果JPanel不为空,则其JComponents返回PreferredSize您可以尝试删除/禁用代码行pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);

暂无
暂无

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

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