This program adds a single button which does nothing when you press it:
...
import javax.swing.*;
import java.awt.*;
...
public void run()
{
JFrame frame = new JFrame();
frame.setTitle("One Button");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JButton button = new JButton("Do Nothing");
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
...
Here are some notes about the program
as with most graphics programs, the old graphics library java.awt.* has to be imported as well as the new library javax.swing.*
(javax was originally for extensions to Java, it has become part of
standard Java now, but not renamed because of compatibility with all
the code which was written using javax)
the frame is a Container i.e. a graphics component whose main task is to contain other graphics components
once a new component like a button is created, it has to be added to a container to make it appear
the frame is sized with pack instead of setSize
to size it automatically from the natural size of the button
the setLocationByPlatform method neds to be called after pack so it knows the frame's size, but before setVisible so that it affects placement
in general, the layout of a graphics interface should be achieved without once mentioning actual coordinates, because that would be clumsy and platform dependent