Search code examples
javaswingjframejpaneljcomponent

Frame showing up super small Java Swing


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!


Solution

  • 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 call setPreferredSize(...) 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:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.Border;
    
    public class MyWelcomeGui extends JPanel {
        private static final float LABEL_FONT_SIZE = 60.0f;
        private static final int PADDING = 60;
        private static final String TITLE = "Wecome To My GUI!";
        private static final Color BACKGROUND = new Color(51, 255, 189);
    
        MyWelcomeGui() {
            JLabel label = new JLabel(TITLE, SwingConstants.CENTER);
            label.setFont(getFont().deriveFont(Font.BOLD, LABEL_FONT_SIZE));
            Border border = BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING);
            label.setBorder(border);
            label.setForeground(Color.BLUE);
            setBackground(BACKGROUND);
            setLayout(new BorderLayout());
            add(label);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame window = new JFrame();
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    window.add(new MyWelcomeGui());
                    window.pack();
                    window.setLocationRelativeTo(null);
                    window.setVisible(true);
                }
            });
    }