「矩形代码java」html矩形代码

博主:adminadmin 2023-01-02 13:51:05 929

本篇文章给大家谈谈矩形代码java,以及html矩形代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中做一个按钮,点击按钮后画一个矩形的代码怎么写?

兄弟帮你写了一个:

import java.awt.Button;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Random;

public class Print {

public static void main(String[] args) {

new Te();

}

}

class Te extends Frame implements ActionListener {

Color cc = Color.red;

int x = -20, y = -50;

Random r = new Random();

public Te() {

this.setLayout(null);

Button b = new Button("画圆");

this.add(b);

b.setBounds(30,30,50,50);

b.addActionListener(this);

this.addWindowListener(new WindowAdapter () {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

this.setBounds(200,200,500,400);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

this.cc = Color.red;

this.x = r.nextInt(400);

do {

int x1 = r.nextInt(300);

this.y = x1;

} while (this.y 50);

this.repaint();

}

@Override

public void paint(Graphics g) {

Color c = g.getColor();

g.setColor(cc);

g.drawRect(x,y,50,50);

g.setColor(c);

}

}

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

System.out.println(rect.getArea());

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}

Java 编写一个矩形类Rect

public class Rect {

private double length;//矩形的长

private double width;//矩形的宽

public Rect() {}//默认构造器

public Rect(double length,double width) {

this.length = length;

this.width = width;

}

/**

* 取得矩形的面积

* */

public double getArea(){

return this.getLength() * this.getWidth();

}

/**

* 取得矩形的周长

* */

public double getPerimeter(){

return (this.getLength() + this.getWidth()) * 2;

}

/**

* 取得矩形的面积,需传入矩形长与宽

* */

public double getArea(double length,double width){

return length * width;

}

/**

* 取得矩形的周长,需传入矩形长与宽

* */

public double getPerimeter(double length,double width){

return (length + width) * 2;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

}

关于矩形代码java和html矩形代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。