I have two almost similar code.
Code 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);
In Code I,NetBeansFrame is a frame that I created using netbeans->Jframe and named it NetBeansFrame.It contains nothing.I added the panel into it using 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
}
In Code II,I am creating a frame from JFrame.Logically both the codes are equivalent.
But on executing Code I,the panel does not appear in netbeansFrame while for Code II the panel appears.
So,I want to know that what may be the cause for this unusual behavior for almost same code.
-
This is very hard to troubleshoot without the source for NetBeansFrame.Jacob is on Codidact– Jacob is on Codidact2012年07月13日 10:11:54 +00:00Commented Jul 13, 2012 at 10:11
-
1) For better help sooner, post an SSCCE. 2) See The Use of Multiple JFrames, Good/Bad Practice?Andrew Thompson– Andrew Thompson2012年07月13日 10:29:15 +00:00Commented Jul 13, 2012 at 10:29
-
@AndrewThompson:thnx for suggesting SSCCE link.. :)Abhinav– Abhinav2012年07月13日 10:39:23 +00:00Commented Jul 13, 2012 at 10:39
1 Answer 1
Dimension windowDim=myPanel.getSize();returned onlyDimension[0, 0], becasue returnedDimensionfromalready visible container (in your case with components
JPanel)after called
pack();
then
pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);returnedDimension[-100, -50]you can
if
JPanelis empty then returns itsPreferredSizeif
JPanelis not empty, then itsJComponentsreturnsPreferredSizeyou can to try that with to remove/disable code linepFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
2 Comments
JFrame pFrame=new NetBeansFrame(); JPanel myPanel=new myPanel(); pFrame.add(myPanel); pFrame.pack(); pFrame.setVisible(true); even though it does not come.myPanel. For better readability, edit your question to include new code.