User interface gui JLIst运行时错误 如何修复RemoveAction类以避免出现以下运行时错误 线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中出现异常:-1 导入javax.swing.*; 导入javax.swing.event.*; 导入java.awt.*; 导入java.awt.event.*; 导入java.util.List; 导入java.util.ArrayList; 导入java.io.*; 公共类GuiDriver扩展JFrame{ JList channelTitleJList,itemttitlejlist; DefaultListModel cModel,iModel; List feedList=新的ArrayList(); int nextFeed=0; 列表选择模型lsm; 公共无效添加源(RssReader源){ feedList.add(feed); } 公共JToolBar createToolBar(){ JToolBar=新的JToolBar(); Action newToolBarButton=新添加操作(“新”); Action deleteToolBarButton=新建删除操作(“删除”); 动作clearToolBarButton=新的ClearAction(“清除”); 添加(新工具栏按钮); 添加(删除工具栏按钮); 添加(ClearToolBar按钮); 可设置浮动条(假); 返回杆; } 公共JSplitPane createJSplitPane(){ JSplitPane hSplitPane=新的JSplitPane(JSplitPane.HORIZONTAL_SPLIT,createChannelJScrollPane(),createItemJScrollPane()); hSplitPane.setdividerllocation(150); 返回窗格; } 公共JScrollPane createChannelJScrollPane(){ cModel=新的DefaultListModel(); channelTitleJList=新JList(cModel); JScrollPane channelJScrollPane=新的JScrollPane(channelTitleJList); channelTitleJList.setVisibleRowCount(20); lsm=channelTitleJList.getSelectionModel(); lsm.addListSelectionListener(新的ChannelListListener()); 返回通道jScrollPane; } 公共JScrollPane createItemJScrollPane(){ iModel=新的DefaultListModel(); itemTitleJList=新JList(iModel); JScrollPane itemJScrollPane=新的JScrollPane(itemttitlejlist); //itemTitleJList.addListSelectionListener(新的ItemListListener()); 返回itemJScrollPane; } 公共类AddAction扩展了AbstractAction{ 公共AddAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ System.out.println(getValue(Action.NAME)+“selected”); JOptionPane消息=新建JOptionPane(); String firstInput=message.showInputDialog(null,“输入URL”); 试一试{ DumpStockPage.readXml(firstInput); 文件f=新文件(“RSSFeed.xml”); addFeed(新RssReader(f)); cModel.addElement(feedList.get(nextFeed.rssChannel.getTitle()); nextFeed++; iModel.clear(); } 捕获(IOEE异常){ 系统错误打印项次(ee); } } } 公共类RemoveAction扩展了AbstractAction{ 公共RemoveAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ cModel.removeElement(channelTitleJList.getSelectedValue()); iModel.clear(); } } 公共类ClearAction扩展了AbstractAction{ 公共ClearAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ iModel.clear(); } } 私有类ChannelListListener实现ListSelectionListener{ public void值已更改(ListSelectionEvent e){ 布尔调整=e.getValueIsAdjusting(); 如果(!调整){ iModel.clear(); 列表标题=新的ArrayList(); int i=channelTitleJList.getSelectedIndex();//左侧列表中选定项的索引 for(RssItem提要:feedList.get(i).rssChannel.items){ iModel.addElement(feed.getTitle());//构建标题列表,显示在右侧列表中 } } } } /* 私有类ItemListListener实现ListSelectionListener{ public void值已更改(ListSelectionEvent e){ 布尔调整=e.getValueIsAdjusting(); 如果(!调整){ int i=e.getLastIndex(); 试试{ JFrame w=新的JFrame(); w、 setTitle(“Html显示”); w、 设置大小(1000600); w、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w、 setVisible(真); JEditorPane htmlPane=新的JEditorPane(); setPage(feedList.get(i).rssChannel.items.get(i).getLink()); w、 添加(新的JScrollPane(htmlPane)); } 捕获(异常ee){ ee.printStackTrace(); } } } } */ 公共静态void main(字符串[]args){ EventQueue.invokeLater(新的Runnable(){ public void run(){ GuiDriver frame=新的GuiDriver(); frame.setTitle(“RSS阅读器”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(frame.createJSplitPane(),BorderLayout.NORTH); frame.add(frame.createToolBar(),BorderLayout.SOUTH); frame.setVisible(true); 框架设置尺寸(800400); } }); } }

User interface gui JLIst运行时错误 如何修复RemoveAction类以避免出现以下运行时错误 线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中出现异常:-1 导入javax.swing.*; 导入javax.swing.event.*; 导入java.awt.*; 导入java.awt.event.*; 导入java.util.List; 导入java.util.ArrayList; 导入java.io.*; 公共类GuiDriver扩展JFrame{ JList channelTitleJList,itemttitlejlist; DefaultListModel cModel,iModel; List feedList=新的ArrayList(); int nextFeed=0; 列表选择模型lsm; 公共无效添加源(RssReader源){ feedList.add(feed); } 公共JToolBar createToolBar(){ JToolBar=新的JToolBar(); Action newToolBarButton=新添加操作(“新”); Action deleteToolBarButton=新建删除操作(“删除”); 动作clearToolBarButton=新的ClearAction(“清除”); 添加(新工具栏按钮); 添加(删除工具栏按钮); 添加(ClearToolBar按钮); 可设置浮动条(假); 返回杆; } 公共JSplitPane createJSplitPane(){ JSplitPane hSplitPane=新的JSplitPane(JSplitPane.HORIZONTAL_SPLIT,createChannelJScrollPane(),createItemJScrollPane()); hSplitPane.setdividerllocation(150); 返回窗格; } 公共JScrollPane createChannelJScrollPane(){ cModel=新的DefaultListModel(); channelTitleJList=新JList(cModel); JScrollPane channelJScrollPane=新的JScrollPane(channelTitleJList); channelTitleJList.setVisibleRowCount(20); lsm=channelTitleJList.getSelectionModel(); lsm.addListSelectionListener(新的ChannelListListener()); 返回通道jScrollPane; } 公共JScrollPane createItemJScrollPane(){ iModel=新的DefaultListModel(); itemTitleJList=新JList(iModel); JScrollPane itemJScrollPane=新的JScrollPane(itemttitlejlist); //itemTitleJList.addListSelectionListener(新的ItemListListener()); 返回itemJScrollPane; } 公共类AddAction扩展了AbstractAction{ 公共AddAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ System.out.println(getValue(Action.NAME)+“selected”); JOptionPane消息=新建JOptionPane(); String firstInput=message.showInputDialog(null,“输入URL”); 试一试{ DumpStockPage.readXml(firstInput); 文件f=新文件(“RSSFeed.xml”); addFeed(新RssReader(f)); cModel.addElement(feedList.get(nextFeed.rssChannel.getTitle()); nextFeed++; iModel.clear(); } 捕获(IOEE异常){ 系统错误打印项次(ee); } } } 公共类RemoveAction扩展了AbstractAction{ 公共RemoveAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ cModel.removeElement(channelTitleJList.getSelectedValue()); iModel.clear(); } } 公共类ClearAction扩展了AbstractAction{ 公共ClearAction(字符串名称){ 超级(姓名); } 已执行的公共无效操作(操作事件e){ iModel.clear(); } } 私有类ChannelListListener实现ListSelectionListener{ public void值已更改(ListSelectionEvent e){ 布尔调整=e.getValueIsAdjusting(); 如果(!调整){ iModel.clear(); 列表标题=新的ArrayList(); int i=channelTitleJList.getSelectedIndex();//左侧列表中选定项的索引 for(RssItem提要:feedList.get(i).rssChannel.items){ iModel.addElement(feed.getTitle());//构建标题列表,显示在右侧列表中 } } } } /* 私有类ItemListListener实现ListSelectionListener{ public void值已更改(ListSelectionEvent e){ 布尔调整=e.getValueIsAdjusting(); 如果(!调整){ int i=e.getLastIndex(); 试试{ JFrame w=新的JFrame(); w、 setTitle(“Html显示”); w、 设置大小(1000600); w、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); w、 setVisible(真); JEditorPane htmlPane=新的JEditorPane(); setPage(feedList.get(i).rssChannel.items.get(i).getLink()); w、 添加(新的JScrollPane(htmlPane)); } 捕获(异常ee){ ee.printStackTrace(); } } } } */ 公共静态void main(字符串[]args){ EventQueue.invokeLater(新的Runnable(){ public void run(){ GuiDriver frame=新的GuiDriver(); frame.setTitle(“RSS阅读器”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(frame.createJSplitPane(),BorderLayout.NORTH); frame.add(frame.createToolBar(),BorderLayout.SOUTH); frame.setVisible(true); 框架设置尺寸(800400); } }); } },user-interface,runtime,jlist,User Interface,Runtime,Jlist,将检查添加到您的ChannelListListener.valueChanged(): 就在for循环之前,或者更好: if (i==-1) return; if (i==-1) return; if (channelTitleJList.isSelectionEmpty()) return;

将检查添加到您的
ChannelListListener.valueChanged()

就在for循环之前,或者更好:

if (i==-1) return;
if (i==-1) return;
if (channelTitleJList.isSelectionEmpty()) return;