[英]Stop Timer In java Swing
我剛開始使用Swing並使用Timer
。 我寫的程序基本上上下移動一個矩形到屏幕上的特定點,我使用計時器讓它運行緩慢而平穩。 但是當我試圖阻止它時我遇到了問題。 以下是代碼:
提升類改變矩形的位置:
public void moveUp(int destination){
speed++;
if(speed>5){
speed = 5;
}
System.out.println("Speed is: "+speed);
yPos -= speed;
if(yPos < destination){
yPos = destination;
isStop = true;
}
setPos(xPos, yPos);
}
而獲得Timer
和MouseListener
的類:
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
liftArray.get(0).moveUp(rowDisctance / 2);
repaint();
}
});
timer.start();
}
}
如果我確實理解你正在尋找這樣的東西,你需要兩個定時器來控制上下機制,timer1一個向下移動,timer2向上移動,反之亦然。 你需要停止timer1然后在timer1里面你需要啟動timer2,這里是下面的代碼和動畫。
添加你的字段
Point rv;
在構造函數中將初始位置設置為對話框(矩形)
rv= rectangle.this.getLocation();
你的按鈕動作已執行
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
timer1.setInitialDelay(0);
timer1.start();
jTextArea1.append("Timer 1 Started Moving Down\n");
}
在java中復制粘貼這兩個timer1和timer2的方法
private Timer timer1 = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rv.y++;
if (rv.y == 500) {
timer1.stop();
jTextArea1.append("Timer 1 Stopped\n");
jTextArea1.append("Timer 2 Started Moving Up\n");
timer2.setInitialDelay(0);
timer2.start();
}
rectangle.this.setLocation(rv.x , rv.y);
rectangle.this.repaint();
}
});
private Timer timer2 = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rv.y--;
if (rv.y == 200 ) {
timer2.stop();
jTextArea1.append("Timer 2 Stopped\n");
}
rectangle.this.setLocation(rv.x , rv.y);
rectangle.this.repaint();
}
});
為了想象如何處理這個問題,我編寫了一個小例子:
啟動時,將創建兩個矩形。 當您在繪圖區域上單擊鼠標左鍵時,它們開始移動。
到達目的地線時,動作將停止。
邏輯取決於Rectangle類中的狀態 。 當計時器事件處理程序運行時, 如果到達的所有矩形都具有isStopped狀態,則 檢查每個矩形的狀態並停止計時器:
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/**
* Variable isStopped is used to track, if any
* rectangle didn't reach the destination yet
*/
boolean isStopped = true;
for(int i = 0; i < count; i++){
rectangles[i].moveUp(destination);
if (!rectangles[i].isStopped()) {
isStopped = false;
}
}
drawPanel.repaint();
/**
* With all rectangles having arrived at destination,
* the timer can be stopped
*/
if (isStopped) {
timer.stop();
}
}
});
timer.start();
}
}
從Rectangle類中摘錄 - 在這里您可以看到,isStopped狀態是如何處理的 - 內部為私有isStop變量 - 可以使用isStopped getter進行檢索。
/**
* Moves the rectangle up until destination is reached
* speed is the amount of a single movement.
*/
public void moveUp(int destination) {
if (speed < 5) {
speed++;
}
y -= speed;
if (y < destination) {
y = destination;
isStop = true;
}
}
public boolean isStopped() {
return isStop;
}
這是整個計划:
主程序MovingRectangle ,創建繪圖區域並提供控制:
package question;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class MovingRectangle extends JPanel {
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1L;
private Rectangle[] rectangles = new Rectangle[10];
private int count;
private int destination;
private JPanel controlPanel;
private DrawingPanel drawPanel;
private JButton stop;
private Timer timer;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Rectangles");
frame.setContentPane(new MovingRectangle());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public MovingRectangle(){
/** Imaginary horizontal line - the destination */
destination = 200;
/** Create the control panel for the left side containing the buttons */
controlPanel = new JPanel();
controlPanel.setPreferredSize(new Dimension(120, 400));
/** Create the button */
stop = new JButton("Stop");
stop.addActionListener(new ButtonListener());
controlPanel.add(stop);
/** Create a hint how to start the movement. */
JTextArea textHint = new JTextArea(5, 10);
textHint.setEditable(true);
textHint.setText("Please click on the drawing area to start the movement");
textHint.setLineWrap(true);
textHint.setWrapStyleWord(true);
controlPanel.add(textHint);
/** Add control panel to the main panel */
add(controlPanel);
/** Create the drawing area for the right side */
drawPanel = new DrawingPanel();
/** Add the drawing panel to the main panel */
add(drawPanel);
/** With a left mouse button click the timer can be started */
drawPanel.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/**
* Variable isStopped is used to track, if any
* rectangle didn't reach the destination yet
*/
boolean isStopped = true;
for(int i = 0; i < count; i++){
rectangles[i].moveUp(destination);
if (!rectangles[i].isStopped()) {
isStopped = false;
}
}
drawPanel.repaint();
/**
* With all rectangles having arrived at destination,
* the timer can be stopped
*/
if (isStopped) {
timer.stop();
}
}
});
timer.start();
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
/** Add two rectangles to the drawing area */
addRectangle(100, 30, 0, 370, new Color(255, 0, 0));
addRectangle(120, 50, 200, 350, new Color(0, 0, 255));
}
private void addRectangle(final int widthParam, final int heightParam, final int xBegin, final int yBegin, final Color color) {
/** Add a new rectangle, if array not filled yet */
if (count < rectangles.length) {
rectangles[count] = new Rectangle(widthParam, heightParam, xBegin, yBegin, color);
count++;
drawPanel.repaint();
}
}
/** This inner class is used to handle the button event. */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if (e.getSource() == stop){
timer.stop();
}
}
}
/** This inner class provides the drawing panel */
private class DrawingPanel extends JPanel{
/** The serial version uid. */
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(400, 400));
setBackground(new Color(200, 220, 255));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
/** Draw destination line */
g.setColor(new Color(150, 150, 150));
g.drawLine(0, destination, 400, destination);
/** Draw rectangles */
for(int i = 0; i < count; i++){
rectangles[i].display(g);
}
}
}
}
移動形狀的矩形類 :
package question;
import java.awt.Color;
import java.awt.Graphics;
public class Rectangle {
private int x, y, width, height;
private Color color;
private boolean isStop = false;
private int speed = 0;
public Rectangle(final int widthParam, final int heightParam, final int xBegin, final int yBegin, final Color colorParam) {
width = widthParam;
height = heightParam;
this.x = xBegin;
this.y = yBegin;
this.color = colorParam;
}
public void display(Graphics g){
g.setColor(this.color);
g.fillRect(this.x, this.y, this.width, this.height);
}
/**
* Moves the rectangle up until destination is reached
* speed is the amount of a single movement.
*/
public void moveUp(int destination) {
if (speed < 5) {
speed++;
}
y -= speed;
if (y < destination) {
y = destination;
isStop = true;
}
}
public boolean isStopped() {
return isStop;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.