我正在学习更改侦听器,并想了解为什么我的部分代码在用户移动滑块时没有触发更改侦听器。
这里是听者。一切都在同一个方法中:
ChangeListener lst = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
showDate();
}
};
这是一些添加侦听器的代码:
m_slDay = new JSlider(JSlider.HORIZONTAL, 1, maxDays,
m_calendar.get(Calendar.DAY_OF_MONTH));
m_slDay.setPaintLabels(true);
m_slDay.setMajorTickSpacing(5);
m_slDay.setMinorTickSpacing(1);
m_slDay.setPaintTicks(true);
// This line of code seems to work.
m_slDay.addChangeListener(lst);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(), "Day"));
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createRigidArea(RIGID_DIMENSION));
p.add(m_slDay);
p.add(Box.createRigidArea(RIGID_DIMENSION));
p1.add(p);
此代码基于上述代码块,但当用户移动滑块时没有任何反应:
m_slDayOfWeek = new JSlider(JSlider.HORIZONTAL, 1, maxDaysOfTheWeek,
m_calendar.get(Calendar.DAY_OF_WEEK));
m_slDayOfWeek.setPaintLabels(true);
m_slDayOfWeek.setMajorTickSpacing(1);
m_slDayOfWeek.setMinorTickSpacing(1);
m_slDayOfWeek.setPaintTicks(true);
// This one is not working and I'm not sure why it's happening.
m_slDayOfWeek.addChangeListener(lst);
p = new JPanel();
p.setBorder(new TitledBorder(new EtchedBorder(), "Day of Week"));
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(Box.createRigidArea(RIGID_DIMENSION));
p.add(m_slDayOfWeek);
p.add(Box.createRigidArea(RIGID_DIMENSION));
p1.add(p);