Chinaunix首页 | 论坛 | 博客
  • 博客访问: 891425
  • 博文数量: 96
  • 博客积分: 10681
  • 博客等级: 上将
  • 技术积分: 2449
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-16 17:52
文章分类

全部博文(96)

文章存档

2011年(30)

2009年(36)

2008年(30)

分类: Java

2011-04-08 12:38:47

6.2 使用弹出菜单:Popup类

并不是我们希望弹出的所有内容都需要是一个菜单。通过Popup与PopupFactory类,我们可以在其他的组件上弹出任何组件。这与工具提示不同,工具提示是只读的不可选择的标签。我们可以弹出可选择的按钮,树或是表。

6.2.1 创建弹出组件

Popup是一个具有两个方法hide()与show()的简单类,同时具有两个受保护的构造函数。我们并不能直接创建Popup对象,而是需要由PopupFactory类获取。

  1. PopupFactory factory = PopupFactory.getSharedInstance();
  2. Popup popup = factory.getPopup(owner, contents, x, y);

由PopupFactory所创建的带有contents组件的Popup则会位于owner组件内的其他组件之上。

6.2.2 一个完整的Popup/PopupFactory使用示例

列表6-7演示了在另一个JButton之上显示了一个JButton的Popup与PopupFactory的使用示例。选择初始的JButton会使得在第一个JButton之上,在随机位置创建第二个。当第二个按钮可见时,每一个都是可选择的。多次选择初始的可见按钮会出现多个弹出按钮,如图6-9所示。每一个弹出菜单将会在三秒后消失。在这个例子中,选择弹出菜单只会在控制台显示一条消息。

swing_6_9

  1. package net.ariel.ch06;
  2.  
  3. import java.awt.Component;
  4. import java.awt.EventQueue;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.Random;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.Popup;
  12. import javax.swing.PopupFactory;
  13. import javax.swing.Timer;
  14.  
  15. public class ButtonPopupSample {
  16.  
  17.     static final Random random = new Random();
  18.  
  19.     // define ActionListener
  20.     static class ButtonActionListener implements ActionListener {
  21.         public void actionPerformed(ActionEvent event) {
  22.             System.out.println("Selected: "+event.getActionCommand());
  23.         }
  24.     };
  25.  
  26.     // define show popu ActionListener
  27.     static class ShowPopupActionListener implements ActionListener {
  28.         private Component component;
  29.         ShowPopupActionListener(Component component) {
  30.             this.component = component;
  31.         }
  32.         public synchronized void actionPerformed(ActionEvent event) {
  33.             JButton button = new JButton("Hello, world");
  34.             ActionListener listener = new ButtonActionListener();
  35.             button.addActionListener(listener);
  36.             PopupFactory factory = PopupFactory.getSharedInstance();
  37.             int x = random.nextInt(200);
  38.             int y = random.nextInt(200);
  39.             final Popup popup = factory.getPopup(component, button, x, y);
  40.             popup.show();
  41.             ActionListener hider = new ActionListener() {
  42.                 public void actionPerformed(ActionEvent event) {
  43.                     popup.hide();
  44.                 }
  45.             };
  46.             // hide popup in 3 seconds
  47.             Timer timer = new Timer(3000, hider);
  48.             timer.start();
  49.         }
  50.     };
  51.     /**
  52.      * @param args
  53.      */
  54.     public static void main(String[] args) {
  55.         // TODO Auto-generated method stub
  56.  
  57.         Runnable runner = new Runnable() {
  58.             public void run() {
  59.                 // create frame
  60.                 JFrame frame = new JFrame("Button Popup Sample");
  61.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.  
  63.                 ActionListener actionListener = new ShowPopupActionListener(frame);
  64.  
  65.                 JButton start = new JButton("Pick Me for Popup");
  66.                 start.addActionListener(actionListener);
  67.                 frame.add(start);
  68.  
  69.                 frame.setSize(350, 250);
  70.                 frame.setVisible(true);
  71.             }
  72.         };
  73.         EventQueue.invokeLater(runner);
  74.     }
  75.  
  76. }
阅读(2644) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~