Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请解释为什么我按下按钮2时什么都没有发生,但在下面的Java代码中,我按下按钮1时得到了预期的结果_Java - Fatal编程技术网

请解释为什么我按下按钮2时什么都没有发生,但在下面的Java代码中,我按下按钮1时得到了预期的结果

请解释为什么我按下按钮2时什么都没有发生,但在下面的Java代码中,我按下按钮1时得到了预期的结果,java,Java,我的代码只有在按下“按钮1”时才能工作,但当我按下“按钮2”时,什么也没有发生。 我知道我的paint()方法和if…else语句有问题,但不知道如何修复它 多谢各位 import java.applet.*; import java.awt.*; import java.awt.event.*; public class AnAppletWithButtons extends Applet implements ActionListener { Button button1, but

我的代码只有在按下“按钮1”时才能工作,但当我按下“按钮2”时,什么也没有发生。 我知道我的
paint()
方法和
if…else
语句有问题,但不知道如何修复它

多谢各位

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithButtons extends Applet implements ActionListener
{
    Button button1, button2;
    int r, g , b;
    Color color;
    boolean drawLine = false;
    boolean fillOval = false;

    public void init()
    {
        String parmStringRED =  getParameter("red");
        r = Integer.parseInt(parmStringRED);
        String parmStringGREEN =  getParameter("green");
        g = Integer.parseInt(parmStringGREEN);
        String parmStringBLUE =  getParameter("blue");
        b = Integer.parseInt(parmStringBLUE);
        color = new Color (r,g,b);
        button1 = new Button("Button 1");
        add(button1);
        button1.addActionListener(this);

        button2 = new Button("Button 2");
        add(button2);
        button2.addActionListener(this);
        button1.setForeground(color);
        button2.setForeground(color);

        button1.setBackground(Color.yellow);
        button2.setBackground(Color.yellow);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == button1) {
            System.out.println("Button 1 was pressed");
            drawLine = true;
        } else if (e.getSource() == button2) {
            System.out.println("Button 2 was pressed");
            fillOval = true;
        }
        repaint();
    }
    public void paint(Graphics g)
    {
        super.paint(g);   // for the background
        if(drawLine){
            System.out.println(drawLine);
            g.drawLine(0, 0, 400, 400);
        }
        else if(fillOval){
            System.out.println(fillOval);
            g.fillOval(10, 10, 390, 390);
        }

    }
}

您从不重置
drawLine
fillOval
的值。一旦将
drawLine
设置为
true
paint
将始终绘制一条线,因为您首先检查该布尔值


如果没有其他方法,您可以在
paint
方法中将两个变量重置为
false
,尽管还有其他方法可以解决此问题。

paint(Graphics g)
中的
else If
拆分为两个
If
s

更改按钮单击事件处理程序,如下所示:

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == button1) {
        System.out.println("Button 1 was pressed");
        drawLine = true;
        fillOval = false;
    } else if (e.getSource() == button2) {
        System.out.println("Button 2 was pressed");
        drawLine = false;
        fillOval = true;
    }
    repaint();
}

这似乎是家庭作业。是作业吗?你拼错了repaint()吗?如果是这样的话,这本身就是个问题:-)@NickCoelius我想这就是你拼写
repaint()
,不是吗?此外,它显然有效,只是没有达到预期的效果。对不起,是的。这就是如何拼写repaint()。但由于我假设他的函数是paint(),我有点假设他可能无意中调用了一个不存在的函数。@Mikelloo记得接受答案,以便将来的访问者知道问题所在!这也会遇到同样的问题,但只要按下其中一个按钮,它就会一直执行该操作。嗯,如果,则分解
,否则当
drawLine
为真时,这两个语句将分别进行计算,而不是短路。当然,一旦他按下两个按钮,就没有办法重置它。