NetBeans中相同摇摆代码的不寻常行为
问题描述:
我有两个几乎相似的代码。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);
代码我,NetBeansFrame是我创建的使用netbeans->的JFrame和框架把它命名为NetBeansFrame.It包含什么。我使用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.Logically帧两个码是等同的。
但是在执行代码I时,面板不出现在netbeansFrame中,而代码II出现面板。
所以,我想知道,对于几乎相同的代码,这种不寻常行为的原因可能是什么。
答
-
Dimension windowDim=myPanel.getSize();
只返回Dimension[0, 0]
,becasue返回Dimension
从已经可见容器(在你的情况与组件
JPanel
)称为后
pack();
然后
pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
返回Dimension[-100, -50]
-
可以
如果
JPanel
为空,然后返回其PreferredSize
如果
JPanel
不为空,则其JComponents
回报PreferredSize
你可以尝试用删除/禁用代码行pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
如果没有NetBeansFrame源代码,这很难排除故障。 – 2012-07-13 10:11:54
1)为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 2)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/a/9554657/418556) – 2012-07-13 10:29:15
@AndrewThompson:thnx建议SSCCE链接.. :) – Abhinav 2012-07-13 10:39:23