Java-如何正确地将ListSelectionListener添加到控制器?
我最近一直在努力解决一个让我抓狂的Java问题。我一直在尝试将控制器中的ListSelectionListener添加到视图中的JList,但当我成功地将侦听器连接到某个对象时,屏幕上绘制的不是JList 下面的代码给出了我试图做什么的基本想法 这是我的课堂,我的主要方法是:Java-如何正确地将ListSelectionListener添加到控制器?,java,swing,model-view-controller,jlist,listselectionlistener,Java,Swing,Model View Controller,Jlist,Listselectionlistener,我最近一直在努力解决一个让我抓狂的Java问题。我一直在尝试将控制器中的ListSelectionListener添加到视图中的JList,但当我成功地将侦听器连接到某个对象时,屏幕上绘制的不是JList 下面的代码给出了我试图做什么的基本想法 这是我的课堂,我的主要方法是: package Application; public class Main { /** * @param args */ public static void main(String[] args) {
package Application;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
View v = new View();
Controller c = new Controller(v);
}
}
我的看法是:
package Application;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
public class View extends JFrame {
/* Components for the JList */
public DefaultListModel<String> listModel = new DefaultListModel<String>();
public JList<String> selectedItems = new JList<String>(listModel);
JScrollPane scroll = new JScrollPane(selectedItems);
public View()
{
// set the window title
this.setTitle("JList Test");
// set the window size
this.setSize(new Dimension(400, 400));
// set the window start position
this.setLocation(25, 25);
// set the window layout
FlowLayout layout = new FlowLayout();
layout.setHgap(0);
layout.setVgap(0);
this.setLayout(layout);
// set the window background
this.getContentPane().setBackground(Color.BLACK);
// make the window non-resizable
this.setResizable(false);
// set the default close operation
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setup and add the JList
initJList();
this.add(scroll);
// add elements to the list model
listModel.addElement(" Item 1 ");
listModel.addElement(" Item 2 ");
listModel.addElement(" Item 3 ");
// make the gui visible
this.setVisible(true);
}
private void initJList() {
selectedItems.setVisibleRowCount(8);
selectedItems.setFixedCellWidth(300);
selectedItems.setFixedCellHeight(40);
selectedItems.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
selectedItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectedItems = new JList<String>(listModel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
}
它编译正常,列表绘制良好,但当我在JList中选择任何内容时,ListSelectionListener从不激发。谁能告诉我到底哪里出了问题?因为我现在已经尝试了很多东西,我完全被难倒了!我觉得奇怪的是,当我尝试用JButtons和ActionListener来做我正在做的事情时,它工作得非常好
非常感谢您的帮助。您在initJList()函数中犯了一个错误,您已经在它之前传递了listModel的值,并且您在这个函数中再次传递了这个值,因此注释这一行可以解决问题。修改后的函数如下所示:
private void initJList() {
selectedItems.setVisibleRowCount(8);
selectedItems.setFixedCellWidth(300);
selectedItems.setFixedCellHeight(40);
selectedItems.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
selectedItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//selectedItems = new JList<String>(listModel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
private void initJList(){
选择EditEMS.setVisibleRowCount(8);
选择Editems.setFixedCellWidth(300);
选择Editems.setFixedCellHeight(40);
选择editems.setboorder(BorderFactory.createEmptyBorder(5,10,5,10));
selectedItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//selectedItems=new JList(listModel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HorizontalScrollBar\uNever);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS);
}
否则:
Declaring the JList like this
public JList<String> selectedItems;
and keeping the function as it is will run fine.
这样声明JList
公共JList selectedItems;
保持功能的原样将可以正常运行。
这样一个简单的错误给我带来了很多问题。万分感谢!
Declaring the JList like this
public JList<String> selectedItems;
and keeping the function as it is will run fine.