自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

瀏覽器的Swing地址欄

開發(fā) 后端
本文介紹瀏覽器的Swing地址欄一般帶有輸入網(wǎng)址的記憶功能,輸入首字母,就會出現(xiàn)以它開頭的所有曾使用記錄。

瀏覽器的Swing地址欄一般帶有輸入網(wǎng)址的記憶功能,輸入首字母,就會出現(xiàn)以它開頭的所有曾使用記錄。在swing中也能很容易的實現(xiàn)這個功能。

對于這個功能,可以分解成幾個步驟:輸入-->響應并彈出提示-->選擇或繼續(xù)輸入。為防止重復的保存,直接用Set保存所有輸入。顯示提示的組件可以用JList外面套上1個JWindow.再加上鼠標響應和輸入響應,基本就完成了。

用戶的所有輸入由addCompletion()方法加入到Set中去,這個動作可以由CompletableJTextField上觸發(fā)Enter快捷鍵響應,或者由其他的自定義動作實現(xiàn),取決于你的需求。用戶無論輸入或者刪除一個字母,后臺都會根據(jù)輸入匹配Set中保存的數(shù)據(jù),然后將所有匹配條目放到 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文件選擇器的制作
責任編輯:佚名 來源: 電子工業(yè)出版社
相關(guān)推薦

2015-12-01 10:43:55

2011-11-04 15:28:49

傲游瀏覽器

2011-05-20 17:23:41

Chrome 13

2020-10-21 11:48:22

欺騙漏洞

2016-10-18 14:22:41

2010-08-27 09:47:07

谷歌

2012-08-05 17:13:47

傲游

2010-08-26 17:54:16

微軟

2017-01-03 20:13:02

2021-05-27 20:46:22

瀏覽器地址欄谷歌

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

惡意攻擊手機瀏覽器地址欄欺騙

2024-04-11 08:33:25

2011-02-25 09:03:03

Chrome

2009-08-06 17:34:27

地址欄控件C#記憶功能

2011-06-28 09:23:22

Firefox地址欄

2009-03-30 08:58:52

Firefox瀏覽器

2011-02-21 14:10:50

Chrome
點贊
收藏

51CTO技術(shù)棧公眾號