This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Previous revision Next revision Both sides next revision | ||
|
swing:popup_menus [2015/05/18 05:47] |
swing:popup_menus [2015/05/18 06:46] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Pop-Up Menus ====== | ||
| + | Ένα pop-up menu (υλοποιείται από την κλάση [[http:// | ||
| + | |||
| + | Η Java λαμβάνει υπόψη αυτή την ιδιαιτερότητα, | ||
| + | |||
| + | <code java> | ||
| + | void mousePressed(MouseEvent e) | ||
| + | //Invoked when a mouse button has been pressed on a component. | ||
| + | |||
| + | void mouseReleased(MouseEvent e) | ||
| + | //Invoked when a mouse button has been released on a component. | ||
| + | </ | ||
| + | |||
| + | Σε κάθε ένα από τα παραπάνω events εξετάζεται αν θα πρέπει να εμφανίσετε το pop-up menu μέσω της κλήσης της μεθόδου | ||
| + | <code java> | ||
| + | |||
| + | Δείτε το παρακάτω απλοποιημένο παράδειγμα από το site της Oracle. Αυτό που αξίζει να προσέξετε είναι η εσωτερική κλάση '' | ||
| + | <code java> | ||
| + | void show(Component invoker, int x, int y) | ||
| + | </ | ||
| + | της κλάσης [[http:// | ||
| + | |||
| + | <code java PopupMenuDemo.java> | ||
| + | package components; | ||
| + | |||
| + | import java.awt.*; | ||
| + | import java.awt.event.*; | ||
| + | import javax.swing.JPopupMenu; | ||
| + | import javax.swing.JMenu; | ||
| + | import javax.swing.JMenuItem; | ||
| + | import javax.swing.JCheckBoxMenuItem; | ||
| + | import javax.swing.JRadioButtonMenuItem; | ||
| + | import javax.swing.ButtonGroup; | ||
| + | import javax.swing.JMenuBar; | ||
| + | import javax.swing.KeyStroke; | ||
| + | import javax.swing.ImageIcon; | ||
| + | |||
| + | import javax.swing.JPanel; | ||
| + | import javax.swing.JTextArea; | ||
| + | import javax.swing.JScrollPane; | ||
| + | import javax.swing.JFrame; | ||
| + | |||
| + | /* PopupMenuDemo.java requires images/ | ||
| + | |||
| + | /* | ||
| + | * Like MenuDemo, but with popup menus added. | ||
| + | */ | ||
| + | public class PopupMenuDemo implements ActionListener { | ||
| + | JTextArea output; | ||
| + | JScrollPane scrollPane; | ||
| + | String newline = " | ||
| + | |||
| + | public Container createContentPane() { | ||
| + | //Create the content-pane-to-be. | ||
| + | JPanel contentPane = new JPanel(new BorderLayout()); | ||
| + | contentPane.setOpaque(true); | ||
| + | |||
| + | //Create a scrolled text area. | ||
| + | output = new JTextArea(5, | ||
| + | output.setEditable(false); | ||
| + | scrollPane = new JScrollPane(output); | ||
| + | |||
| + | //Add the text area to the content pane. | ||
| + | contentPane.add(scrollPane, | ||
| + | |||
| + | return contentPane; | ||
| + | } | ||
| + | |||
| + | public void createPopupMenu() { | ||
| + | JMenuItem menuItem; | ||
| + | |||
| + | //Create the popup menu. | ||
| + | JPopupMenu popup = new JPopupMenu(); | ||
| + | menuItem = new JMenuItem(" | ||
| + | menuItem.addActionListener(this); | ||
| + | popup.add(menuItem); | ||
| + | menuItem = new JMenuItem(" | ||
| + | menuItem.addActionListener(this); | ||
| + | popup.add(menuItem); | ||
| + | |||
| + | //Add listener to the text area so the popup menu can come up. | ||
| + | MouseListener popupListener = new PopupListener(popup); | ||
| + | output.addMouseListener(popupListener); | ||
| + | } | ||
| + | |||
| + | public void actionPerformed(ActionEvent e) { | ||
| + | JMenuItem source = (JMenuItem)(e.getSource()); | ||
| + | String s = " | ||
| + | + newline | ||
| + | + " | ||
| + | + " (an instance of " + getClassName(source) + " | ||
| + | output.append(s + newline); | ||
| + | output.setCaretPosition(output.getDocument().getLength()); | ||
| + | } | ||
| + | |||
| + | // Returns just the class name -- no package info. | ||
| + | protected String getClassName(Object o) { | ||
| + | String classString = o.getClass().getName(); | ||
| + | int dotIndex = classString.lastIndexOf(" | ||
| + | return classString.substring(dotIndex+1); | ||
| + | } | ||
| + | |||
| + | /** Returns an ImageIcon, or null if the path was invalid. */ | ||
| + | protected static ImageIcon createImageIcon(String path) { | ||
| + | java.net.URL imgURL = PopupMenuDemo.class.getResource(path); | ||
| + | if (imgURL != null) { | ||
| + | return new ImageIcon(imgURL); | ||
| + | } else { | ||
| + | System.err.println(" | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Create the GUI and show it. For thread safety, | ||
| + | * this method should be invoked from the | ||
| + | * event-dispatching thread. | ||
| + | */ | ||
| + | private static void createAndShowGUI() { | ||
| + | //Create and set up the window. | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | |||
| + | // | ||
| + | PopupMenuDemo demo = new PopupMenuDemo(); | ||
| + | frame.setContentPane(demo.createContentPane()); | ||
| + | |||
| + | //Create and set up the popup menu. | ||
| + | demo.createPopupMenu(); | ||
| + | |||
| + | //Display the window. | ||
| + | frame.setSize(450, | ||
| + | frame.setVisible(true); | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | //Schedule a job for the event-dispatching thread: | ||
| + | //creating and showing this application' | ||
| + | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| + | public void run() { | ||
| + | createAndShowGUI(); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | class PopupListener extends MouseAdapter { | ||
| + | JPopupMenu popup; | ||
| + | |||
| + | PopupListener(JPopupMenu popupMenu) { | ||
| + | popup = popupMenu; | ||
| + | } | ||
| + | |||
| + | public void mousePressed(MouseEvent e) { | ||
| + | maybeShowPopup(e); | ||
| + | } | ||
| + | |||
| + | public void mouseReleased(MouseEvent e) { | ||
| + | maybeShowPopup(e); | ||
| + | } | ||
| + | |||
| + | private void maybeShowPopup(MouseEvent e) { | ||
| + | if (e.isPopupTrigger()) { | ||
| + | popup.show(e.getComponent(), | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||