展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class CalculateArea extends JFrame {
private static final long serialVersionUID = 8109685421829572012L;
private JTextField cntField;
private JPanel panel;
private JScrollPane sPanel;
private JTextField[][] coordinateFields;
private JTextField resultField ;
public CalculateArea(String title){
62616964757a686964616fe59b9ee7ad9431333332623930super(title);
init();
}
public void init(){
Container c = this.getContentPane();
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(260, 30));
panel1.setLayout(new FlowLayout());
JLabel cntLab = new JLabel("多边形边数:");
cntLab.setPreferredSize(new Dimension( 70, 20));
panel1.add(cntLab);
cntField = new JTextField();
cntField.setPreferredSize(new Dimension(110, 20));
panel1.add(cntField);
JButton okBut = new JButton("确定");
okBut.setPreferredSize(new Dimension( 60, 20));
okBut.addActionListener(new CreatePanelActionListener(this));
panel1.add(okBut);
c.add(panel1,BorderLayout.NORTH);
//给确定按钮添加自定义事件
panel = new JPanel();
sPanel = new JScrollPane();
sPanel.setPreferredSize(new Dimension(240, 300));
sPanel.setViewportView(panel);
sPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
c.add(sPanel, BorderLayout.CENTER);
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(260, 30));
panel2.setLayout(new FlowLayout());
JButton calculateBut = new JButton("计算");
calculateBut.setPreferredSize(new Dimension( 60, 20));
panel2.add(calculateBut);
JLabel resultLab = new JLabel("计算结果为:");
resultLab.setPreferredSize(new Dimension( 70, 20));
panel2.add(resultLab);
resultField = new JTextField();
resultField.setPreferredSize(new Dimension(110, 20));
panel2.add(resultField);
calculateBut.addActionListener(new CalculateActionListener(this));
c.add(panel2,BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
//this.setResizable(false);
this.setSize(300, 500);
setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new CalculateArea("多边形面积计算");
}
public JTextField getCntField() {
return cntField;
}
public JPanel getPanel() {
return panel;
}
public JTextField[][] getCoordinateFields() {
return coordinateFields;
}
public void setCoordinateFields(JTextField[][] coordinateFields) {
this.coordinateFields = coordinateFields;
}
public JScrollPane getsPanel() {
return sPanel;
}
public JTextField getResultField() {
return resultField;
}
}
//计算面积的事件
class CalculateActionListener implements ActionListener{
private JFrame frame;
public CalculateActionListener(JFrame frame){
this.frame = frame;
}
public void actionPerformed(ActionEvent e) {
JPanel panel = ((CalculateArea)frame).getPanel();
JTextField[][] coordinateFields = ((CalculateArea)frame).getCoordinateFields();
JTextField resultField = ((CalculateArea)frame).getResultField();
int[][] coord = new int[2][coordinateFields[0].length];
if(coordinateFields==null){
JLabel label = new JLabel("请先输入多边形,然后填写坐标信息!");
label.setBounds(40, 200, 295, 20);
panel.add(label);
frame.repaint();
}else{
for (int i = 0; i
try{
String xStr = coordinateFields[0][i].getText();
String yStr =coordinateFields[1][i].getText();
int x = Integer.parseInt(xStr);
int y = Integer.parseInt(yStr);
coord[0][i] = x;
coord[1][i] = y;
}catch (Exception ex) {
resultField.setText("输入的坐标值有误,请修改后重新输入!");
frame.repaint();
return;
}
}
calulateArea(coord, resultField);
}
}
//根据坐标计算面积
//计算多边形面积公式为:0.5*abs(x1*y2-y1*x2+x2*y3-y2*x3+...+xn*y1-yn*x1)
public void calulateArea(int[][] coord,JTextField resultField){
double area = 0;
int sum = 0;
for (int i = 0; i
sum = sum + coord[0][i]*coord[1][i+1]-coord[1][i]*coord[0][i+1];
}
sum = sum + coord[0][coord[0].length-1]*coord[1][0]-coord[1][coord[0].length-1]*coord[0][0];
area = 0.5*Math.abs(sum);
resultField.setText(area+"");
}
}
//生成填写坐标的面板的事件
class CreatePanelActionListener implements ActionListener{
private JFrame frame;
public CreatePanelActionListener(JFrame frame){
this.frame = frame;
}
public void actionPerformed(ActionEvent eve) {
String cntStr = ((CalculateArea)frame).getCntField().getText();
JPanel panel = ((CalculateArea)frame).getPanel();
panel.removeAll();
int cnt=0;
//获取填写的多边形坐标数
try{
cnt = Integer.parseInt(cntStr);
}catch (Exception e) {
JLabel label = new JLabel("请输入大于等于3的整数!");
label.setBounds(70, 200, 295, 20);
panel.add(label);
((CalculateArea)frame).repaint();
}
if(cnt<=2){
JLabel label = new JLabel("请输入大于等于3的整数!");
label.setBounds(70, 200, 295, 20);
panel.add(label);
((CalculateArea)frame).repaint();
}else{
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(260,25*cnt+25));
panel.setSize(new Dimension(260,25*cnt+25));
//清除填写坐标面板上的所有控件
JLabel label = new JLabel("请输入每个点的坐标:");
label.setPreferredSize(new Dimension(260, 20));
panel.add(label);
label = new JLabel("(注:必须按顺时针或逆时针依次填入坐标!)");
label.setForeground(Color.red);
label.setPreferredSize(new Dimension(260, 20));
panel.add(label);
//填写坐标的标签和输入框
JLabel[] coordinateLabels = new JLabel[cnt];
JTextField[][] coordinateFields = new JTextField[2][cnt];
for (int i = 0; i
coordinateLabels[i] = new JLabel("第"+(i+1)+"点:");
coordinateLabels[i].setPreferredSize(new Dimension( 45, 20));
panel.add(coordinateLabels[i]);
JLabel labelx = new JLabel("X:");
labelx.setPreferredSize(new Dimension(20, 20));
panel.add(labelx);
coordinateFields[0][i] = new JTextField();
coordinateFields[0][i].setPreferredSize(new Dimension(70, 20));
panel.add(coordinateFields[0][i]);
JLabel labely = new JLabel("Y:");
labely.setPreferredSize(new Dimension(20, 20));
panel.add(labely);
coordinateFields[1][i] = new JTextField();
coordinateFields[1][i].setPreferredSize(new Dimension(70, 20));
panel.add(coordinateFields[1][i]);
}
((CalculateArea)frame).setCoordinateFields(coordinateFields);
((CalculateArea)frame).repaint();
}
}
}