浏览器的Swing地址栏

开发 后端
本文介绍浏览器的Swing地址栏一般带有输入网址的记忆功能,输入首字母,就会出现以它开头的所有曾使用记录。

浏览器的Swing地址栏一般带有输入网址的记忆功能,输入首字母,就会出现以它开头的所有曾使用记录。在swing中也能很容易的实现这个功能。

对于这个功能,可以分解成几个步骤:输入-->响应并弹出提示-->选择或继续输入。为防止重复的保存,直接用Set保存所有输入。显示提示的组件可以用JList外面套上1个JWindow.再加上鼠标响应和输入响应,基本就完成了。

用户的所有输入由addCompletion()方法加入到Set中去,这个动作可以由CompletableJTextField上触发Enter快捷键响应,或者由其他的自定义动作实现,取决于你的需求。用户无论输入或者删除一个字母,后台都会根据输入匹配Set中保存的数据,然后将所有匹配条目放到 Jlist中由JWindow显示出来。

如果要看起来更好看,可以在JWindow上setBorder(xx),比如设置一个带阴影层次效果的setBorder(roundedShadowBorder);

如果要更精细一些,可考虑为JList添加上移、下移和回车事件响应,这样就跟浏览器的Swing地址栏完全一样了。

  1. publicclassCompletableJTextFieldextendsJTextFieldimplements  
  2. ListSelectionListener{  
  3. privatestaticfinallongserialVersionUID=1L;  
  4. JListcompletionList;  
  5. DefaultListModelcompletionListModel;  
  6. JScrollPanelistScroller;  
  7. JWindowlistWindow;  
  8. Set<String>completions;  
  9.  
  10. publicCompletableJTextField(intcol){  
  11. super(col);  
  12. getDocument().addDocumentListener(newCompleter());  
  13. completionListModel=newDefaultListModel();  
  14. completionList=newJList(completionListModel);  
  15. completionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
  16. completionList.addListSelectionListener(this);  
  17. listScroller=newJScrollPane(completionList,  
  18. ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,  
  19. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
  20. listWindow=newJWindow();  
  21. listWindow.getContentPane().add(listScroller);  
  22. }  
  23. publicvoidaddCompletion(Strings){  
  24. completions.add(s);  
  25. }  
  26.  
  27. publicvoidremoveCompletion(Strings){  
  28. completions.remove(s);  
  29. }  
  30.  
  31. publicvoidclearCompletions(){  
  32. completions.clear();  
  33. listWindow.setVisible(false);  
  34. }  
  35.  
  36. publicvoidvalueChanged(ListSelectionEvente){  
  37. if(completionList.getModel().getSize()==0){  
  38. return;  
  39. }  
  40. listWindow.setVisible(false);  
  41. finalStringcompletionString=(String)completionList  
  42. .getSelectedValue();  
  43. SwingUtilities.invokeLater(newRunnable(){  
  44. publicvoidrun(){  
  45. if(null!=completionString){  
  46. setText(completionString);  
  47. }  
  48. }});  
  49. }  
  50.  
  51. /**  
  52. *@returnthecompletions  
  53. */  
  54. publicSet<String>getCompletions(){  
  55. returncompletions;  
  56. }  
  57.  
  58. /**  
  59. *@paramcompletionsthecompletionstoset  
  60. */  
  61. publicvoidsetCompletions(Set<String>completions){  
  62. this.completions=completions;  
  63. }  
  64.  
  65. classCompleterimplementsDocumentListener{  
  66. privatePatternpattern;  
  67.  
  68. privatevoidbuildPopup(){  
  69. completionListModel.clear();  
  70. Iterator<String>it=completions.iterator();  
  71. pattern=Pattern.compile(getText()+".+");  
  72. while(it.hasNext()){  
  73. Stringcompletion=it.next();  
  74. Matchermatcher=pattern.matcher(completion);  
  75. if(matcher.matches()){  
  76. completionListModel.add(completionListModel.getSize(),  
  77. completion);  
  78. }  
  79. }  
  80. }  
  81.  
  82. privatevoidshowPopup(){  
  83. if(completionListModel.getSize()==0){  
  84. listWindow.setVisible(false);  
  85. return;  
  86. }  
  87.  
  88. Pointlos=getLocationOnScreen();  
  89. intpopX=los.x;  
  90. intpopY=los.y+getHeight();  
  91. listWindow.setLocation(popX,popY);  
  92. listWindow.pack();  
  93. listWindow.setVisible(true);  
  94. }  
  95.  
  96. privatevoidbuildAndShowPopup(){  
  97. if(getText().length()<1)  
  98. return;  
  99. buildPopup();  
  100. showPopup();  
  101. }  
  102.  
  103. publicvoidinsertUpdate(DocumentEvente){  
  104. buildAndShowPopup();  
  105. }  
  106.  
  107. publicvoidremoveUpdate(DocumentEvente){  
  108. buildAndShowPopup();  
  109. }  
  110.  
  111. publicvoidchangedUpdate(DocumentEvente){  
  112. buildAndShowPopup();  
  113. }  
  114.  
  115. }  
  116.  

以上是介绍浏览器的Swing地址栏,希望对大家有用。

【编辑推荐】

  1. 在表格中Swing增加列表框
  2. 浅谈Swing控件JList
  3. 概述Swing组件与外部线程
  4. Java Swing做什么好
  5. Swing文件选择器的制作
责任编辑:佚名 来源: 电子工业出版社
相关推荐

2015-12-01 10:43:55

2011-11-04 15:28:49

傲游浏览器

2011-05-20 17:23:41

Chrome 13

2016-10-18 14:22:41

2020-10-21 11:48:22

欺骗漏洞

2010-08-26 17:54:16

微软

2010-08-27 09:47:07

谷歌

2012-08-05 17:13:47

傲游

2021-05-27 20:46:22

浏览器地址栏谷歌

2017-01-03 20:13:02

2013-11-27 15:38:14

IE浏览器故障

2023-02-02 16:35:36

微软Edge浏览器

2023-01-27 11:01:54

谷歌Chrome浏览器

2020-10-26 09:56:40

恶意攻击手机浏览器地址栏欺骗

2011-02-25 09:03:03

Chrome

2009-08-06 17:34:27

地址栏控件C#记忆功能

2009-03-30 08:58:52

Firefox浏览器

2011-06-28 09:23:22

Firefox地址栏

2011-02-21 14:10:50

Chrome

2011-10-09 09:16:59

JavaScript
点赞
收藏

51CTO技术栈公众号