Java ActionListener和组合框问题
我目前遇到一个问题,我正在创建一个简单的web浏览器,并希望写入访问的每个页面的url。无论如何,当历史记录组合框中填充URL的时,.addItem函数将触发my action listener。这有什么办法吗Java ActionListener和组合框问题,java,swing,actionlistener,Java,Swing,Actionlistener,我目前遇到一个问题,我正在创建一个简单的web浏览器,并希望写入访问的每个页面的url。无论如何,当历史记录组合框中填充URL的时,.addItem函数将触发my action listener。这有什么办法吗 Action Listener: historyList.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent
Action Listener:
historyList.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
final String url= historyList.getSelectedItem().toString();
addressbar.setText(url); //set the address bar to the url
historyArray[historynum] = url; //add the url to the array which contains the history
historynum = historynum + 1; //move one up the history array
Display(url); //display the url
backplace = 1; //reset the amount of backs a user can take
forwardBtn = false; //not allow the user to go forward again
}
}
);
}
将配置文件中的URL添加到组合:
for (String linehistory : Files.readAllLines(Paths.get("config.history"),Charset.defaultCharset())) {
if (!(linehistory ==null)){
for (String partHistory : linehistory.split("\\s+")) {
String historylines = partHistory;
if( i >6){
if (!(partHistory ==null)){
historyList.addItem(partHistory);
}
}
i = i +1;
}
}
我已经注意到了这一点,我认为这可能是由于组合框的状态被更改,有没有办法将其更改为onClick 听起来不对。JComboBox文档说,在进行选择时调用操作侦听器,而不是在添加项时调用。所以在其他地方这可能是个错误。你能创建一个完整的例子来运行和再现这个问题吗?@markspace当我逐步完成代码时,当它进入.addItem时,它就会跳转。我不知道作为一个更大项目的一部分,我会怎么做?。当运行循环时,问题会变得更加混乱,因为循环运行时会用第一次工作的值填充框,不会导致动作侦听器激活,但只有在再次使用时,它才会导致问题您只需要编写一个小程序来重现问题,仅此而已。@markspace我已对问题进行了编辑,这有什么不同吗?如果不是的话,我会重拍这部电影。我看不出有什么不同。至少对我来说,需要完整的代码。