Java by API/java.awt.event/ActionEvent

Материал из Java эксперт
Перейти к: навигация, поиск

ActionEvent.ALT_MASK

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame();
   Container contentPane = frame.getContentPane();
   ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Command: " + e.getActionCommand());
       int modifiers = e.getModifiers();
       System.out.println("\tALT : "
           + checkMod(modifiers, ActionEvent.ALT_MASK));
       System.out.println("\tCTRL : "
           + checkMod(modifiers, ActionEvent.CTRL_MASK));
       System.out.println("\tMETA : "
           + checkMod(modifiers, ActionEvent.META_MASK));
       System.out.println("\tSHIFT: "
           + checkMod(modifiers, ActionEvent.SHIFT_MASK));
       Object source = e.getSource();
       if (source instanceof JComboBox) {
         JComboBox jb = (JComboBox) source;
         System.out.println("Combo: " + jb.getSelectedItem());
       }
     }
     private boolean checkMod(int modifiers, int mask) {
       return ((modifiers & mask) == mask);
     }
   };
   String flavors[] = { "Item 1", "Item 2", "Item 3"};
   JComboBox jc = new JComboBox(flavors);
   jc.setMaximumRowCount(4);
   jc.setEditable(true);
   jc.addActionListener(listener);
   contentPane.add(jc, BorderLayout.NORTH);
   JButton b = new JButton("Button!");
   b.addActionListener(listener);
   contentPane.add(b, BorderLayout.CENTER);
   JPanel panel = new JPanel();
   JLabel label = new JLabel("Label 1: ");
   JTextField text = new JTextField("Type your text", 15);
   text.addActionListener(listener);
   label.setDisplayedMnemonic(KeyEvent.VK_1);
   label.setLabelFor(text);
   panel.add(label);
   panel.add(text);
   contentPane.add(panel, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



ActionEvent.CTRL_MASK

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame();
   Container contentPane = frame.getContentPane();
   ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Command: " + e.getActionCommand());
       int modifiers = e.getModifiers();
       System.out.println("\tALT : "
           + checkMod(modifiers, ActionEvent.ALT_MASK));
       System.out.println("\tCTRL : "
           + checkMod(modifiers, ActionEvent.CTRL_MASK));
       System.out.println("\tMETA : "
           + checkMod(modifiers, ActionEvent.META_MASK));
       System.out.println("\tSHIFT: "
           + checkMod(modifiers, ActionEvent.SHIFT_MASK));
       Object source = e.getSource();
       if (source instanceof JComboBox) {
         JComboBox jb = (JComboBox) source;
         System.out.println("Combo: " + jb.getSelectedItem());
       }
     }
     private boolean checkMod(int modifiers, int mask) {
       return ((modifiers & mask) == mask);
     }
   };
   String flavors[] = { "Item 1", "Item 2", "Item 3"};
   JComboBox jc = new JComboBox(flavors);
   jc.setMaximumRowCount(4);
   jc.setEditable(true);
   jc.addActionListener(listener);
   contentPane.add(jc, BorderLayout.NORTH);
   JButton b = new JButton("Button!");
   b.addActionListener(listener);
   contentPane.add(b, BorderLayout.CENTER);
   JPanel panel = new JPanel();
   JLabel label = new JLabel("Label 1: ");
   JTextField text = new JTextField("Type your text", 15);
   text.addActionListener(listener);
   label.setDisplayedMnemonic(KeyEvent.VK_1);
   label.setLabelFor(text);
   panel.add(label);
   panel.add(text);
   contentPane.add(panel, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



ActionEvent: getActionCommand()

   <source lang="java">

import java.awt.BorderLayout; import java.awt.ItemSelectable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class MainClass {

 public static void main(final String args[]) {
   final String labels[] = { "A", "B", "C", "D", "E" };
   JFrame frame = new JFrame("Selecting JComboBox");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JComboBox comboBox = new JComboBox(labels);
   frame.add(comboBox, BorderLayout.SOUTH);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       System.out.println("Command: " + actionEvent.getActionCommand());
       ItemSelectable is = (ItemSelectable)actionEvent.getSource();
       System.out.println(", Selected: " + selectedString(is));
     }
   };
   comboBox.addActionListener(actionListener);
   frame.setSize(400, 200);
   frame.setVisible(true);
 }
 static private String selectedString(ItemSelectable is) {
   Object selected[] = is.getSelectedObjects();
   return ((selected.length == 0) ? "null" : (String)selected[0]);
 }  

}


 </source>
   
  
 
  



