怎样用java编写图形界面的Application程序?

怎样用java编写图形界面的Application程序?,第1张

java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码租碰,然后自己再根据需要修改,如:

package mainFrame

import java.awt.EventQueue

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JCheckBox

import javax.swing.JFrame。

Java是一门面向对象御型消编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复镇知杂的编程。

public class Test013 {

/**

* 编写液樱一个图形类MyGraphic。 1)它有两个基本属性:图形线条的颜色String lineColor和图形的填充颜色String

* fillColor。 2)设计矩形类CRectangle,有属性double rLong和宽double rWidth, 使用方法 float

* calCircum()可以返回矩形的周长,使用方法float calSquare()可以返回矩形的面积。

* 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。 3)设计圆形类CCircle,定义属性:半径double

* radius,可以通过同名方法计算周长和面积。 编写方法show(),显示图形的线条颜色和填充颜色,输出面积和方法。

* 4)编写出应用程序对CRectangle类和CCircle类进行验闹吵丛证。 完成上述要求即可

*/

public static void main(String[] args) {

MyGraphic rectangle = new CRectangle(10, 5)

rectangle.setFillColor("紫色")//设定矩形填充颜色

rectangle.setLineColor("白色")//设定矩形线条颜色

rectangle.show()

System.out.println("矩形周长 = " + rectangle.calCircum())

System.out.println("矩形面积 = " + rectangle.calSquare())

MyGraphic circle = new CCircle(3)

circle.setFillColor("红色")

circle.setLineColor("黄色")

circle.show()

System.out.println("园形周长 = " + circle.calCircum())

System.out.println("园形面积 = " + circle.calSquare())

}

}

/**

* 图形类

*

*/

abstract class MyGraphic {

private String lineColor// 图形线条的颜色

private String fillColor// 图形的填充颜色

public String getLineColor() {

return lineColor

}

public void setLineColor(String lineColor) {

this.lineColor = lineColor

}

public String getFillColor() {

return fillColor

}

public void setFillColor(String fillColor) {

this.fillColor = fillColor

}

public MyGraphic(String lineColor, String fillColor) {

this.lineColor = lineColor

this.fillColor = fillColor

}

public MyGraphic() {

}

/**

* 显示图碰明形的颜色

*/

public abstract void show()

/**

* 计算图形的周长

*/

public abstract float calCircum()

/**

* 计算图形的面积

*/

public abstract float calSquare()

}

/**

* 矩形类

*

*/

class CRectangle extends MyGraphic {

private double rLong// 长

private double rWidth// 宽

/**

* 通过构造函数为图形的属性赋值

*

* @param rLong

* @param rWidth

*/

public CRectangle(double rLong, double rWidth) {

this.rLong = rLong

this.rWidth = rWidth

}

/**

* @return 矩形的周长

*/

@Override

public float calCircum() {

return (float) (2 * (rLong + rWidth))

}

/**

* @return 矩形的面积

*/

@Override

public float calSquare() {

return (float) (rLong * rWidth)

}

@Override

public void show() {

System.out.println("矩形线条的颜色: " + super.getLineColor())

System.out.println("矩形填充颜色: " + super.getFillColor())

}

public double getrLong() {

return rLong

}

public void setrLong(double rLong) {

this.rLong = rLong

}

public double getrWidth() {

return rWidth

}

public void setrWidth(double rWidth) {

this.rWidth = rWidth

}

}

/**

* 圆形类

*

*/

class CCircle extends MyGraphic {

private double radius// 圆形半径

public CCircle(double radius) {

this.radius = radius

}

/**

* @return 圆形的周长

*/

@Override

public float calCircum() {

return (float) (2 * Math.PI * radius)

}

/**

* @return 圆形的面积

*/

@Override

public float calSquare() {

return (float) (Math.PI * radius * radius)

}

@Override

public void show() {

System.out.println("圆形线条的颜色: " + super.getLineColor())

System.out.println("圆形填充颜色: " + super.getFillColor())

}

public double getRadius() {

return radius

}

public void setRadius(double radius) {

this.radius = radius

}

}

1. 抽象形状类

package com

//抽象的让衫形状类

public abstract class Shape{

}

2. 显示接口

package com

//接口

public interface IDisplay{

void display() //显示图形的基本信息

double getArea() //计算面积

double getGirth() //计算周长

}

