运行:点按钮不显示彩色圆,不知哪儿搞错了?请各位帮忙看下,先谢谢:)
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends Canvas{
int red,green,blue,x,y,r;
MyCanvas(){
setBackground(Color.white);
}
public void setRed(int r){
red=r;
}
public void setGreen(int g){
green=g;
}
public void setBlue(int b){
blue=b;
}
public void setPosition(int x,int y){
this.x=x;
this.y=y;
}
public void setRadius(int r){
this.r=r;
}
public void paint(Graphics g){
if(red==1){
g.setColor(Color.red);
}
else if(green==1){
g.setColor(Color.green);
}
else if(blue==1){
g.setColor(Color.blue);
}
g.fillOval(x, y, 2*r, 2*r);
}
}
class MyFrame extends Frame implements ActionListener{
Button b1,b2,b3;
MyCanvas c;
MyFrame(String s){
super(s);
b1=new Button("红");
b1.setBounds(10, 30, 70, 25);
b2=new Button("绿");
b2.setBounds(80, 30, 70, 25);
b3=new Button("蓝");
b3.setBounds(150, 30, 70, 25);
c=new MyCanvas();
add(b1);
add(b2);
add(b3);
add(c);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setLayout(null);
setBounds(100,100,360,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
int w=c.getBounds().width;
int h=c.getBounds().height;
int m=Math.min(w,h);
c.setRadius(m/6);
if(e.getSource().equals(b1)){
if(c!=null){
c.setRed(1);
c.setGreen(0);
c.setBlue(0);
c.setPosition(w/3, h/3);
c.repaint();
}
}
else if(e.getSource().equals(b2)){
if(c!=null){
c.setRed(0);
c.setGreen(1);
c.setBlue(0);
c.setPosition(w/3, h/3);
c.repaint();
}
}
else if(e.getSource().equals(b3)){
if(c!=null){
c.setRed(0);
c.setGreen(0);
c.setBlue(1);
c.setPosition(w/3, h/3);
c.repaint();
}
}
}
}
public class TestButton {
public static void main(String[] args){
MyFrame myFrame=new MyFrame("改变颜色");
myFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends Canvas{
int red,green,blue,x,y,r;
MyCanvas(){
setBackground(Color.white);
}
public void setRed(int r){
red=r;
}
public void setGreen(int g){
green=g;
}
public void setBlue(int b){
blue=b;
}
public void setPosition(int x,int y){
this.x=x;
this.y=y;
}
public void setRadius(int r){
this.r=r;
}
public void paint(Graphics g){
if(red==1){
g.setColor(Color.red);
}
else if(green==1){
g.setColor(Color.green);
}
else if(blue==1){
g.setColor(Color.blue);
}
g.fillOval(x, y, 2*r, 2*r);
}
}
class MyFrame extends Frame implements ActionListener{
Button b1,b2,b3;
MyCanvas c;
MyFrame(String s){
super(s);
b1=new Button("红");
b1.setBounds(10, 30, 70, 25);
b2=new Button("绿");
b2.setBounds(80, 30, 70, 25);
b3=new Button("蓝");
b3.setBounds(150, 30, 70, 25);
c=new MyCanvas();
add(b1);
add(b2);
add(b3);
add(c);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setLayout(null);
setBounds(100,100,360,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
int w=c.getBounds().width;
int h=c.getBounds().height;
int m=Math.min(w,h);
c.setRadius(m/6);
if(e.getSource().equals(b1)){
if(c!=null){
c.setRed(1);
c.setGreen(0);
c.setBlue(0);
c.setPosition(w/3, h/3);
c.repaint();
}
}
else if(e.getSource().equals(b2)){
if(c!=null){
c.setRed(0);
c.setGreen(1);
c.setBlue(0);
c.setPosition(w/3, h/3);
c.repaint();
}
}
else if(e.getSource().equals(b3)){
if(c!=null){
c.setRed(0);
c.setGreen(0);
c.setBlue(1);
c.setPosition(w/3, h/3);
c.repaint();
}
}
}
}
public class TestButton {
public static void main(String[] args){
MyFrame myFrame=new MyFrame("改变颜色");
myFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货