1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  
  5  public class ActionInterfaceDemo extends JApplet {
  6    private JPanel buttonPanel = new JPanel();
  7    private FlowLayout flowLayout = new FlowLayout();
  8  
  9    public ActionInterfaceDemo() {
 10      // Create image icons
 11      ImageIcon leftImageIcon = new ImageIcon(getClass().getResource(
 12        "/image/leftAlignment.png"));
 13      ImageIcon centerImageIcon = new ImageIcon(getClass().getResource(
 14        "/image/centerAlignment.png"));
 15      ImageIcon rightImageIcon = new ImageIcon(getClass().getResource(
 16        "/image/rightAlignment.png"));
 17  
 18      // Create actions
 19      Action leftAction = new MyAction("Left", leftImageIcon,
 20        "Left alignment for the buttons in the panel",
 21        new Integer(KeyEvent.VK_L),
 22        KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
 23      Action centerAction = new MyAction("Center", centerImageIcon,
 24        "Center alignment for the buttons in the panel",
 25        new Integer(KeyEvent.VK_C),
 26        KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
 27      Action rightAction = new MyAction("Right", rightImageIcon,
 28        "Right alignment for the buttons in the panel",
 29        new Integer(KeyEvent.VK_R),
 30        KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
 31  
 32      // Create menus
 33      JMenuBar jMenuBar1 = new JMenuBar();
 34      JMenu jmenuAlignment = new JMenu("Alignment");
 35      setJMenuBar(jMenuBar1);
 36      jMenuBar1.add(jmenuAlignment);
 37  
 38      // Add actions to the menu
 39      jmenuAlignment.add(leftAction);
 40      jmenuAlignment.add(centerAction);
 41      jmenuAlignment.add(rightAction);
 42  
 43      // Add actions to the toolbar
 44      JToolBar jToolBar1 = new JToolBar(JToolBar.VERTICAL);
 45      jToolBar1.setBorder(BorderFactory.createLineBorder(Color.red));
 46      jToolBar1.add(leftAction);
 47      jToolBar1.add(centerAction);
 48      jToolBar1.add(rightAction);
 49  
 50      // Add buttons to the button panel
 51      buttonPanel.setLayout(flowLayout);
 52      JButton jbtLeft = new JButton(leftAction);
 53      JButton jbtCenter = new JButton(centerAction);
 54      JButton jbtRight = new JButton(rightAction);
 55      buttonPanel.add(jbtLeft);
 56      buttonPanel.add(jbtCenter);
 57      buttonPanel.add(jbtRight);
 58  
 59      // Add tool bar to the east and panel to the center
 60      add(jToolBar1, BorderLayout.EAST);
 61      add(buttonPanel, BorderLayout.CENTER);
 62    }
 63  
 64    private class MyAction extends AbstractAction {
 65      String name;
 66  
 67      MyAction(String name, Icon icon) {
 68        super(name, icon);
 69        this.name = name;
 70      }
 71  
 72      MyAction(String name, Icon icon, String desc, Integer mnemonic,
 73          KeyStroke accelerator) {
 74        super(name, icon);
 75        putValue(Action.SHORT_DESCRIPTION, desc);
 76        putValue(Action.MNEMONIC_KEY, mnemonic);
 77        putValue(Action.ACCELERATOR_KEY, accelerator);
 78        this.name = name;
 79      }
 80  
 81      @Override
 82      public void actionPerformed(ActionEvent e) {
 83        if (name.equals("Left"))
 84          flowLayout.setAlignment(FlowLayout.LEFT);
 85        else if (name.equals("Center"))
 86          flowLayout.setAlignment(FlowLayout.CENTER);
 87        else if (name.equals("Right"))
 88          flowLayout.setAlignment(FlowLayout.RIGHT);
 89  
 90        buttonPanel.revalidate();
 91      }
 92    }
 93  
 94    public static void main(String[] args) {
 95      ActionInterfaceDemo applet = new ActionInterfaceDemo();
 96      JFrame frame = new JFrame();
 97      //EXIT_ON_CLOSE == 3
 98      frame.setDefaultCloseOperation(3);
 99      frame.setTitle("ActionInterfaceDemo");
100      frame.getContentPane().add(applet, BorderLayout.CENTER);
101      applet.init();
102      applet.start();
103      frame.setSize(400,320);
104      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
105      frame.setLocation((d.width - frame.getSize().width) / 2,
106        (d.height - frame.getSize().height) / 2);
107      frame.setVisible(true);
108    }
109  }