3. 三角形类

package com.tri

import com.*

//三角形类

public class Triangle extends Shape implements IDisplay{

protected double a

protected double b

protected double c

public Triangle(double a, double b, double c){

this.a = a

this.b = b

this.c = c

}

@Override

public double getArea() {

double s = (a + b + c) / 2

return Math.sqrt(s*(s-a)*(s-b)*(s-c))

}

@Override

public double getGirth() {

return this.a + this.b + this.c

}

@Override

public void display() {

System.out.println("三角昌闭形")

System.out.println("边长:" + a + ", " + b + ", " + c)

}

}

4. 矩形类

package com.rec

import com.*

/耐滑裂/矩形类

public class Rectangle extends Shape implements IDisplay {

protected double width

protected double height

public Rectangle(double width, double height){

this.width = width

this.height = height

}

@Override

public double getArea() {

return this.width * this.height

}

@Override

public double getGirth() {

return 2 * ( this.width + this.height)

}

@Override

public void display() {

System.out.println("矩形")

System.out.println("宽:" + this.width + ", 高:" + this.height)

}

}

5. 圆类

package com.cir

import com.*

//圆类

public class Circle extends Shape implements IDisplay {

protected double radius

public Circle(double radius){

this.radius = radius

}

@Override

public double getArea() {

return Math.PI * this.radius * this.radius

}

@Override

public double getGirth() {

return 2 * Math.PI * this.radius

}

@Override

public void display() {

System.out.println("圆")

System.out.println("半径:" + this.radius)

}

}

6. 正多边形

package com.mul

import com.*

//正多边形类

public class MulSide extends Shape implements IDisplay {

protected double side //边长

protected int n //边数

public MulSide(double side, int n){

this.side = side

this.n = n

}

@Override

public double getArea() {

return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4

}

@Override

public double getGirth() {

return this.side * this.n

}

@Override

public void display() {

System.out.println("正多边形")

System.out.println("连长:" + this.side + ", 边数:" + this.n)

}

}

7. 主类(测试类)

package com

import java.util.Scanner

import com.cir.Circle

import com.mul.MulSide

import com.rec.Rectangle

import com.tri.Triangle

public class Test22 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in)

double a, b, c

double width, height

double radius

double side

int n

IDisplay s

System.out.println("请输入三角形的基本信息")

System.out.print("边长1:")

a = scan.nextDouble()

System.out.print("边长2:")

b = scan.nextDouble()

System.out.print("边长3:")

c = scan.nextDouble()

s = new Triangle(a, b, c)

s.display()

System.out.println("三角形的面积:" + s.getArea())

System.out.println("三角形的周长:" + s.getGirth())

System.out.println("请输入矩形的基本信息")

System.out.print("宽:")

width = scan.nextDouble()

System.out.print("高:")

height = scan.nextDouble()

s = new Rectangle(width, height)

s.display()

System.out.println("矩形的面积:" + s.getArea())

System.out.println("矩的周长:" + s.getGirth())

System.out.println("请输入圆的基本信息")

System.out.print("半径:")

radius = scan.nextDouble()

s = new Circle(radius)

s.display()

System.out.println("圆的面积:" + s.getArea())

System.out.println("圆的周长:" + s.getGirth())

System.out.println("请输入正多边形的基本信息")

System.out.print("边长:")

side = scan.nextDouble()

System.out.print("边数:")

n = scan.nextInt()

s = new MulSide(side, n)

s.display()

System.out.println("正多边形的面积:" + s.getArea())

System.out.println("正多边形的周长:" + s.getGirth())

}

}

运行测试:

请输入三角形的基本信息

边长1:3

边长2:4

边长3:5

三角形

边长:3.0, 4.0, 5.0

三角形的面积:6.0

三角形的周长:12.0

请输入矩形的基本信息

宽:3

高:4

矩形

宽:3.0, 高:4.0

矩形的面积:12.0

矩的周长:14.0

请输入圆的基本信息

半径:2

半径:2.0

圆的面积:12.566370614359172

圆的周长:12.566370614359172

请输入正多边形的基本信息

边长:2

边数:6

正多边形

连长:2.0, 边数:6

正多边形的面积:10.39230484541326

正多边形的周长:12.0


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/12482800.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存