could someone tell me why my frame is showing up super small/minimized? I thought I just had to pack my frame to resolve the issue but packing my frame does not seem to have any effect on the outcome.
small frame / frame not showing up properly
Here's my code:
package welcome.netbeans;
import java.awt.Container;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WelcomeNetbeans extends JFrame {
private JLabel welcomeText;
public static void main(String[] args) {
WelcomeNetbeans frame = new WelcomeNetbeans();
frame.start();
}
public void start() {
JPanel panel = new JPanel();
Container cont = getContentPane();
cont.add(panel);
setSize(300, 300);
setTitle("Welcome Netbeans!");
setLayout(new GridBagLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setFocusable(true);
welcomeText = new JLabel("Wecome Netbeans!");
welcomeText.setSize(100, 50);
panel.add(welcomeText);
pack();
setLocationRelativeTo(null); // center
setVisible(true);
}
}
Thank you so much!
You must understand that most layout managers do not respect a component's size, but rather its preferred size. And so calling
setSize(...)on a component usually won't help you. You could callsetPreferredSize(...)on the JLabel but doing so would still miss the point and make for rigid GUI's that might not work well. Myself, I often set the label's Font and sometimes give it an empty border, anything to avoid setting sizes and preferred sizes directly. Occasionally, I'll override a component's `getPreferredSize() method.For example: