Creating Buttons with java.awt.Button Class

This section provides a tutorial example on how to create a button with the java.awt.Button class.

Buttons are so easy to create with the java.awt.Button class. Here is a sample program:

/* ButtonOk.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.Frame;
import java.awt.Button;
public class ButtonOk {
   public static void main(String[] a) {
      Frame f = new Frame("My OK Button");
      Button b = new Button("OK");
      f.add(b);
      f.pack();      
      f.setVisible(true);
   }
}

If you run this example, you will get:

AWT Button Test
AWT Button Test

Note: Clicking the OK button will trigger no actions, because no event listeners are added to the button component.

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

AWT (Abstract Windows Toolkit)

 What Is AWT (Abstract Windows Toolkit)

 HelloAWT.java - My First AWT Program

 Closing AWT Frame and Terminating Application

 Drawing Graphics - Using paint() on Frame or Component

 Creating Labels with java.awt.Label Class

Creating Buttons with java.awt.Button Class

 AWT Button Action Handler at the Component Level

 AWT Button Action Handler at the Frame Level

 AWT Button Mouse Click Handler at the Frame Level

 AWT TextField and ActionListener

 MenuBarTest.java - AWT Menu Bar Test Program

 MenuTest.java - AWT Menu Test Program

 MenuItemTest.java - AWT Menu Item Test Program

 MenuItemActionListenerTest.java - AWT Menu Item Action Listener

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB