Graphical Interfaces


Graphical User Interfaces (GUI) are visible menus you can interact with.

Purpose is for taking input from users or pop up info to them.

Graphic objects are found in the swing folder of the built in Java files.

A recommended way to build graphical interfaces is to use Netbeans, which builds these interfaces faster by using a visual editor that also automatically generates your code.


Making a window


Use the JFrame class to create a window.

Show this window by using the method setVisible(True).


JFrame frame = new JFrame("title"); //create frame with title
frame.setVisible(true); //show frame

Set its size by setSize(), or pack() to set it to whatever size its contents take. Example:

frame.setSize(100, 100); //size it 100 pixels width, 100 pixels height
frame.pack(); //pack it down to its contents


Adding components


This adds graphical objects into one another.

Every object in the swing folder is a Container, which means they can be contained in one another.


JFrame frame = new JFrame();
JLabel label = new JLabel("label"); //make label
frame.add(label); //add it to frame


Positioning components


Classes inheriting LayoutManager (from java.awt package) help you position component positions more precisely:

LayoutManager Description
AbsoluteLayout Absolute positioning of objects given by coordinates
BorderLayout (JFrame default) Separates container into 5 areas: center, north, west, south, and east
BoxLayout Bottom to top layout
CardLayout Creates layers of panels where each can be brought to front by an action
FlowLayout (JPanel default) Left to right layout
Group Layout Groups containers; designed for use by visual editors like NetBeans
GridLayout Arranges components in a grid, each same sized, with a fixed number of rows and columns
GridBagLayout Like Grid Layout, but columns can have different sizes, and one component can span multiple columns
null No layout; each object needs to set their own rectangular bounds to show up correctly
OverlayLayout Back to front layout (overlapping objects)

Use setLayout() method to change the layout.


JFrame frame = new JFrame(); //make window
frame.setLayout(new GridLayout()); //set to grid layout
List list = new ArrayList();
for (int i = 0; i < 5; i++) //make list of buttons
        list.add(new JButton("test"));
for (JButton button : list) //add buttons to window
frame.pack(); //auto-size window
frame.setVisible(true); //show window




ActionListeners are objects that listen for a trigger in a component and run a list of statements when it is triggered.

ActionListeners implement the ActionListener interface.

Each component has a unique trigger:

Component Trigger
JButton Clicked
JComboBox An option is clicked
JRadioButton Clicked
JCheckBox Clicked
JTextBox Pressed enter when focused
JToggleButton  Clicked

To make a customized ActionListener with your own statements to run, make a new class implementing it and write your statements in the inherited method actionPerformed(). Example:

class Listener implements ActionListener
     public void actionPerformed(ActionEvent event)
          //statements run when component is triggered

Then call this method from a component to add an ActionListener to a component.



class Listener implements ActionListener
     public void actionPerformed(ActionEvent event)
          System.out.println(“Triggered!”); //print something
class Tester
     public static void main(String[] args)
          JFrame frame = new JFrame(); //make window
          JButton button = new JButton(“click me”); //make button
          button.addActionListener(new Listener()); //add new listener
          frame.add(button); //add it to frame
Common Error: forgetting to add an ActionListenerActionListener-less objects do nothing when you click on them, so beware that you do not forget them.
Please like & share: