编写一个Applet(Java 小程序) a) 画出给定半径的圈,并求出此圆圈儿的面积,打印在画面上 b) 画出给定长度
1个回答
展开全部
我也是刚学Applet的,下面是我写的,有什么问题可以提出来,大家一起研究
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
public class oval extends Applet{
private TextField tf1;
private JLabel jb1;
private JLabel jb2;
private int x,y;
Button b1;
Button b2;
private Graphics g;
private String i;
Thread t = new Thread();
public void init(){
g = this.getGraphics();
jb1=new JLabel();
jb1.setText("半径(cm)=");
add(jb1);
tf1=new TextField(2);
add(tf1);
setSize(700, 600);
b1 = new Button("输出");
add(b1);
b1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
i=tf1.getText();
g.setColor(Color.WHITE);
g.fillRect(10, 25, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("面积="+Integer.parseInt(i)*Integer.parseInt(i)*3.14,50,40);
g.setColor(Color.BLACK);
g.drawString("圆:", 50, 160);
g.drawOval(60, 160,Integer.parseInt(i),Integer.parseInt(i));
g.drawString("线:", 50, 60);
g.drawLine(100, 60, 100+Integer.parseInt(i), 60);
}
});
}
}
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
public class oval extends Applet{
private TextField tf1;
private JLabel jb1;
private JLabel jb2;
private int x,y;
Button b1;
Button b2;
private Graphics g;
private String i;
Thread t = new Thread();
public void init(){
g = this.getGraphics();
jb1=new JLabel();
jb1.setText("半径(cm)=");
add(jb1);
tf1=new TextField(2);
add(tf1);
setSize(700, 600);
b1 = new Button("输出");
add(b1);
b1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
i=tf1.getText();
g.setColor(Color.WHITE);
g.fillRect(10, 25, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("面积="+Integer.parseInt(i)*Integer.parseInt(i)*3.14,50,40);
g.setColor(Color.BLACK);
g.drawString("圆:", 50, 160);
g.drawOval(60, 160,Integer.parseInt(i),Integer.parseInt(i));
g.drawString("线:", 50, 60);
g.drawLine(100, 60, 100+Integer.parseInt(i), 60);
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |