Posted on 2010-08-19 14:59
TWaver 阅读(1499)
评论(2) 编辑 收藏
JPopupMenu是对于Swing开发者是在熟悉不过的组件了,不过大部分人只将其作为右击弹出菜单使用,其实JPopupMenu还可以有很多用途,可以说只要是需要弹出式效果的都可以用其实现。例如我们用开发工具输入import java.util.当输入“.”时会自动弹出提示框选项,以下的小程序展示了这样的效果:
1import java.awt.BorderLayout;
2import java.awt.Color;
3import java.awt.Dimension;
4import java.awt.Point;
5import java.awt.event.KeyAdapter;
6import java.awt.event.KeyEvent;
7
8import javax.swing.JComponent;
9import javax.swing.JFrame;
10import javax.swing.JLabel;
11import javax.swing.JList;
12import javax.swing.JPopupMenu;
13import javax.swing.JScrollPane;
14import javax.swing.JTextArea;
15import javax.swing.SwingConstants;
16import javax.swing.SwingUtilities;
17
18import twaver.TWaverUtil;
19
20public class PopupTipDemo extends JFrame {
21
22 String[] messages = new String[] {
23 "getTWaverJava()",
24 "getTWaverWeb()",
25 "getTWaverFlex()",
26 "getTWaverDotNET()",
27 "getTWaverGIS()",
28 "getTWaverHTML5()",
29 "getTWaverJavaFX()",
30 "getTWaver", };
31
32 JLabel label = new JLabel("TWaver makes everything easy!");
33 JList list = new JList(messages);
34 JComponent tip = new JScrollPane(list);
35 JTextArea text = new JTextArea();
36 JPopupMenu popup = new JPopupMenu();
37
38 public PopupTipDemo() {
39 super("www.servasoftware.com");
40 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
41 this.getContentPane().setLayout(new BorderLayout());
42 this.getContentPane().add(new JScrollPane(text), BorderLayout.CENTER);
43 this.tip.setPreferredSize(new Dimension(230, 80));
44 this.label.setForeground(Color.BLUE);
45 this.label.setHorizontalAlignment(SwingConstants.CENTER);
46 this.popup.setLayout(new BorderLayout());
47 this.popup.add(label, BorderLayout.NORTH);
48 this.popup.add(tip, BorderLayout.CENTER);
49
50 this.text.setText("// Try to press '.'\nimport twaver.Node;\nimport twaver.Link;\nimport twaver.network");
51 this.text.setBackground(Color.WHITE);
52 this.text.setForeground(Color.BLUE);
53 this.text.setCaretColor(Color.RED);
54
55 this.text.addKeyListener(new KeyAdapter() {
56 public void keyReleased(KeyEvent e) {
57 if (popup.isShowing()) {
58 popup.setVisible(false);
59 } else if (e.getKeyCode() == KeyEvent.VK_PERIOD) {
60 Point point = text.getCaret().getMagicCaretPosition();
61 if (point != null) {
62 popup.show(text, point.x, point.y);
63 }
64 text.requestFocus();
65 }
66 }
67 });
68 }
69
70 public static void main(String[] args) {
71 SwingUtilities.invokeLater(new Runnable() {
72 public void run() {
73 PopupTipDemo demo = new PopupTipDemo();
74 demo.setSize(400, 200);
75 TWaverUtil.centerWindow(demo);
76 demo.setVisible(true);
77 }
78 });
79 }
80}
其实TWaver Java的Demo中的QuickSearch栏就有这么个下拉功能,twaver.swing.TDropDownSelector组件,这个封装得非常好,以下寥寥几行demo.DemoUtil中的代码就实现了下拉效果,大家只要专注在自己下拉的内容即可
1// create drop down selector
2final TDropDownSelector selector = new TDropDownSelector(txtSearch, new JScrollPane(list)){
3 public Dimension getSelectorSize(){
4 int width = this.getSize().width;
5 if(width < 200){
6 width = 200;
7 }
8 return new Dimension(width, 320);
9 }
10};