数字和循环问题

时间:2014-07-24 12:38:12

标签: java

我是编程新手,对此问题很困难。我正在尝试根据在JTextField中输入的数字构建一个墙,但数量不能超过20.我无法显示我的错误消息,也无法构建我的砖墙。有人可以帮我弄清楚我做错了吗?

public class Wall extends JApplet implements ActionListener {

JTextField enter;
Boolean submit;
JLabel bricks;
JButton build;
JPanel top;
Image zombie;
int value;

public void init() {
    setLayout(new BorderLayout());

    top = new JPanel();
    build = new JButton("AHHHHHH...ZOMBIES!");                          //button for building wall
    bricks = new JLabel("Enter between 1 & 20 rows to contruct:");
    enter = new JTextField(2);

    top.add(build);         //add zombie button
    top.add(bricks);        //add intructions
    top.add(enter);         //add text field
    add(top, BorderLayout.NORTH);


    build.addActionListener(this);


}

public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == build) {
        int value = Integer.parseInt(build.getText());
        if (value > 0 && value < 21) {
            submit = true;
            repaint();
        } else {
            submit = false;
            repaint();
        }

    }
}

public void paint(Graphics g) {
    super.paint(g);

    //add zombie image
    Image zombie = getImage(getCodeBase(), "Zombie.jpg");
    g.drawImage(zombie, 0, 45, this);

    if (submit = false) //add error message
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("TimesRoman", Font.BOLD, 40));
        g.drawString("You must enter a number between 1 & 20!", 400, 100);  //add message
    }

    int brick_width = 50;
    int brick_height = 20;
    int spacing = 1;
    int x = 0;
    while (x < 21) {
        drawBrick(g, nextInt(brick_width + spacing), nextInt(brick_height + spacing));
        x = x + getWidth() + 50;
        x = x - 25 + getWidth() + 50;
        x++;

    }

}

public void drawBrick(Graphics g, int x, int y) {
    g.setColor(new Color(150, 0, 0));
        g.fillRect(0, 635, 50, 20);
    }
}

5 个答案:

答案 0 :(得分:4)

而不是直接在top-level containers上绘图,例如JApplet,您必须使用JPanel并在其上绘画。只需覆盖paintComponent()的{​​{1}}方法即可。永远不要忘记在重写方法中调用JPanel()

示例代码:

super.paintComponent(g)

注意:

top = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); ... // your custom painting code goes here } }; submit == false是检查它的正确方法,但它仍然会导致!submit,因为您从未初始化实例变量NullPointerException

使用原始submit代替boolean来避免此类异常或正确初始化。

Boolean

答案 1 :(得分:0)

将您的if语句更改为:     if (submit == false)
这会弹出

的错误信息

答案 2 :(得分:0)

应该是if(submit == false)而不是if(submit = false)。

答案 3 :(得分:0)

两个问题:

1)您的提交变量未初始化,您应将其初始化为true / false。 如果您不想初始化它,可以使用

进行简单的尝试
if(submit!=null && !submit)

2) 其次,“=”用于分配值,其中“==”用于比较 改变

if (submit = false) //add error message
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("TimesRoman", Font.BOLD, 40));
        g.drawString("You must enter a number between 1 & 20!", 400, 100);  //add message
    }

if (submit == false) //add error message
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("TimesRoman", Font.BOLD, 40));
        g.drawString("You must enter a number between 1 & 20!", 400, 100);  //add message
    }

答案 4 :(得分:-1)

要解决您的错误显示问题,

submit = false应为submit == false

=用于作业

==用于比较

此外,初始化变量以避免空指针异常:

boolean submit = false;