ActionEvent: getModifiers()

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame();
   Container contentPane = frame.getContentPane();
   ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Command: " + e.getActionCommand());
       int modifiers = e.getModifiers();
       System.out.println("\tALT : "
           + checkMod(modifiers, ActionEvent.ALT_MASK));
       System.out.println("\tCTRL : "
           + checkMod(modifiers, ActionEvent.CTRL_MASK));
       System.out.println("\tMETA : "
           + checkMod(modifiers, ActionEvent.META_MASK));
       System.out.println("\tSHIFT: "
           + checkMod(modifiers, ActionEvent.SHIFT_MASK));
       Object source = e.getSource();
       if (source instanceof JComboBox) {
         JComboBox jb = (JComboBox) source;
         System.out.println("Combo: " + jb.getSelectedItem());
       }
     }
     private boolean checkMod(int modifiers, int mask) {
       return ((modifiers & mask) == mask);
     }
   };
   String flavors[] = { "Item 1", "Item 2", "Item 3"};
   JComboBox jc = new JComboBox(flavors);
   jc.setMaximumRowCount(4);
   jc.setEditable(true);
   jc.addActionListener(listener);
   contentPane.add(jc, BorderLayout.NORTH);
   JButton b = new JButton("Button!");
   b.addActionListener(listener);
   contentPane.add(b, BorderLayout.CENTER);
   JPanel panel = new JPanel();
   JLabel label = new JLabel("Label 1: ");
   JTextField text = new JTextField("Type your text", 15);
   text.addActionListener(listener);
   label.setDisplayedMnemonic(KeyEvent.VK_1);
   label.setLabelFor(text);
   panel.add(label);
   panel.add(text);
   contentPane.add(panel, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



ActionEvent: getSource()

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Button Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   final JButton button1 = new JButton("Select Me");
   final JButton button2 = new JButton("No Select Me");
   final Random random = new Random();
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       JButton button = (JButton) actionEvent.getSource();
       int red = random.nextInt(255);
       int green = random.nextInt(255);
       int blue = random.nextInt(255);
       button.setBackground(new Color(red, green, blue));
     }
   };
   PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
     public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
       String property = propertyChangeEvent.getPropertyName();
       if ("background".equals(property)) {
         button2.setBackground((Color) propertyChangeEvent.getNewValue());
       }
     }
   };
   button1.addActionListener(actionListener);
   button1.addPropertyChangeListener(propertyChangeListener);
   button2.addActionListener(actionListener);
   frame.add(button1, BorderLayout.NORTH);
   frame.add(button2, BorderLayout.SOUTH);
   frame.setSize(300, 100);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



