Only one component shows up in JFrame

The content pane of a JFrame has a BorderLayout. If you place a component in a BL with no constraints it ends up in the CENTER. The center can only display one component.

For an immediate effect, I suggest:

f.add(top, BorderLayout.PAGE_START);
f.add(mid);
f.add(bot, BorderLayout.PAGE_END);

Other points.

  1. Take out f.setSize(500, 500); and call pack() immediately before setVisible(true)
  2. For a better way to end the GUI, change f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  3. in.setVisible(true); Except for the frame itself, take these out. A component automatically becomes visible when it is added to a top level container and that container is itself made visible.
  4. Change
    public class EncDecExample extends JFrame
    to
    public class EncDecExample
    This code keeps a reference to a frame, and that is the right way to go.

Leave a Comment