浏览器的Swing地址栏(转帖)

浏览器的Swing地址栏一般带有输入网址的记忆功能,输入首字母,就会出现以它开头的所有曾使用记录。在swing中也能很容易的实现这个功能。 对于这个功能,可以分解成几个步骤:输入-->响应并弹出提示-->选择或继续输入。为防止重复的保存,直接用Set保存所有输入。显示提示的组件可以用JList外面套上1个JWindow.再加上鼠标响应和输入响应,基本就完成了。 用户的所有输入由addCompletion()方法加入到Set中去,这个动作可以由CompletableJTextField上触发Enter快捷键响应,或者由其他的自定义动作实现,取决于你的需求。用户无论输入或者删除一个字母,后台都会根据输入匹配Set中保存的数据,然后将所有匹配条目放到 Jlist中由JWindow显示出来。 如果要看起来更好看,可以在JWindow上setBorder(xx),比如设置一个带阴影层次效果的setBorder(roundedShadowBorder); 如果要更精细一些,可考虑为JList添加上移、下移和回车事件响应,这样就跟浏览器的Swing地址栏完全一样了。

publicclassCompletableJTextFieldextendsJTextFieldimplements  ListSelectionListener{  privatestaticfinallongserialVersionUID=1L;  JListcompletionList;  DefaultListModelcompletionListModel;  JScrollPanelistScroller;  JWindowlistWindow;  Set<String>completions;   publicCompletableJTextField(intcol){  super(col);  getDocument().addDocumentListener(newCompleter());  completionListModel=newDefaultListModel();  completionList=newJList(completionListModel);  completionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  completionList.addListSelectionListener(this);  listScroller=newJScrollPane(completionList,  ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,  ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  listWindow=newJWindow();  listWindow.getContentPane().add(listScroller);  }  publicvoidaddCompletion(Strings){  completions.add(s);  }   publicvoidremoveCompletion(Strings){  completions.remove(s);  }   publicvoidclearCompletions(){  completions.clear();  listWindow.setVisible(false);  }   publicvoidvalueChanged(ListSelectionEvente){  if(completionList.getModel().getSize()==0){  return;  }  listWindow.setVisible(false);  finalStringcompletionString=(String)completionList  .getSelectedValue();  SwingUtilities.invokeLater(newRunnable(){  publicvoidrun(){  if(null!=completionString){  setText(completionString);  }  }});  }   /**  *@returnthecompletions  */  publicSet<String>getCompletions(){  returncompletions;  }   /**  *@paramcompletionsthecompletionstoset  */  publicvoidsetCompletions(Set<String>completions){  this.completions=completions;  }   classCompleterimplementsDocumentListener{  privatePatternpattern;   privatevoidbuildPopup(){  completionListModel.clear();  Iterator<String>it=completions.iterator();  pattern=Pattern.compile(getText()+".+");  while(it.hasNext()){  Stringcompletion=it.next();  Matchermatcher=pattern.matcher(completion);  if(matcher.matches()){  completionListModel.add(completionListModel.getSize(),  completion);  }  }  }   privatevoidshowPopup(){  if(completionListModel.getSize()==0){  listWindow.setVisible(false);  return;  }   Pointlos=getLocationOnScreen();  intpopX=los.x;  intpopY=los.y+getHeight();  listWindow.setLocation(popX,popY);  listWindow.pack();  listWindow.setVisible(true);  }   privatevoidbuildAndShowPopup(){  if(getText().length()<1)  return;  buildPopup();  showPopup();  }   publicvoidinsertUpdate(DocumentEvente){  buildAndShowPopup();  }   publicvoidremoveUpdate(DocumentEvente){  buildAndShowPopup();  }   publicvoidchangedUpdate(DocumentEvente){  buildAndShowPopup();  }   }   }

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

你可能感兴趣的:(swing,浏览器)