import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JToolBarApp extends JPanel { private JLabel theLabel = new JLabel("", JLabel.CENTER); public JToolBarApp() { JToolBar tbar = new JToolBar(); setLayout(new BorderLayout()); add(tbar, BorderLayout.NORTH); add(theLabel, BorderLayout.CENTER); theLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 36)); addBtn(tbar, "Car1.GIF", "Blue"); addBtn(tbar, "Car2.GIF", "Red"); addBtn(tbar, "Car3.GIF", "Yellow"); addBtn(tbar, "Car4.GIF", "Green"); } public void addBtn(JToolBar tbar, String file, String color) { ImageIcon icon = new ImageIcon(file); JButton btn = new JButton(icon); btn.setToolTipText(color); final String op = "Start the " + color + " car."; btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { theLabel.setText(op); } }); tbar.add(btn); } public static void main(String[] args) { JToolBarApp ja = new JToolBarApp(); GApp app = new GApp("The JToolBar Application", ja); app.setSize(450, 250); app.show(); } }