嘿伙计们我正在使用Imageviewer,但我遇到了摆动计时器最终停止发送动作事件的问题。 如果我按下按钮"停止"它会停止计时器但启用其他按钮。
diashow确实有效。
public class test3wegenDiashow扩展JFrame实现ActionListener,MouseListener {
ArrayList<Image> imagelist = new ArrayList<Image>(); //findet später das Image durch das zahlenarraylist
ArrayList<Integer> zahlen = new ArrayList<Integer>(); // findet später das image welches angeklickt wurde
Container c = getContentPane();
JButton b0,b1,b2,b3,b4;
JPanel p0;
JPanel p1;
JLabel l0,tester;
File file[];
int currentBild = 0;
Timer timer;
public static void main(String[] args) {
test3wegenDiashow frame = new test3wegenDiashow();
}
public test3wegenDiashow() {
super("Photoviewer");
timer = new Timer(1000,this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,800);
Container c =getContentPane();
p0= new JPanel();
p0.setLayout(new FlowLayout(FlowLayout.LEFT));
c.setLayout(new BorderLayout());
p0.add(b0 = new JButton("Bilderauswahl"));
b0.addActionListener(this);
p0.add(b1 = new JButton("Vorwärts"));
b1.addActionListener(this);
p0.add(b2 = new JButton("Rückwärts"));
b2.addActionListener(this);
p0.add(b3 = new JButton("Diashow"));
b3.addActionListener(this);
p0.add(b4 = new JButton("Stop"));
b4.addActionListener(this);
c.add("South",p0);
p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add("Center",p1);
b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
String l = b.getLabel();
if(l.equals("Bilderauswahl")) { //Bilder werden mittels JFileChooser in das Array file gespeichert.
p1.removeAll();
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Bilderauswahl");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG UND GIF", "jpg", "gif");
fc.setFileFilter(filter);
fc.setMultiSelectionEnabled(true);
fc.showOpenDialog(this);
file = fc.getSelectedFiles(); // gewählte Datein werden
for(int i = 0;i<file.length;i++) { //File Liste wird durchgegangen und es werden Images mithilfe des
if (file.length!=0) { // ImageIO.read Methode erstellt. Die Images werden anschließend
try { // in JLabels umgewandelt und dem Pannel übergeben.
Image image = ImageIO.read(file[i]);
imagelist.add(image);
image =image.getScaledInstance(155,155,java.awt.Image.SCALE_SMOOTH); //Skallierung
JLabel ladd = new JLabel(new ImageIcon(image));
ladd.setText("Bild "+(i+1)); // Setze Text des Labels, um später im Array zu finden.
p1.add(ladd);
ladd.addMouseListener(this);
repaint();
revalidate();
pack();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
for (int z = 0;z<file.length;z++) {
zahlen.add(z+1);
}
}
if (l.equals("Vorwärts")) {
try {
if(currentBild >=0 && currentBild<file.length) {
currentBild = currentBild +1;
JLabel addit = new JLabel(new ImageIcon(imagelist.get(currentBild)));
p1.removeAll();
p1.add(addit);
repaint();
revalidate();
}
} catch(Exception juckt2) {
};
}
if (l.equals("Rückwärts")) {
try {
if(currentBild>0 && currentBild <=file.length) {
currentBild = currentBild -1;
JLabel addit = new JLabel(new ImageIcon(imagelist.get(currentBild)));
p1.removeAll();
p1.add(addit);
repaint();
revalidate();
}
}catch(Exception juckt3) {
};
} if (l.equals("Diashow")) {
b0.setEnabled(false);
b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(true);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) { // was soll der timer machen
p1.removeAll();
JLabel addit = new JLabel(new ImageIcon(imagelist.get(currentBild)));
p1.add(addit);
repaint();
revalidate();
currentBild = currentBild + 1;
if (currentBild>=file.length) {
currentBild=0;
}
}
});
timer.start();
}
if (l.equals("Stop")) {
timer.stop();
b0.setEnabled(true);
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(true);
b4.setEnabled(false);
}
}
@Override
public void mouseClicked(MouseEvent e) {
String abfrage = ((JLabel)e.getSource()).getText();
try {
for(int y=0;y<=file.length;y++) {
if (abfrage.equals(("Bild ")+ zahlen.get(y))) {
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(true);
b4.setEnabled(false);
JLabel addit = new JLabel(new ImageIcon(imagelist.get(y)));
p1.removeAll();
p1.add(addit);
currentBild = zahlen.get(y);
repaint();
revalidate();
pack();
}
}
}catch(Exception juckt) {
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
在第
行Timer timer = new Timer(1000, new ActionListener() {
在类的actionPerformed()
方法中,您声明了一个新的Timer
变量,而不是使用已经声明的静态变量。
只需从行中删除Timer
即
timer = new Timer(1000, new ActionListener() {