Java window component bug

Posted on 2009-04-15
Last Modified: 2013-11-23
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);

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

        //Add JButton
        JButton button = new JButton("JButton");

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

        //Add JtextField
        JTextField textFld = new JTextField();

        //Display the window.
        //frame.addWindowListener(new ExitListener());

    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() {

The window run is very small. None of the components show. Why? Please advise.
Question by:ksfok
    1 Comment
    LVL 92

    Accepted Solution

    because default layout is BorderLayout so each component you add replaces the previous
    either use a different layout manager, or specify different borderlayout *areas* for each

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Java MSI Solution 3 52
    MaxSpan challenge 9 51
    linearIn  challenge 23 54
    java set up 1 37
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now