java怎样按下一个键,绘制一个图形
1个回答
展开全部
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询