自己编写的一个java简单的窗口实现两点求园面积

来源:互联网 发布:rpc java 编辑:程序博客网 时间:2024/05/01 09:04
package Circle;

public class test {

@SuppressWarnings("unused")
public static void main(String[] args) {
// TODO Auto-generated method stub
Getdata data =new Getdata();

}

}//主方法


//点类

package Circle;

public class Point {
public double x,y;
double getLance(Point p1,Point p2){
double lance = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return lance;
}
void setXY(double i,double j){
x=i;
y=j;
}

public Point(double i,double j){
x=i;
y=j;
}

}


//圆类,继承点类

package Circle;




public class Circlearea extends Point {
double r;

public Circlearea(){
super(0,0);//调用父类的构造方法

}

public double getarea(Point p1,Point p2){
r=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
double area=3.14*r*r;
return area;
}


}


//窗体显示类

package Circle;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;


/**
* @author admin
* wirte win
*
*/
public class Getdata extends Frame{

/**
*
*/
private static final long serialVersionUID = 6165187708490829110L;
TextField text1=new TextField(5);
TextField text2=new TextField(5);
TextField text3=new TextField(5);
TextField text4=new TextField(5);
Button button1=new Button("确认计算");
Button button2=new Button("清零");//


public Getdata()
{
super("计算");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
add(new JLabel("第一个点xy坐标"));
add(text1);
add(text2);
add(new JLabel("第二个点xy坐标"));
add(text3);
add(text4);
add(button1);
add(button2);//
button2.setBackground(Color.lightGray);
addWindowListener(new WindowCloser());
pack();
setVisible(true);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button1)
{
double a,b,c,d;
a=Double.parseDouble(text1.getText());
b=Double.parseDouble(text2.getText());
c=Double.parseDouble(text3.getText());
d=Double.parseDouble(text4.getText());
System.out.println(a);
Point p1=new Point(a,b);
Point p2=new Point(c,d);
System.out.println(p1.toString());
Circlearea cir=new Circlearea();
double f=cir.getarea(p1, p2);
String str =Double.toString(f);
showAnswer(str);
}
else{
System.out.print("出错");
}

}
}
);

button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button2)
{
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
}
else{
System.out.print("出错");
}

}
}
);
setWindowIcon();//
}

/*
* 图标修改,shift alt j
*/
public void setWindowIcon() //
{
ImageIcon imageIcon = new ImageIcon(getClass().getResource("/img/ico.png"));
this.setIconImage(imageIcon.getImage());
}


private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

public void showAnswer(String str){
JOptionPane.showMessageDialog(null, "结果为:"+str,"结果",JOptionPane.INFORMATION_MESSAGE);
}

}






0 0