Site Sponsors:
Order Of Operations 
When we set our layout *after* we have added our components, we do not need to use any of the size-oriented (setSize(), setPreferredSize(), setMaximumSize(), setMinimumSize()) operations:


import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
* Use the content of a panel for the main form. Application ends when a main form
* is closed.
*
* @author profnagy
*/
public class MainFrame extends JFrame implements Runnable {

public Thread pwThread = null;

public MainFrame(JPanel jPanel) {
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
if (jPanel == null) {
JLabel lbl = new JLabel();
lbl.setText(this.getClass().getName());
this.add(lbl);
} else {
this.add(jPanel); // do this first -
}
this.setLayout(new CardLayout()); // the layout (queries all children), afterwards
this.pack();
com.soft9000.gui.Misc.ScreenCenter(this);
}

public void start() {
pwThread = new Thread(this);
pwThread.start();
this.setVisible(true);
}

public void run() {
}
}


So if you ever see that your window size does not work, check to see when we have applied our layout manager. In general, simply remember to assign the layout manager just before calling pack().

Enjoy,

-Rn

Comments

Add Comment
Comments are not available for this entry.