0

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

代码一

   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 会出现面板。

所以,我想知道几乎相同的代码出现这种异常行为的原因可能是什么。

4

1 回答 1

3
  1. Dimension windowDim=myPanel.getSize();只返回Dimension[0, 0],因为Dimension

    • 已经可见的容器(在您的情况下为 components 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);

于 2012-07-13T10:12:51.860 回答