我有一个用于收听歌曲的小软件。我创建了一个小图形界面,显示已经过的时间,标签中发布的时间和JSlider,以了解歌曲中的大致位置。这部分代码还可以。
我想,当我在JSlider上移动拇指时,更新歌曲的阅读位置。为此,我使用UpdateObservateur(Observateur =观察者)方法实现了一个接口“Observable”,它必须更新观察者列表。
我的问题是:我可以访问方法updateObservateur但从不更新观察者。在下面的代码中,这意味着我可以显示“简单更新”,但从不显示“完整更新”。我认为解决这个问题很容易但是......我没有找到任何解决方案。
import java.awt.BorderLayout;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class GUITimeDisplayer extends JPanel implements Observable {
private JLabel durationLabel = new JLabel();
private JLabel elapsedTimeLabel = new JLabel();
private JLabel remainedTimeLabel = new JLabel();
private ArrayList<Observateur> listObservateur = new ArrayList<Observateur>();
double position;
private double durationTime, remainingTime, elapsedSec, remainedSec;
private int elapsedMin, remainedMin;
DecimalFormat df = new DecimalFormat("00.00");
JSlider time = new JSlider();
boolean timeControl = true;
GUITimeDisplayer(double durationTime) {
this.durationTime=durationTime;
this.setLayout(new BorderLayout());
JPanel timeDisplayer=new JPanel();
timeDisplayer.setLayout(new BorderLayout());
timeDisplayer.add(elapsedTimeLabel, BorderLayout.WEST);
timeDisplayer.add(remainedTimeLabel, BorderLayout.EAST);
time.setMinimum(0);
time.setMaximum((int)durationTime);
time.setPaintTicks(true);
//time.setPaintLabels(true);
time.setMinorTickSpacing(20);
time.setMajorTickSpacing(60);
if (timeControl == true ) {
time.addChangeListener(new TestListener() {
});
}
//this.add(durationLabel);
this.add(timeDisplayer, BorderLayout.NORTH);
//this.add(remainedTimeLabel);
this.add(time, BorderLayout.CENTER);
}
class TestListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent event) {
// TODO Auto-generated method stub
position = (double)(((JSlider)event.getSource()).getValue());
//System.out.println(position);
updateObservateur();
}
}
public void setDurationTime(double durationTime) {
this.durationTime=durationTime;
df.format(durationTime);
durationLabel.setText(String.valueOf(df.format(durationTime)));
}
public void setElapsedTime(double elapsedTime) {
if (elapsedTime >= 60) {
elapsedMin=(int) elapsedTime/60;
elapsedSec=elapsedTime%60;
elapsedTimeLabel.setText(String.valueOf(elapsedMin)+":"+String.valueOf(df.format(elapsedSec)));
} else {
//elapsedSec=elapsedTime;
elapsedTimeLabel.setText(String.valueOf(df.format(elapsedTime)));
}
remainingTime=durationTime-elapsedTime;
if (remainingTime>=60) {
remainedMin=(int) remainingTime/60;
remainedSec=remainingTime%60;
remainedTimeLabel.setText("-" + String.valueOf(remainedMin)+":"+String.valueOf(df.format(remainedSec)));
} else {
remainedTimeLabel.setText(String.valueOf(df.format(remainingTime)));
}
time.setValue((int)elapsedTime);
}
@Override
public void addObservateur(Observateur obs) {
// TODO Auto-generated method stub
this.listObservateur.add(obs);
}
@Override
public void delObservateur() {
// TODO Auto-generated method stub
this.listObservateur = new ArrayList<Observateur>();
}
@Override
public void updateObservateur() {
// TODO Auto-generated method stub
System.out.println("Simple update : "+position);
for(Observateur obs : this.listObservateur) {
obs.updatePosition(position);
System.out.println("Complete update : "+position);
}
}
}