netbeans中的Java程序不能正常运行,但在调试时工作得很好

时间:2016-03-01 21:08:14

标签: java swing netbeans

晚上好, 我使用netbeans过去几天一直在编写一个蛇游戏。一切都非常好,直到今天我决定放入JMenuBar以便用户可以设置难度等。这也很好但是当我把MenuItems的监听器放到程序时,程序只会在运行项目时显示一个空框架(F6) 。但是当我点击调试项目(ctrl + f5)时,游戏开始正常。删除监听器时,运行按钮也可以工作。知道为什么会这样吗? 主要课程:

    package test1;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.logging.Level;
    import java.util.logging.Logger;



    public class Test1 extends JPanel implements KeyListener, Runnable{

    private JFrame frame;
    private Schlange snake;
    private Apfel apfel;
    private boolean up;
    private boolean left;
    private boolean right;
    private boolean down;
    private int bounds;
    private boolean beschleunigen;
    private int level;
    private JMenu menu;
    private JMenu sub1;
    private JMenu sub2;
    private JMenuItem restart;
    private JMenuItem schw1, schw2, schw3, schw4, schw5;
    private JMenuBar menuBar;
    private boolean running;
    public Test1(){
    super();       
    init();
    }

    public static void main(String[] args) {

        Test1 t1 = new Test1();
    }

    private void init() {
        bounds = 300;
        //Frame
        frame = new JFrame();
        frame.setSize(bounds,bounds);
        frame.setTitle("Snake by Fluffy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100,100);
        frame.setVisible(true);
        frame.setFocusable(true);
        frame.setResizable(false);
        frame.addKeyListener(this);

        //Spiel
        snake = new Schlange(5, this);
        apfel = new Apfel(frame);
        up = false;
        left = false;
        right = false;
        down = false;
        apfel.getRnd(snake);
        level = 160;
        beschleunigen = false;
        running = true;

        //Menue
        sub1 = new JMenu("Schwierigkeit"); 
        sub2 = new JMenu("Spiel");
        schw1 = new JMenuItem("Leicht");
        schw2 = new JMenuItem("Mittel");
        schw3 = new JMenuItem("Schwer");
        schw4 = new JMenuItem("Irre");
        schw5 = new JMenuItem("Beschleunigung");
        restart = new JMenuItem("Neustart");



        //Menue Listener
        schw1.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 200;
            }
        });

        schw2.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 160;
            }
        });

        schw3.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 120;
            }
        });

        schw4.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                level = 80;
            }
        });

        schw5.addActionListener(new ActionListener() {
            @Override           
            public void actionPerformed(ActionEvent e) {
                beschleunigen = true;
            }
        });

        restart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                restart();
            }
        });



        sub1.add(schw1);
        sub1.add(schw2);
        sub1.add(schw3);
        sub1.add(schw4);
        sub1.add(schw5);    
        sub2.add(restart);
        menuBar = new JMenuBar();
        menu = new JMenu("Optionen");
        menu.add(sub1);        
        //menu.add(restart);
        menuBar.add(sub2);
        menuBar.add(menu);      

        //Ende
        frame.setJMenuBar(menuBar);
        this.setBackground(Color.green);
        frame.add(this);       
        run();      
    }

    @Override
    public void paintComponent(Graphics gr) {
        super.paintComponent(gr);   
        apfel.paintComponent(gr);
        if(snake.collision()) {
            gr.drawString("GAME OVER", 200, 200);
            running = false;
        }
        snake.paintComponent(gr);

    }

    @Override
    public void keyTyped(KeyEvent e) {        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 37:
                if(right!=true){
                left = true;
                up = false;
                down = false;
                right = false;
                }
                break;                
            case 38:
                if(down!=true){
                up = true;
                down = false;
                right = false;
                left = false;
                }
                break;               
            case 39:
                if(left!=true){
                right = true;
                left = false;
                up = false;
                down = false;
                }
                break;
            case 40:
                if(up!=true){
                down = true;
                up = false;
                right = false;
                left = false;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {        
        while (running) {
            if(up) snake.move("up");
            if(down) snake.move("down");
            if(left) snake.move("left");
            if(right) snake.move("right");
            repaint();
            if(snake.collisionApfel(apfel.getX(), apfel.getY())) {
                apfel.getRnd(snake);
                if(level > 20 && beschleunigen) level -= 10;
            }
            try {
                Thread.sleep(level);
            } catch (InterruptedException ex) {
                Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public Apfel getApfel(){
        return apfel;
    }

    public void restart(){
        snake = null;
        apfel = null;      
        snake = new Schlange(5, this);
        apfel = new Apfel(frame);
        repaint();
        up = false;
        left = false;
        right = false;
        down = false;
        apfel.getRnd(snake);
        level = 160;
        beschleunigen = false;
        running = true;
    }


}

如果需要,我也可以发布其他课程但不应该是问题...有些代码是德语的,我希望也没问题:D

1 个答案:

答案 0 :(得分:2)

frame.setVisible(true);并将其放在init方法的末尾。您在屏幕上实现的帧与EDT之间存在竞争条件