בעיה עם Timer ב-JAVA
לא מצליח לגרום לזה לעבוד...
אני רוצה שבלחיצת כפתור, הטיימר יתחיל לפעול, וכל X שניות ידפיס למסך אחת מההודעות (מערך של מחרוזות).
אחרי שהוא מדפיס את כולן, הוא אמור להפסיק.
את ההדפסה לקונסול אני רואה אחרי כל X שניות, אבל את התצוגה ל-GUI, רואים רק אחרי שהוא מסיים.
נראה לי שאני מפספס פה משהו די בסיסי..
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.Timer;public class TestThread extends JFrame{ JButton button = new JButton("Run"); JPanel msgPanel = new JPanel(); Timer myTimer; public TestThread(){ super(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] msgs = new String[]{"1", "2", "3", "4"}; myTimer = new Timer(1500, new TimerAction(msgs)); myTimer.start(); } }); add(button, BorderLayout.NORTH); add(msgPanel, BorderLayout.CENTER); pack(); setSize(400, 400); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class TimerAction extends AbstractAction{ String[] messageToPrint; int counter; public TimerAction(String[] msgs){ messageToPrint = msgs; } @Override public void actionPerformed(ActionEvent e) { if(counter < messageToPrint.length){ System.out.println(messageToPrint[counter]); msgPanel.add(new JLabel(messageToPrint[counter])); counter ++; } else myTimer.stop(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { new TestThread(); } }); }}
לא מצליח לגרום לזה לעבוד...
אני רוצה שבלחיצת כפתור, הטיימר יתחיל לפעול, וכל X שניות ידפיס למסך אחת מההודעות (מערך של מחרוזות).
אחרי שהוא מדפיס את כולן, הוא אמור להפסיק.
את ההדפסה לקונסול אני רואה אחרי כל X שניות, אבל את התצוגה ל-GUI, רואים רק אחרי שהוא מסיים.
נראה לי שאני מפספס פה משהו די בסיסי..
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.Timer;public class TestThread extends JFrame{ JButton button = new JButton("Run"); JPanel msgPanel = new JPanel(); Timer myTimer; public TestThread(){ super(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] msgs = new String[]{"1", "2", "3", "4"}; myTimer = new Timer(1500, new TimerAction(msgs)); myTimer.start(); } }); add(button, BorderLayout.NORTH); add(msgPanel, BorderLayout.CENTER); pack(); setSize(400, 400); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class TimerAction extends AbstractAction{ String[] messageToPrint; int counter; public TimerAction(String[] msgs){ messageToPrint = msgs; } @Override public void actionPerformed(ActionEvent e) { if(counter < messageToPrint.length){ System.out.println(messageToPrint[counter]); msgPanel.add(new JLabel(messageToPrint[counter])); counter ++; } else myTimer.stop(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { new TestThread(); } }); }}