编写Application程序:TestChange.java 要求:(1)编写一个窗口,在窗口中有一个画布和三个按钮。 (2)窗
//为啥我的程序出来了,单击按钮时不显示所画圆的颜色,但是最小化之后颜色又变为//按钮的颜色importjava.awt.*;importjava.awt.event.*...
//为啥我的程序出来了,单击按钮时不显示所画圆的颜色,但是最小化之后颜色又变为
//按钮的颜色
import java.awt.*;
import java.awt.event.*;
class TestChange1 extends Frame implements ActionListener{
Button butt1,butt2,butt3;
//Color c;
Panel pan;
myCanvas can;
TestChange1(){
setLayout(new BorderLayout());
butt1=new Button("红色");
butt2=new Button("绿色");
butt3=new Button("蓝色");
butt1.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
butt1.setBackground(Color.red);
butt2.setBackground(Color.green);
butt3.setBackground(Color.blue);
pan=new Panel();
pan.add(butt1);
pan.add(butt2);
pan.add(butt3);
can=new myCanvas();
//can.setBackground(Color.yellow);
add(pan,BorderLayout.SOUTH);
add(can,BorderLayout.CENTER);
setBounds(100,100,300,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==butt1){
can.huanc(Color.red);
}
else if(e.getSource()==butt2){
can.huanc(Color.green);
}
else if(e.getSource()==butt3){
can.huanc(Color.blue);
}
}
}
class myCanvas extends Canvas{
Color c;
/*public myCanvas(Color c){
this.c=c;
}*/
public void huanc(Color b){
this.c=b;
}
public myCanvas(){};
public void paint(Graphics g){
g.setColor(c);
g.drawOval(50,50,100,100);
}
}
class TestChange
{
public static void main(String[] args){
new TestChange1();
}
} 展开
//按钮的颜色
import java.awt.*;
import java.awt.event.*;
class TestChange1 extends Frame implements ActionListener{
Button butt1,butt2,butt3;
//Color c;
Panel pan;
myCanvas can;
TestChange1(){
setLayout(new BorderLayout());
butt1=new Button("红色");
butt2=new Button("绿色");
butt3=new Button("蓝色");
butt1.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
butt1.setBackground(Color.red);
butt2.setBackground(Color.green);
butt3.setBackground(Color.blue);
pan=new Panel();
pan.add(butt1);
pan.add(butt2);
pan.add(butt3);
can=new myCanvas();
//can.setBackground(Color.yellow);
add(pan,BorderLayout.SOUTH);
add(can,BorderLayout.CENTER);
setBounds(100,100,300,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==butt1){
can.huanc(Color.red);
}
else if(e.getSource()==butt2){
can.huanc(Color.green);
}
else if(e.getSource()==butt3){
can.huanc(Color.blue);
}
}
}
class myCanvas extends Canvas{
Color c;
/*public myCanvas(Color c){
this.c=c;
}*/
public void huanc(Color b){
this.c=b;
}
public myCanvas(){};
public void paint(Graphics g){
g.setColor(c);
g.drawOval(50,50,100,100);
}
}
class TestChange
{
public static void main(String[] args){
new TestChange1();
}
} 展开
推荐于2017-10-06
展开全部
//更改设置后记得repait(),类似刷新
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butt1) {
can.huanc(Color.red);
can.repaint();//加上这个
} else if (e.getSource() == butt2) {
can.huanc(Color.green);
can.repaint();//加上这个
} else if (e.getSource() == butt3) {
can.huanc(Color.blue);
can.repaint();//加上这个
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butt1) {
can.huanc(Color.red);
can.repaint();//加上这个
} else if (e.getSource() == butt2) {
can.huanc(Color.green);
can.repaint();//加上这个
} else if (e.getSource() == butt3) {
can.huanc(Color.blue);
can.repaint();//加上这个
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询