Java by API/javax.swing/JPopupMenu — различия между версиями

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

Текущая версия на 14:20, 31 мая 2010

JPopupMenu: add(JMenuItem menuItem)

 
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    final JPopupMenu popup = new JPopupMenu();
    JMenuItem menuItem1 = new JMenuItem("Option 1");
    popup.add(menuItem1);
    JMenuItem menuItem2 = new JMenuItem("Option 2");
    popup.add(menuItem2);
    
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        popup.show(textField, 10,10);
      }
    };
    
    KeyStroke keystroke =
      KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke,
      JComponent.WHEN_FOCUSED);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}





JPopupMenu: addPopupMenuListener(PopupMenuListener lis)

 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new PopupActionListener();
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();
    JPopupMenu popupMenu = new JPopupMenu("Title");
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);
    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}
class PopupActionListener implements ActionListener {
  public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("Selected: " + actionEvent.getActionCommand());
  }
}
class MyPopupMenuListener implements PopupMenuListener {
  public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
    System.out.println("Canceled");
  }
  public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Invisible");
  }
  public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Visible");
  }
}





JPopupMenu: addSeparator()

 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new PopupActionListener();
    PopupMenuListener popupMenuListener = new MyPopupMenuListener();
    JPopupMenu popupMenu = new JPopupMenu("Title");
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);
    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);
    JButton label = new JButton();
    frame.add(label);
    label.setComponentPopupMenu(popupMenu);
    frame.setSize(350, 250);
    frame.setVisible(true);
  }
}
class PopupActionListener implements ActionListener {
  public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("Selected: " + actionEvent.getActionCommand());
  }
}
class MyPopupMenuListener implements PopupMenuListener {
  public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
    System.out.println("Canceled");
  }
  public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Invisible");
  }
  public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
    System.out.println("Becoming Visible");
  }
}





JPopupMenu: setDefaultLightWeightPopupEnabled(boolean aFlag)

 

import javax.swing.JPopupMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean lwPopup = JPopupMenu.getDefaultLightWeightPopupEnabled(); // true
    // Globally use heavyweight components for all popup menus
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
  }
}





JPopupMenu: setLightWeightPopupEnabled(boolean aFlag)

 
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class Main {
  public static void main(String[] argv) throws Exception {
    JMenu menu = new JMenu("Menu Label");
    menu.add(new JMenuItem("Item Label"));
    boolean lwPopup = menu.getPopupMenu().isLightWeightPopupEnabled(); // true
    menu.getPopupMenu().setLightWeightPopupEnabled(false);
  }
}





JPopupMenu: show(Component invoker, int x, int y)

 
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    final JPopupMenu popup = new JPopupMenu();
    JMenuItem menuItem1 = new JMenuItem("Option 1");
    popup.add(menuItem1);
    JMenuItem menuItem2 = new JMenuItem("Option 2");
    popup.add(menuItem2);
    
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        popup.show(textField, 10,10);
      }
    };
    
    KeyStroke keystroke =
      KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke,
      JComponent.WHEN_FOCUSED);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}