3个按钮 分别显示画3个圆 可点按钮 没反映 有源程序很小的,程序编译通过, 帮我看看 成心谢谢 定义一个Button 在actionPerformed中获取ee.getSource在paint(Graphics g) 中判断再设置颜色并画。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.applet.*;import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener{ Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝"); Canvas c=new Canvas(); public void init() { this.setLayout(null); this.setSize(400,400); this.setBackground(Color.black); br.setSize(35,20);br.setLocation(10,10); bg.setSize(35,20);bg.setLocation(50,10); bb.setSize(35,20);bb.setLocation(100,10); c.setSize(300,300); c.setBackground(Color.yellow); add(br);add(bg);add(bb); add(c); br.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { c.setBackground(Color.red); repaint(); } }); bg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { c.setBackground(Color.green); repaint(); } }); bb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { c.setBackground(Color.blue); repaint(); } }); } public void actionPerformed(ActionEvent ee) { repaint(); }} if((Button)ee.getSource()==br)gg.setColor(Color.red); else if((Button)ee.getSource()==bg)gg.setColor(Color.green); else if((Button)ee.getSource()==bb)gg.setColor(Color.blue); repaint(); gg.fillOval(50,50,50,50);换成一个fill(圆的引用好了) import java.awt.*;import java.applet.*;import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener{ Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝"); Canvas c=new Canvas(); int GraphicsModel;/////////////////////////////////////////////////////////// public void init() { this.setLayout(null); this.setSize(400,400); this.setBackground(Color.black); br.setSize(35,20);br.setLocation(10,10); bg.setSize(35,20);bg.setLocation(50,10); bb.setSize(35,20);bb.setLocation(100,10); c.setSize(300,300); c.setBackground(Color.yellow); add(br);add(bg);add(bb); add(c); br.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { GraphicsModel=0; } }); bg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { GraphicsModel=1; } }); bb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { GraphicsModel=2; } }); } public void paint(Graphics g) { if(GraphicsModel==0) { g.fill()};///////////////////////////////////////////// if(GraphicsModel==1) { g.fill()};//////////////////////////////////////////// if(GraphicsModel==2) { g.fill()};/////////////////////////////////////////////// } import java.awt.*;import java.applet.*;import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener{ Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝"); Canvas c=new MyCanvas(); Graphics g=c.getGraphics(); public void init() { this.setLayout(null); this.setSize(400,400); this.setBackground(Color.black); br.setSize(35,20);br.setLocation(10,10); bg.setSize(35,20);bg.setLocation(50,10); bb.setSize(35,20);bb.setLocation(100,10); c.setSize(300,300); c.setBackground(Color.yellow); add(br);add(bg);add(bb); add(c); br.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { Graphics g=c.getGraphics(); g.setColor(Color.red); g.fillOval(50,50,50,50); repaint(); } }); bg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { Graphics g=c.getGraphics(); g.setColor(Color.green); g.fillOval(50,50,50,50); repaint(); } }); bb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { Graphics g=c.getGraphics(); g.setColor(Color.blue); g.fillOval(50,50,50,50); repaint(); } }); } public void actionPerformed(ActionEvent ee) { repaint(); }}class MyCanvas extends Canvas{ public void paint(Graphics gg) { gg.setColor(Color.red); gg.fillOval(50,50,50,50); }} 不可能啊!<HTML><applet code="Drawing_Ovel.class" width=400 height=200></applet></HTML>不要用IE,可能plug-in有问题.用appletviewer ?.html 我是用JCreator 建立的工程 运行的啊 你也运行一下看看可以不? 帮你改了改,看这个行不import java.awt.*;import java.applet.*;import java.awt.event.*;public class Cir extends Applet implements ActionListener{ Button br=new Button("红"); Button bg=new Button("绿"); Button bb=new Button("蓝"); int color=0; public void init() { this.setLayout(null); this.setSize(400,400); this.setBackground(Color.black); br.setSize(35,20);br.setLocation(10,10); bg.setSize(35,20);bg.setLocation(50,10); bb.setSize(35,20);bb.setLocation(100,10); add(br);add(bg);add(bb); br.addActionListener(this); bg.addActionListener(this); bb.addActionListener(this); } public void actionPerformed(ActionEvent ee) { if(ee.getSource()==br)color=1; else if(ee.getSource()==bg)color=2; else if(ee.getSource()==bb)color=3; repaint(); } public void paint(Graphics g) { switch (color) { case 1: g.setColor(Color.red);break; case 2: g.setColor(Color.green);break; case 3: g.setColor(Color.blue);break; default: g.setColor(Color.black); } g.fillOval(50,50,50,50); } } 两个问题:1、要获取canvas的graphics2、applet没有paint()方法,只有paint(Graphics g)两者不一样,建议区分使用,否则会画错地方的。楼主的程序相当于是自定义一个paint方法,所以要显示调用才行。import java.awt.*;import java.applet.*;import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener{Graphics gg;Button br=new Button("ºì"),bg=new Button("ÂÌ"),bb=new Button("À¶");Canvas c=new Canvas();public void init() {this.setLayout(null);this.setSize(400,400);this.setBackground(Color.black);br.setSize(35,20);br.setLocation(10,10);bg.setSize(35,20);bg.setLocation(50,10);bb.setSize(35,20);bb.setLocation(100,10);c.setSize(300,300);c.setBackground(Color.yellow);add(br);add(bg);add(bb);add(c);br.addActionListener(this);bg.addActionListener(this);bb.addActionListener(this);}public void actionPerformed(ActionEvent ee){ gg=c.getGraphics();if(ee.getSource()==br)gg.setColor(Color.red);elseif(ee.getSource()==bg)gg.setColor(Color.green);elseif(ee.getSource()==bb)gg.setColor(Color.blue);paint();}public void paint() {gg.fillOval(50,50,50,50);}} 恩 楼上的2位改的可以用 谢谢可我还有个程序有问题就可以在帮我看看吗 这个程序可以运行 但按按钮没反映 也没有debug显示 可以看看那里有问题吗?import java.awt.*;import java.applet.*;import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener{ Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝"); Canvas c=new Canvas(); int y=0; public void init() { this.setLayout(null); this.setSize(400,400); this.setBackground(Color.black); br.setSize(35,20);br.setLocation(10,10); bg.setSize(35,20);bg.setLocation(50,10); bb.setSize(35,20);bb.setLocation(100,10); c.setSize(300,300); c.setBackground(Color.white); add(br);add(bg);add(bb); add(c); br.addActionListener(this); bg.addActionListener(this); bb.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==br)y=0; if(e.getSource()==bg)y=1; if(e.getSource()==bb)y=2; } public void paint(Graphics g) { if(y==0) { g.setColor(Color.red); g.fillOval(50,50,50,50);} if(y==1) { g.setColor(Color.green); g.fillOval(50,50,50,50);} if(y==2) {g.setColor(Color.blue); g.fillOval(50,50,50,50);} }} 还是2个问题:1、现在用的是applet的grahpics,所以是在applet上画图,而canvas是加在applet上的,所以会覆盖applet所画的图像,所以不要加canvas(即将add(c)去掉)2、每次改变需要用repaint刷新一下public void actionPerformed(ActionEvent e) { if(e.getSource()==br)y=0; if(e.getSource()==bg)y=1; if(e.getSource()==bb)y=2; repaint(); //////////////////// } 如何学习JAVA容器? 关于图片的问题 java初学者 cannot resolve symbol 菜鸟询问一低级问题,40分仅赠送一人 国内的程序员你好 请教高手!!!! 一个费时的程序运行,只要进行窗口切换,就会发生费时窗口白板的情况,请问如何刷新呢? 高手请进:学到内隐类时不大明白,用过的高手能谈谈体会吗?或是很少用? 菜鸟提问:关于用JAXP的配置 有个Applet程序可以运行但按按钮没反映 在后面的专门 debug DOS窗口中按按钮时也没debug出来请帮我看看 谢谢 高份请教!急!!!!!
import java.applet.*;
import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
{
Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
Canvas c=new Canvas();
public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
c.setSize(300,300);
c.setBackground(Color.yellow);
add(br);add(bg);add(bb);
add(c);
br.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
c.setBackground(Color.red);
repaint();
}
});
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
c.setBackground(Color.green);
repaint();
}
});
bb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
c.setBackground(Color.blue);
repaint();
}
});
}
public void actionPerformed(ActionEvent ee)
{
repaint();
}
}
else
if((Button)ee.getSource()==bg)gg.setColor(Color.green);
else
if((Button)ee.getSource()==bb)gg.setColor(Color.blue);
repaint();
换成一个fill(圆的引用好了)
import java.applet.*;
import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
{
Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
Canvas c=new Canvas();
int GraphicsModel;///////////////////////////////////////////////////////////
public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
c.setSize(300,300);
c.setBackground(Color.yellow);
add(br);add(bg);add(bb);
add(c);
br.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
GraphicsModel=0;
}
});
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
GraphicsModel=1;
}
});
bb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
GraphicsModel=2;
}
});
}
public void paint(Graphics g)
{
if(GraphicsModel==0)
{ g.fill()};/////////////////////////////////////////////
if(GraphicsModel==1)
{ g.fill()};////////////////////////////////////////////
if(GraphicsModel==2)
{ g.fill()};///////////////////////////////////////////////
}
import java.applet.*;
import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
{
Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
Canvas c=new MyCanvas();
Graphics g=c.getGraphics();
public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
c.setSize(300,300);
c.setBackground(Color.yellow);
add(br);add(bg);add(bb);
add(c);
br.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
Graphics g=c.getGraphics();
g.setColor(Color.red);
g.fillOval(50,50,50,50);
repaint();
}
});
bg.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
Graphics g=c.getGraphics();
g.setColor(Color.green);
g.fillOval(50,50,50,50);
repaint();
}
});
bb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ee)
{
Graphics g=c.getGraphics();
g.setColor(Color.blue);
g.fillOval(50,50,50,50);
repaint();
}
});
}
public void actionPerformed(ActionEvent ee)
{
repaint();
}
}
class MyCanvas extends Canvas
{ public void paint(Graphics gg)
{
gg.setColor(Color.red);
gg.fillOval(50,50,50,50);
}
}
<HTML>
<applet code="Drawing_Ovel.class" width=400 height=200>
</applet>
</HTML>不要用IE,可能plug-in有问题.用appletviewer ?.html
你也运行一下看看可以不?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;public class Cir extends Applet implements ActionListener
{
Button br=new Button("红");
Button bg=new Button("绿");
Button bb=new Button("蓝");
int color=0; public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
add(br);add(bg);add(bb);
br.addActionListener(this);
bg.addActionListener(this);
bb.addActionListener(this);
} public void actionPerformed(ActionEvent ee)
{
if(ee.getSource()==br)color=1;
else
if(ee.getSource()==bg)color=2;
else
if(ee.getSource()==bb)color=3;
repaint(); } public void paint(Graphics g)
{
switch (color)
{
case 1: g.setColor(Color.red);break;
case 2: g.setColor(Color.green);break;
case 3: g.setColor(Color.blue);break;
default: g.setColor(Color.black);
}
g.fillOval(50,50,50,50);
}
}
1、要获取canvas的graphics
2、applet没有paint()方法,只有paint(Graphics g)
两者不一样,建议区分使用,否则会画错地方的。
楼主的程序相当于是自定义一个paint方法,所以要显示调用才行。import java.awt.*;
import java.applet.*;
import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
{
Graphics gg;
Button br=new Button("ºì"),bg=new Button("ÂÌ"),bb=new Button("À¶");
Canvas c=new Canvas();
public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
c.setSize(300,300);
c.setBackground(Color.yellow);
add(br);add(bg);add(bb);
add(c);
br.addActionListener(this);
bg.addActionListener(this);
bb.addActionListener(this);
}
public void actionPerformed(ActionEvent ee)
{
gg=c.getGraphics();
if(ee.getSource()==br)gg.setColor(Color.red);
else
if(ee.getSource()==bg)gg.setColor(Color.green);
else
if(ee.getSource()==bb)gg.setColor(Color.blue);
paint();}public void paint()
{
gg.fillOval(50,50,50,50);
}
}
可我还有个程序有问题就
可以在帮我看看吗
这个程序可以运行 但按按钮没反映 也没有debug显示 可以看看那里有问题吗?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;public class Drawing_Ovel extends Applet implements ActionListener
{
Button br=new Button("红"),bg=new Button("绿"),bb=new Button("蓝");
Canvas c=new Canvas();
int y=0;
public void init()
{
this.setLayout(null);
this.setSize(400,400);
this.setBackground(Color.black);
br.setSize(35,20);br.setLocation(10,10);
bg.setSize(35,20);bg.setLocation(50,10);
bb.setSize(35,20);bb.setLocation(100,10);
c.setSize(300,300);
c.setBackground(Color.white);
add(br);add(bg);add(bb);
add(c);
br.addActionListener(this);
bg.addActionListener(this);
bb.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==br)y=0;
if(e.getSource()==bg)y=1;
if(e.getSource()==bb)y=2;
}
public void paint(Graphics g)
{
if(y==0)
{ g.setColor(Color.red);
g.fillOval(50,50,50,50);}
if(y==1)
{ g.setColor(Color.green);
g.fillOval(50,50,50,50);}
if(y==2)
{g.setColor(Color.blue);
g.fillOval(50,50,50,50);}
}
}
1、现在用的是applet的grahpics,所以是在applet上画图,而canvas是加在applet上的,所以会覆盖applet所画的图像,所以不要加canvas(即将add(c)去掉)2、每次改变需要用repaint刷新一下
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==br)y=0;
if(e.getSource()==bg)y=1;
if(e.getSource()==bb)y=2; repaint(); ////////////////////
}