在循环期间退出程序

时间:2013-07-20 13:40:35

标签: java loops applet exit

我最近在玩Java,我制作了一个循环运行的程序。当我尝试击中角落里的X来关闭它时,什么也没发生。我尝试添加标准的mouselistener代码以使其退出,但它不会通过循环。该程序只是一个围绕屏幕弹跳的小方块:

package mainPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.*;

public class MainClass extends JApplet{
    public static boolean isRunning = true;
    public static void startStop(){
        if(isRunning){
            isRunning = false;
        } else {
            isRunning = true;
        }
    }
    /**
     * Eclipse kept yelling at me.
     */
    private static final long serialVersionUID = -3997547128895841169L;
    final static Color bg = Color.white;
    final static Color fg = Color.black;
            /*
     * The direction of the box; 1 is up-right, 2 is down-right, etc.
     */
    public static byte direction = 1;
            /*
             *The X and Y values start somewhat randomly.
             */
    public static short x = (short) (Math.random() * 150);
    public static short y = (short) (Math.random() * 150);
    public void init() {

        }
    public static void moveBall(){
        if(x >= 585){
            if(direction == 1){
                direction = 4;
            } else {
                direction = 3;
            }
        } else if(x <= 0){
            if(direction == 3){
                direction = 2;
            } else {
                direction = 1;
            }
        } else if(y <= 0){
            if(direction == 1){
                direction = 2;
            } else {
                direction = 3;
            }
        } else if(y >= 365){
            if(direction == 3){
                direction = 4;
            } else {
                direction = 1;
            }
        }
        if(direction == 1){
            x++; y--;
        } else if(direction == 2){ x++; y++;
        } else if(direction == 3){ x--; y++;
        } else if(direction == 4){ x--; y--;
        } else { System.out.println(direction); System.exit(5);}
    }
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        Color color = Color.black;
        Rectangle2D.Double a = new Rectangle2D.Double(250, 250, 10, 10);
        Rectangle2D.Double fill = new Rectangle2D.Double(0, 0, 600, 400);
        g2.setPaint(color);
        g2.fill(fill);
        while(isRunning){
            g2.setPaint(Color.blue);
            g2.fill(a);
            try {
                Thread.sleep(15L);
            } catch (InterruptedException e) {
                System.out.println("Interrupted!");
                e.printStackTrace();
            }
            g2.setPaint(Color.black);
            g2.fill(a);
            a.setRect(x, y, 10, 10);
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame jframe = new JFrame("Test");
        jframe.setVisible(true);
        jframe.setBounds(250, 250, 600, 400);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new MainClass();       
        jframe.getContentPane().add("Center", applet);
        while(isRunning){
            moveBall();
            try {
                Thread.sleep(15L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("Interrupted.");
            }
        }
    }
}

[编辑]我忘了提到当我将它导出到可执行的JAR文件然后运行它时,我不得不打开任务管理器,然后告诉我它没有响应,以便关闭它。

是否有重写这个以便关闭?

2 个答案:

答案 0 :(得分:0)

我认为你不是在听近距离事件。试试这个:

class WindowEventHandler extends WindowAdapter {
 public void windowClosing(WindowEvent evt) {
  isRunnig = false;
 }
}

jframe.addWindowListener(new WindowEventHandler());

答案 1 :(得分:0)

Thread.sleep和Swing EDT中的主循环是个坏主意。 使用javax.swing.Timer