我正在学习有关更改侦听器的信息,并且想知道为什么我的代码的一部分在用户移动滑块时未触发更改侦听器。ChangeListener未触发
这是听众。一切都是在同一个方法:
下面是一些代码,增加了听众:
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);
我不禁怀疑问题出在您没有发布的其中一行。 showDate()方法做了什么?你有没有试过把断点或println语句放入它来检查它是否到达? – 2012-03-22 15:00:30
你的'showDate()'方法做什么?你可以在这个位置添加一个断点或一些syserr调试语句,以证明没有任何事情发生? – 2012-03-22 15:02:16
谢谢大家。接得好。它在showDate()方法中。其中一条if语句没有执行。使用System.out.println()证明该方法正在执行,但不是我的一些代码。 – 2012-03-22 15:23:15