The default layout for a JFrame
is BorderLayout
which can only accept one component per layout constraint. The default when none is specified is CENTER
. So change:
frame.add(button);
frame.add(x);
To:
frame.add(button, BorderLayout.PAGE_START);
frame.add(x);
And you should see both components.
Other tips:
- Don't set the size of top level containers. Instead layout the content & call
pack()
. - Don't mix Swing & AWT without good reason. As the other poster mentioned, we'd typically use a
JPanel
for custom rendering in Swing. - The
BorderLayout
will ignore the size of the button and stretch it to fit. To have it stay a certain size, set a preferred size and add it to aFlowLayout
. Add theFlowLayout
to thePAGE_START
constraint.