Java GUI没有打开...在后台运行而不是...其他答案不起作用

时间:2016-07-20 04:49:31

标签: java swing nullpointerexception

每次运行我的程序时,当我的变量都没有被声明为null时,它总是显示NullPointerException ...任何想法为什么?

public void BrownTank() {
        while (true) {
            int speed = 2;
            Random randomGenerator = new Random();
            int Movement = randomGenerator.nextInt(4) + 1;
            if (Movement == 1) {
                jLabel2.setBounds(jLabel2.getX(), jLabel2.getY() - speed, jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 2) {
                jLabel2.setBounds(jLabel2.getX(), jLabel2.getY() + speed, jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 3) {
                jLabel2.setBounds(jLabel2.getX() - speed, jLabel2.getY(), jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 4) {
                jLabel2.setBounds(jLabel2.getX() + speed, jLabel2.getY(), jLabel2.getWidth(), jLabel2.getHeight());
            }
        }
    }

编辑: 这是完整的代码

package tanks;

import java.util.Random;

public class Tanks extends javax.swing.JFrame {

    public Tanks() {
        BrownTank();
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tanks!");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("PMingLiU", 1, 16)); // NOI18N
        jLabel1.setText("◻");

        jLabel2.setFont(new java.awt.Font("PMingLiU", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 51, 0));
        jLabel2.setText("◻");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(244, 244, 244)
                        .addComponent(jLabel1)))
                .addContainerGap(315, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 281, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(89, 89, 89))
        );

        pack();
    }// </editor-fold>                        

    private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
        int speed = 3;
        if (evt.getKeyChar() == 'w') {
            jLabel1.setBounds(jLabel1.getX(), jLabel1.getY() - speed, jLabel1.getWidth(), jLabel1.getHeight());
        }
        if (evt.getKeyChar() == 's') {
            jLabel1.setBounds(jLabel1.getX(), jLabel1.getY() + speed, jLabel1.getWidth(), jLabel1.getHeight());
        }
        if (evt.getKeyChar() == 'a') {
            jLabel1.setBounds(jLabel1.getX() - speed, jLabel1.getY(), jLabel1.getWidth(), jLabel1.getHeight());
        }
        if (evt.getKeyChar() == 'd') {
            jLabel1.setBounds(jLabel1.getX() + speed, jLabel1.getY(), jLabel1.getWidth(), jLabel1.getHeight());
        }
    }                               

    public void PlayerBullet() {

    }

    public void BrownTank() {
        while (true) {
            int speed = 2;
            Random randomGenerator = new Random();
            int Movement = randomGenerator.nextInt(4) + 1;
            if (Movement == 1) {
                jLabel2.setBounds(jLabel2.getX(), jLabel2.getY() - speed, jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 2) {
                jLabel2.setBounds(jLabel2.getX(), jLabel2.getY() + speed, jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 3) {
                jLabel2.setBounds(jLabel2.getX() - speed, jLabel2.getY(), jLabel2.getWidth(), jLabel2.getHeight());
            }

            if (Movement == 4) {
                jLabel2.setBounds(jLabel2.getX() + speed, jLabel2.getY(), jLabel2.getWidth(), jLabel2.getHeight());
            }
        }
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tanks().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

编辑我的程序以获得答案... 当我尝试运行它时,它不会打开GUI并在后台运行。

1 个答案:

答案 0 :(得分:1)

您应该初始化jLabel2

因此,在致电BrownTank()之前,请致电initComponents()