程序的作用是按下两个不同的按钮,在屏幕上画出圆和方形,该程序在另外的机器上运行成功,但是在我这里出现了奇怪的问题,不能进入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();
}
}
环境:
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();
}
}
解决方案 »
- 欲构造一个ArrayList类实例,此类继承List接口,下列哪个是对的
- 为什么输出的都是0呢?
- 关于自己做的小东西,有的地方不理解。。。
- jsp无法使用bean的问题 等到解决问题为止!!!!!
- =====有奖竟猜=====乌迪内斯Vs罗马===== 截止时间:4月10日-10点30分
- 如何在TextArea上加KeyListener事件啊
- 我想学Collection,大家知道有什么连接地址吗?
- jfc的简单问题
- java的运行速度真是慢啊!
- java初学者,菜鸟问题,数组定义有问题吗?我编译不过去?各位大哥帮帮忙?
- ①这样的特殊字符转化为JIS码为什么不是标准的JIS码
- java虚拟机负载问题
//</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();
}
}