A Simple AWT Application

You probably have some programs lying around that use regular AWT buttons that you’d love to replace with image buttons, but you don’t have the time or, honestly, the necessity to produce your own image button class. Let’s look at a simple application that demonstrates an upgrade path you can use on your own programs.

First, let’s look at the code for this very simple application:

// ToolbarFrame1.java // A simple frame containing a "toolbar" made up of several java.awt.Button // objects. We'll be converting the Buttons to JButtons in the ToolbarFrame2.java // file. // import java.awt.*; import java.awt.event.*; public class ToolbarFrame1 extends Frame { Button cutButton, copyButton, pasteButton; public ToolbarFrame1( ) { super("Toolbar Example (AWT)"); setSize(450, 250); addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { System.exit(0); } }); ActionListener printListener = new ActionListener( ) { public void actionPerformed(ActionEvent ae) { System.out.println(ae.getActionCommand( )); } }; Panel toolbar = new Panel( ); toolbar.setLayout(new FlowLayout(FlowLayout.LEFT)); cutButton = new Button("Cut"); cutButton.addActionListener(printListener); toolbar.add(cutButton); copyButton = new Button("Copy"); copyButton.addActionListener(printListener); toolbar.add(copyButton); pasteButton = new Button("Paste"); pasteButton.addActionListener(printListener); toolbar.add(pasteButton); // The "preferred" BorderLayout add call add(toolbar, ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.