ActionEvent.META_MASK

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame();
   Container contentPane = frame.getContentPane();
   ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Command: " + e.getActionCommand());
       int modifiers = e.getModifiers();
       System.out.println("\tALT : "
           + checkMod(modifiers, ActionEvent.ALT_MASK));
       System.out.println("\tCTRL : "
           + checkMod(modifiers, ActionEvent.CTRL_MASK));
       System.out.println("\tMETA : "
           + checkMod(modifiers, ActionEvent.META_MASK));
       System.out.println("\tSHIFT: "
           + checkMod(modifiers, ActionEvent.SHIFT_MASK));
       Object source = e.getSource();
       if (source instanceof JComboBox) {
         JComboBox jb = (JComboBox) source;
         System.out.println("Combo: " + jb.getSelectedItem());
       }
     }
     private boolean checkMod(int modifiers, int mask) {
       return ((modifiers & mask) == mask);
     }
   };
   String flavors[] = { "Item 1", "Item 2", "Item 3"};
   JComboBox jc = new JComboBox(flavors);
   jc.setMaximumRowCount(4);
   jc.setEditable(true);
   jc.addActionListener(listener);
   contentPane.add(jc, BorderLayout.NORTH);
   JButton b = new JButton("Button!");
   b.addActionListener(listener);
   contentPane.add(b, BorderLayout.CENTER);
   JPanel panel = new JPanel();
   JLabel label = new JLabel("Label 1: ");
   JTextField text = new JTextField("Type your text", 15);
   text.addActionListener(listener);
   label.setDisplayedMnemonic(KeyEvent.VK_1);
   label.setLabelFor(text);
   panel.add(label);
   panel.add(text);
   contentPane.add(panel, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



ActionEvent.SHIFT_MASK

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Main {

 public static void main(String args[]) {
   JFrame frame = new JFrame();
   Container contentPane = frame.getContentPane();
   ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       System.out.println("Command: " + e.getActionCommand());
       int modifiers = e.getModifiers();
       System.out.println("\tALT : "
           + checkMod(modifiers, ActionEvent.ALT_MASK));
       System.out.println("\tCTRL : "
           + checkMod(modifiers, ActionEvent.CTRL_MASK));
       System.out.println("\tMETA : "
           + checkMod(modifiers, ActionEvent.META_MASK));
       System.out.println("\tSHIFT: "
           + checkMod(modifiers, ActionEvent.SHIFT_MASK));
       Object source = e.getSource();
       if (source instanceof JComboBox) {
         JComboBox jb = (JComboBox) source;
         System.out.println("Combo: " + jb.getSelectedItem());
       }
     }
     private boolean checkMod(int modifiers, int mask) {
       return ((modifiers & mask) == mask);
     }
   };
   String flavors[] = { "Item 1", "Item 2", "Item 3"};
   JComboBox jc = new JComboBox(flavors);
   jc.setMaximumRowCount(4);
   jc.setEditable(true);
   jc.addActionListener(listener);
   contentPane.add(jc, BorderLayout.NORTH);
   JButton b = new JButton("Button!");
   b.addActionListener(listener);
   contentPane.add(b, BorderLayout.CENTER);
   JPanel panel = new JPanel();
   JLabel label = new JLabel("Label 1: ");
   JTextField text = new JTextField("Type your text", 15);
   text.addActionListener(listener);
   label.setDisplayedMnemonic(KeyEvent.VK_1);
   label.setLabelFor(text);
   panel.add(label);
   panel.add(text);
   contentPane.add(panel, BorderLayout.SOUTH);
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



new ActionEvent(Object source, int id, String command)

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.EventListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.event.EventListenerList; public class MainClass {

 public static void main(String args[]) {
   JFrame frame = new JFrame("Key Text Sample");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   KeyTextComponent keyTextComponent = new KeyTextComponent();
   final JTextField textField = new JTextField();
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       String keyText = actionEvent.getActionCommand();
       textField.setText(keyText);
     }
   };
   keyTextComponent.addActionListener(actionListener);
   frame.add(keyTextComponent, BorderLayout.CENTER);
   frame.add(textField, BorderLayout.SOUTH);
   frame.setSize(300, 200);
   frame.setVisible(true);
 }

} class KeyTextComponent extends JComponent {

 private EventListenerList actionListenerList = new EventListenerList();
 public KeyTextComponent() {
   setBackground(Color.CYAN);
   KeyListener internalKeyListener = new KeyAdapter() {
     public void keyPressed(KeyEvent keyEvent) {
       if (actionListenerList != null) {
         int keyCode = keyEvent.getKeyCode();
         String keyText = KeyEvent.getKeyText(keyCode);
         ActionEvent actionEvent = new ActionEvent(this,
             ActionEvent.ACTION_PERFORMED, keyText);
         fireActionPerformed(actionEvent);
       }
     }
   };
   MouseListener internalMouseListener = new MouseAdapter() {
     public void mousePressed(MouseEvent mouseEvent) {
       requestFocusInWindow();
     }
   };
   addKeyListener(internalKeyListener);
   addMouseListener(internalMouseListener);
 }
 public void addActionListener(ActionListener actionListener) {
   actionListenerList.add(ActionListener.class, actionListener);
 }
 public void removeActionListener(ActionListener actionListener) {
   actionListenerList.remove(ActionListener.class, actionListener);
 }
 protected void fireActionPerformed(ActionEvent actionEvent) {
   EventListener listenerList[] = actionListenerList
       .getListeners(ActionListener.class);
   for (int i = 0, n = listenerList.length; i < n; i++) {
     ((ActionListener) listenerList[i]).actionPerformed(actionEvent);
   }
 }
 public boolean isFocusable() {
   return true;
 }

}


 </source>