移至 :vote
SliderMoved
class SliderMoved implements ChangeListener {
private int vote;
public void stateChanged(ChangeEvent e) {
this.vote = ratingS.getValue();
// do something with the vote, you can even access
// methods and fields of the outer class
}
public int getVote() {
return this.vote;
}
}
SliderMoved sm = new SliderMoved();
ratingS.addChangeListener(sm);
// if you need access to the actual rating...
int value = rattingS.getValue();
// ...or
int value2 = sm.getVote();
编辑
或者,将模型类传递给更改侦听器
public class Person {
private String name;
private int vote;
public int getVote() {
return this.vote;
}
public void setVote(int vote) {
this.vote = vote;
}
// omitting other setter and getter
}
Person
使用方式如下:
class SliderMoved implements ChangeListener {
private Person person;
public SliderMoved(Person person) {
this.person = person;
}
public void stateChanged(ChangeEvent e) {
this.person.setVote(ratingS.getValue());
}
public Person getPerson() {
return this.person;
}
}
Person person = new Person();
ratingS.addChangeListener(new SliderMoved(person));
// access the vote
int vote = person.getVote();