求java大神!!!跪求!期末考试题!!好的我绝对追分!!在线等 10
求java大神!!!跪求!期末考试题!!好的我绝对追分!!在线等如图,谢谢!!!!!期末在线等...
求java大神!!!跪求!期末考试题!!好的我绝对追分!!在线等如图,谢谢!!!!!期末在线等
展开
2个回答
展开全部
package demo.com.test;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
public class Distance3d {
private JFrame frame;
private JTextField x1,y1,z1,x2,y2,z2;
private JTextPane resultStr;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Distance3d window = new Distance3d();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Distance3d() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("距离计算");
frame.setBounds(100, 100, 555, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("点A坐标:");
lblNewLabel.setBounds(10, 10, 63, 15);
frame.getContentPane().add(lblNewLabel);
x1 = new JTextField();
x1.setBounds(94, 7, 38, 21);
frame.getContentPane().add(x1);
x1.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("X");
lblNewLabel_1.setBounds(83, 10, 14, 15);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblY = new JLabel("Y");
lblY.setBounds(142, 10, 14, 15);
frame.getContentPane().add(lblY);
y1 = new JTextField();
y1.setColumns(10);
y1.setBounds(153, 7, 38, 21);
frame.getContentPane().add(y1);
JLabel lblZ = new JLabel("Z");
lblZ.setBounds(199, 10, 14, 15);
frame.getContentPane().add(lblZ);
z1 = new JTextField();
z1.setColumns(10);
z1.setBounds(210, 7, 38, 21);
frame.getContentPane().add(z1);
z2 = new JTextField();
z2.setColumns(10);
z2.setBounds(491, 7, 38, 21);
frame.getContentPane().add(z2);
JLabel lblZ_1 = new JLabel("Z");
lblZ_1.setBounds(480, 10, 14, 15);
frame.getContentPane().add(lblZ_1);
y2 = new JTextField();
y2.setColumns(10);
y2.setBounds(434, 7, 38, 21);
frame.getContentPane().add(y2);
JLabel lblY_1 = new JLabel("Y");
lblY_1.setBounds(423, 10, 14, 15);
frame.getContentPane().add(lblY_1);
x2 = new JTextField();
x2.setColumns(10);
x2.setBounds(375, 7, 38, 21);
frame.getContentPane().add(x2);
JLabel lblX = new JLabel("X");
lblX.setBounds(364, 10, 14, 15);
frame.getContentPane().add(lblX);
JLabel lblb = new JLabel("点B坐标:");
lblb.setBounds(291, 10, 63, 15);
frame.getContentPane().add(lblb);
JButton btnNewButton = new JButton("开始计算");
btnNewButton.setBounds(10, 90, 97, 50);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double dx1 = Double.parseDouble(x1.getText().toString().trim());
double dy1 = Double.parseDouble(y1.getText().toString().trim());
double dz1 = Double.parseDouble(z1.getText().toString().trim());
double dx2 = Double.parseDouble(x2.getText().toString().trim());
double dy2 = Double.parseDouble(y2.getText().toString().trim());
double dz2 = Double.parseDouble(z2.getText().toString().trim());
Point3 p1 =new Point3(dx1,dy1,dz1);
Point3 p2 =new Point3(dx2,dy2,dz2);
resultStr.setText(p1.toString("A")+" ;"+p2.toString("B")+" ;两点直线距离为:"+distance(p1, p2));
}
});
frame.getContentPane().add(btnNewButton);
resultStr = new JTextPane();
resultStr.setBounds(117, 62, 412, 100);
frame.getContentPane().add(resultStr);
}
public double distance(Point3 p1, Point3 p2) {
return Math.sqrt(Math.pow((p1.getX() - p2.getX()), 2)
+ Math.pow((p1.getY() - p2.getY()), 2)
+ Math.pow((p1.getZ() - p2.getZ()), 2));
}
}
class Point3 {
double x;
double y;
double z;
public Point3(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setZ(double z) {
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public String toString(String tag) {
// TODO Auto-generated method stub
return tag + "点坐标为: [" + x + "," + y + "," + z + "]";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询