Toolbars.java Source code

Java tutorial

Introduction

Here is the source code for Toolbars.java

Source

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Toolbars {
    public static void main(String[] args) {
        JToolBar toolbar1 = new JToolBar();
        JToolBar toolbar2 = new JToolBar();

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JButton newb = new JButton(new ImageIcon("new.png"));
        JButton openb = new JButton(new ImageIcon("open.png"));
        JButton saveb = new JButton(new ImageIcon("save.png"));

        toolbar1.add(newb);
        toolbar1.add(openb);
        toolbar1.add(saveb);
        toolbar1.setAlignmentX(0);

        JButton exitb = new JButton(new ImageIcon("exit.png"));
        toolbar2.add(exitb);
        toolbar2.setAlignmentX(0);

        exitb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        panel.add(toolbar1);
        panel.add(toolbar2);

        JFrame f = new JFrame();
        f.add(panel, BorderLayout.NORTH);

        f.setSize(300, 200);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}