「图形java编码」编程java语言代码图片
本篇文章给大家谈谈图形java编码,以及编程java语言代码图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java编写图形抽象类(Shape)
我来写一下吧:
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB lengthC) || (lengthA + lengthC lengthB) || (lengthB+lengthC lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}
java设计的图形界面,为啥中文显示为乱码?
这是编码问题,可以做如下设置: 右键单击这个项目,然后“设置配置”-“定制”-“源”-“编码”改为GBK
java图形界面编程
因为这是个applet 不能直接运行
注意:applet不能直接用java命令运行
方法(步骤):
1、Javac welcome.java后生成welcome.class
2、在跟welcome.class同一级的目录下新建一个文本文件,并输入:
applet code="welcome.class"
width=320 height=180
/applet
3、保存并将文件名改为welcome.html
(这个html文件的名字任意,在这边我用welcome)
4、在cmd中输入appletviewer welcome.html便可以看见你的applet了
或直接把这个welcome.html拖入浏览器运行,也可以有同样效果
5、现在我解释一下上面的代码:
applet
/applet
是必要的格式,在里面可以输入许多参数,其中code,width,height是必须的
code:欲运行的class的名字
width:applet窗口的宽度
height: applet窗口的高度
java图形界面代码
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame {
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook() {
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件保存路径如c:\\ab.txt");
jb = new JButton("保存文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp, BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//-------------核心代码---------
String path = jtf.getText();
File f = new File(path);
String txt = jta.getText().replaceAll("\n", "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(txt);//写入文件中
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//-------------核心代码---------
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}
java 图形界面汉字乱码
这是编码问题,可以做如下设置: 右键单击这个项目,然后“设置配置”-“定制”-“源”-“编码”改为GBK
关于图形java编码和编程java语言代码图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。