我正在學習有關更改偵聽器的信息,並且想知道爲什麼我的代碼的一部分在用戶移動滑塊時未觸發更改偵聽器。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