我有两个几乎相似的代码。
代码I
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);
Code II
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
}
在Code II中,我正在从JFrame创建一个帧。逻辑上两个代码都是等价的。
但是在执行Code I时,面板不会出现在netbeansFrame中,而对于Code II,面板会出现。
所以,我想知道几乎相同的代码可能导致这种异常行为的原因。
答案 0 :(得分:4)
Dimension windowDim=myPanel.getSize();
仅返回Dimension[0, 0]
,因为从
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);