Link to home
Start Free TrialLog in
Avatar of ksfok
ksfok

asked on

Java window component bug

Given the below java code:

import java.awt.*;
import javax.swing.*;

public class WindowSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Windows Asgn");
        frame.setSize(400, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create container
        Container content = frame.getContentPane();

        //Add JButton
        JButton button = new JButton("JButton");
        content.add(button);

        //Add JLabel.
        JLabel label = new JLabel("Enter Your First Name Below:");
        content.add(label);

        //Add JtextField
        JTextField textFld = new JTextField();
        content.add(textFld);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        //frame.addWindowListener(new ExitListener());
          //frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

The window run is very small. None of the components show. Why? Please advise.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial