程序的作用是按下两个不同的按钮,在屏幕上画出圆和方形,该程序在另外的机器上运行成功,但是在我这里出现了奇怪的问题,不能进入actionPerformed中的条件判断,请问这是什么原因?
环境:
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode)程序代码:import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;public class MyButton extends Applet implements ActionListener
{
private Button ovalButton;
private Button rectButton;
private boolean b;//用b来判断按钮状态

public void init()
{
Button ovalButton=new Button("Oval");
ovalButton.addActionListener(this);
add(ovalButton);

Button rectButton=new Button("Rectangle");
rectButton.addActionListener(this);
add(rectButton);
}

public void paint(Graphics g)
{
Random r=new Random();
int x=Math.abs(r.nextInt()%100);
int y=Math.abs(r.nextInt()%100);
int width=Math.abs(r.nextInt()%200);
int height=Math.abs(r.nextInt()%200);
Color color=new Color(r.nextInt());
g.setColor(color); //输出信息到控制台便于调试找错
System.out.println("x="+x+", y="+y+", width="+width+", height="+height+", color is "+color);

if(b==true)
{
g.fillOval(x,y,width,height);
System.out.println("b is true to draw oval : "+b);
}
else
{
g.fillRect(x,y,width,height);
System.out.println("b is false to draw oval : "+b);
}

System.out.println("Boolean b is "+b);
}

public void actionPerformed(ActionEvent ae)
{
System.out.println("Button wait press.");
//下面两个条件语句不能进入,请问为什么?
if(ovalButton == ae.getSource())
{
System.out.println("OvalButton pressed.");
b=true;
repaint();
}

if(rectButton == ae.getSource())
{
System.out.println("RectangleButton pressed.");
b=false;
repaint();
}
//repaint();
}
}

解决方案 »

  1.   

    //<applet code=MyButton.class width=400 height=500>
    //</applet>import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.*;public class MyButton extends Applet implements ActionListener
    {
    private Button ovalButton =new Button("Oval");
    private Button rectButton =new Button("Rectangle");
    private boolean b;//用b来判断按钮状态

    public void init()
    {
    //Button ovalButton=new Button("Oval");
    ovalButton.addActionListener(this);
    add(ovalButton);

    //Button rectButton=new Button("Rectangle");
    rectButton.addActionListener(this);
    add(rectButton);
    }

    public void paint(Graphics g)
    {
    Random r=new Random();
    int x=Math.abs(r.nextInt()%100);
    int y=Math.abs(r.nextInt()%100);
    int width=Math.abs(r.nextInt()%200);
    int height=Math.abs(r.nextInt()%200);
    Color color=new Color(r.nextInt());
    g.setColor(color); //输出信息到控制台便于调试找错
    System.out.println("x="+x+", y="+y+", width="+width+", height="+height+", color is "+color);

    if(b==true)
    {
    g.fillOval(x,y,width,height);
    System.out.println("b is true to draw oval : "+b);
    }
    else
    {
    g.fillRect(x,y,width,height);
    System.out.println("b is false to draw oval : "+b);
    }

    System.out.println("Boolean b is "+b);
    }

    public void actionPerformed(ActionEvent ae)
    {
                      // 查看此句,你就知道了.
    System.out.println("Button wait press."+ovalButton+":"+ae.getSource());
    //下面两个条件语句不能进入,请问为什么?
    if(ovalButton == ae.getSource())
    {
    System.out.println("OvalButton pressed.");
    b=true;
    repaint();
    }

    if(rectButton == ae.getSource())
    {
    System.out.println("RectangleButton pressed.");
    b=false;
    repaint();
    }
    //repaint();
    }
    }