日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
瀏覽器的Swing地址欄

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

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

用戶的所有輸入由addCompletion()方法加入到Set中去,這個(gè)動(dòng)作可以由CompletableJTextField上觸發(fā)Enter快捷鍵響應(yīng),或者由其他的自定義動(dòng)作實(shí)現(xiàn),取決于你的需求。用戶無論輸入或者刪除一個(gè)字母,后臺(tái)都會(huì)根據(jù)輸入匹配Set中保存的數(shù)據(jù),然后將所有匹配條目放到 Jlist中由JWindow顯示出來。

如果要看起來更好看,可以在JWindow上setBorder(xx),比如設(shè)置一個(gè)帶陰影層次效果的setBorder(roundedShadowBorder);

如果要更精細(xì)一些,可考慮為JList添加上移、下移和回車事件響應(yīng),這樣就跟瀏覽器的Swing地址欄完全一樣了。

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

以上是介紹瀏覽器的Swing地址欄,希望對(duì)大家有用。


本文題目:瀏覽器的Swing地址欄
文章路徑:http://m.5511xx.com/article/ccseggd.html