编写一个绘图java 小程序,用户点击某一按钮后,程序绘制相应的图形(如点击"画圆",则绘制圆形);
这是我写的部分错误代码:importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassT2...
这是我写的部分错误代码:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class T2 extends Applet implements ActionListener
{
Button line,rect,oval;//直线,矩形,椭圆;
Label label3,label4;
public void init()
{
setLayout(null);//空布局
setBackground(Color.white);
label3=new Label("图形");
label4=new Label("绘图区");
add(label3);
add(label4);
label3.setBounds(20, 20, 80, 20);
label4.setBounds(350, 20, 80, 20);
line=new Button("直线");
rect=new Button("矩形");
oval=new Button("椭圆");
add(line);
add(rect);
add(oval);
line.setBounds(20, 60, 60, 20);
rect.setBounds(20, 90, 60, 20);
oval.setBounds(20, 120, 60, 20);
line.addActionListener(this);
rect.addActionListener(this);
oval.addActionListener(this);
validate();
setVisible(true);
}
public void paint(Graphics g)
{
g.drawLine(260,80,360,60);
g.drawRect(270, 85, 65, 70);
g.drawOval(350, 150, 80, 68);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==line)
repaint();
}
}
谢谢!! 展开
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class T2 extends Applet implements ActionListener
{
Button line,rect,oval;//直线,矩形,椭圆;
Label label3,label4;
public void init()
{
setLayout(null);//空布局
setBackground(Color.white);
label3=new Label("图形");
label4=new Label("绘图区");
add(label3);
add(label4);
label3.setBounds(20, 20, 80, 20);
label4.setBounds(350, 20, 80, 20);
line=new Button("直线");
rect=new Button("矩形");
oval=new Button("椭圆");
add(line);
add(rect);
add(oval);
line.setBounds(20, 60, 60, 20);
rect.setBounds(20, 90, 60, 20);
oval.setBounds(20, 120, 60, 20);
line.addActionListener(this);
rect.addActionListener(this);
oval.addActionListener(this);
validate();
setVisible(true);
}
public void paint(Graphics g)
{
g.drawLine(260,80,360,60);
g.drawRect(270, 85, 65, 70);
g.drawOval(350, 150, 80, 68);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==line)
repaint();
}
}
谢谢!! 展开
展开全部
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tester extends Applet implements ActionListener
{
Button btn1,btn2,btn3,btn4;
public void init(){
setLayout(null);
btn1=new Button("绘制直线");
btn2=new Button("绘制矩形");
btn3=new Button("绘制圆");
add(btn1);
add(btn2);
add(btn3);
btn1.setBounds(10, 60, 60, 20);
btn2.setBounds(10, 90, 60, 20);
btn3.setBounds(10, 120, 60, 20);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
validate();
setVisible(true);
}
public void start(){
}
public void actionPerformed(ActionEvent e)
{
Graphics g=this.getGraphics();
if(e.getSource()==btn1)
{
g.drawLine(200, 100, 300, 200);
}
else if(e.getSource()==btn2)
{
g.drawRect(200, 250, 200, 200);
}
else
{
g.drawArc(200, 500, 200, 200, 0, 360);
}
}
}
import java.awt.*;
import java.awt.event.*;
public class Tester extends Applet implements ActionListener
{
Button btn1,btn2,btn3,btn4;
public void init(){
setLayout(null);
btn1=new Button("绘制直线");
btn2=new Button("绘制矩形");
btn3=new Button("绘制圆");
add(btn1);
add(btn2);
add(btn3);
btn1.setBounds(10, 60, 60, 20);
btn2.setBounds(10, 90, 60, 20);
btn3.setBounds(10, 120, 60, 20);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
validate();
setVisible(true);
}
public void start(){
}
public void actionPerformed(ActionEvent e)
{
Graphics g=this.getGraphics();
if(e.getSource()==btn1)
{
g.drawLine(200, 100, 300, 200);
}
else if(e.getSource()==btn2)
{
g.drawRect(200, 250, 200, 200);
}
else
{
g.drawArc(200, 500, 200, 200, 0, 360);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询