/**
* 租金計算器
*/
package com.my.gui;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.my.util.CompareTime;
import com.my.util.StringUtil;
public class RentCalculatorFrame extends JFrame implements ActionListener,
KeyListener, MouseListener{
private JLabel rentPriceMonth;//月租金
private JTextField rentPriceMonthValue;
private JLabel startDate;
private JTextField startDateValue;
private JLabel endDate;
private JTextField endDateValue;
private JLabel rentPersonNums;
private JTextField rentPersonNumsValue;
private JLabel propertyCost;//其他費用
private JTextField propertyCostValue;
private JLabel othercost;//其他費用
private JTextField otherCostValue;
private JLabel sumCost;//總費用
private JLabel sumCostValue;
private JLabel everyCost;//平均費用
private JLabel everyCostValue;
private JButton submitButton;
private JButton resetButton;
private JPanel northPanel;
private JPanel centerPanel;
private JPanel southPanel;
private CompareTime compareTime;//日期比較
private StringUtil stringUtil;//字串工具類
private JCalendarFrame jcalendarFrame;
private int typee;//日期回應型別1:開始日期2:結束日期
public RentCalculatorFrame() {
this.setBounds(350,250,450,350);
this.setLayout(null);
this.setTitle("租金計算器");
northPanel=new JPanel();
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel.setBounds(3,5,435,190);
northPanel.setBorder(BorderFactory.createTitledBorder("租金計算資訊"));
rentPriceMonth=new JLabel("月租金");
rentPriceMonthValue=https://bbs.csdn.net/topics/new JTextField(15);
rentPriceMonthValue.addKeyListener(this);
startDate=new JLabel("開始日期");
startDateValue=https://bbs.csdn.net/topics/new JTextField(8);
startDateValue.addMouseListener(this);
endDate=new JLabel("結束日期");
endDateValue=https://bbs.csdn.net/topics/new JTextField(8);
endDateValue.addMouseListener(this);
endDateValue.addKeyListener(this);
rentPersonNums=new JLabel("合租人員");
rentPersonNumsValue=https://bbs.csdn.net/topics/new JTextField(15);
rentPersonNumsValue.addKeyListener(this);
propertyCost=new JLabel("物業費用");
propertyCostValue=https://bbs.csdn.net/topics/new JTextField(15);
propertyCostValue.addKeyListener(this);
othercost=new JLabel("其他費用");
otherCostValue=https://bbs.csdn.net/topics/new JTextField(15);
otherCostValue.addKeyListener(this);
Box vBox=Box.createVerticalBox();
Box vBox1=Box.createHorizontalBox();
Box hBox1=Box.createHorizontalBox();
hBox1.add(rentPriceMonth);
hBox1.add(Box.createHorizontalStrut(10));
hBox1.add(rentPriceMonthValue);
vBox1.add(hBox1);
vBox1.add(Box.createVerticalStrut(10));
Box vBox2=Box.createVerticalBox();
Box hBox2=Box.createHorizontalBox();
hBox2.add(startDate);
hBox2.add(Box.createHorizontalStrut(10));
hBox2.add(startDateValue);
hBox2.add(Box.createHorizontalStrut(10));
hBox2.add(endDate);
hBox2.add(Box.createHorizontalStrut(10));
hBox2.add(endDateValue);
vBox2.add(hBox2);
vBox2.add(Box.createHorizontalStrut(10));
Box vBox3=Box.createVerticalBox();
Box hBox3=Box.createHorizontalBox();
hBox3.add(rentPersonNums);
hBox3.add(Box.createHorizontalStrut(10));
hBox3.add(rentPersonNumsValue);
vBox3.add(hBox3);
vBox3.add(Box.createVerticalStrut(10));
Box vBox4=Box.createVerticalBox();
Box hBox4=Box.createHorizontalBox();
hBox4.add(propertyCost);
hBox4.add(Box.createHorizontalStrut(10));
hBox4.add(propertyCostValue);
vBox4.add(hBox4);
vBox4.add(Box.createVerticalStrut(10));
Box vBox5=Box.createVerticalBox();
Box hBox5=Box.createHorizontalBox();
hBox5.add(othercost);
hBox5.add(Box.createHorizontalStrut(10));
hBox5.add(otherCostValue);
vBox5.add(hBox5);
vBox5.add(Box.createVerticalStrut(10));
vBox.add(vBox1);
vBox.add(vBox2);
vBox.add(vBox3);
vBox.add(vBox4);
vBox.add(vBox5);
northPanel.add(vBox);
centerPanel.setBounds(3,195,435,70);
centerPanel.setBorder(BorderFactory.createTitledBorder("租金計算結果"));
sumCost=new JLabel("總費用: ");
sumCostValue=https://bbs.csdn.net/topics/new JLabel("");
everyCost=new JLabel("平均費用");
everyCostValue=https://bbs.csdn.net/topics/new JLabel("");
Box cvBox=Box.createVerticalBox();
Box cvBox1=Box.createVerticalBox();
Box chBox1=Box.createHorizontalBox();
chBox1.add(sumCost);
chBox1.add(Box.createHorizontalStrut(15));
chBox1.add(sumCostValue);
cvBox1.add(chBox1);
cvBox1.add(Box.createVerticalStrut(5));
Box cvBox2=Box.createVerticalBox();
Box chBox2=Box.createHorizontalBox();
chBox2.add(everyCost);
chBox2.add(Box.createHorizontalStrut(20));
chBox2.add(everyCostValue);
cvBox2.add(chBox2);
cvBox.add(cvBox1);
cvBox.add(cvBox2);
centerPanel.add(cvBox);
southPanel.setBounds(3,270,435,35);
submitButton=new JButton("確定");
resetButton=new JButton("重置");
submitButton.addActionListener(this);
resetButton.addActionListener(this);
southPanel.add(submitButton);
southPanel.add(resetButton);
Container container=this.getContentPane();
container.add(northPanel);
container.add(centerPanel);
container.add(southPanel);
compareTime=new CompareTime();
jcalendarFrame=new JCalendarFrame();
jcalendarFrame.setVisible(false);
stringUtil=new StringUtil();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
this.setVisible(true);
}
public int getTypee() {
return typee;
}
public void setTypee(int type1) {
this.typee=typee;
}
public static void main(String[] args) {
RentCalculatorFrame rentCalculator=new RentCalculatorFrame();
}
public JTextField getStartDateValue() {
return startDateValue;
}
public void setStartDateValue(JTextField startDateValue) {
this.startDateValue=https://bbs.csdn.net/topics/startDateValue;
}
public JTextField getEndDateValue() {
return endDateValue;
}
public void setEndDateValue(JTextField endDateValue) {
this.endDateValue=https://bbs.csdn.net/topics/endDateValue;
}
public void actionPerformed(ActionEvent event) {
//重置按鈕事件處理
if(event.getSource()==resetButton) {
rentPriceMonthValue.setText("");
startDateValue.setText("");
endDateValue.setText("");
rentPersonNumsValue.setText("");
propertyCostValue.setText("");
otherCostValue.setText("");
sumCostValue.setText("");
everyCostValue.setText("");
}
if(event.getSource()==submitButton) {
String rentPriceMonth=rentPriceMonthValue.getText();//月租金
String startDate=startDateValue.getText();//開始日期
String endDate=endDateValue.getText();//結束日期
String rentPersonNums=rentPersonNumsValue.getText();//合租人員
int day=0;
String propertyCost=propertyCostValue.getText();//物業費用
String otherCost=otherCostValue.getText();//其他費用
if(rentPriceMonth.equals("")||startDate.equals("")
||endDate.equals("")||rentPersonNums.equals("")) {
JOptionPane.showMessageDialog(this, "你還沒有輸入資訊呢!","租金計算出錯",
JOptionPane.ERROR_MESSAGE);
}else {
try {
//開始日期與結束日期校驗
if(compareTime.isRightSequence(startDate, endDate)) {
JOptionPane.showMessageDialog(this,"輸入結束日期在開始日期之前!",
"租金計算出錯",JOptionPane.ERROR_MESSAGE);
}else {
double rentPriceMonthDouble=Double
.parseDouble(rentPriceMonth);
double propertyCostDouble=Double
.parseDouble(propertyCost);
double otherCostDouble=Double.parseDouble(otherCost);
try {
day=compareTime
.getBetweenDays(startDate, endDate);
}catch(ParseException e) {
e.printStackTrace();
}
//計算總的租金費用
double allCost=0;
//System.out.println("day/30="+day/30);
int month=0;
try {
month=compareTime.betweenMonths(startDate, endDate);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("month="+month);
allCost=month*rentPriceMonthDouble+propertyCostDouble+otherCostDouble;
//計算合租的每一個人費用
int rentPersonNumsInt=Integer
.parseInt(rentPersonNums);
double everyCost=allCost/rentPersonNumsInt;
sumCostValue.setText(""+allCost);
everyCostValue.setText(""+Math.ceil(everyCost));
}
}catch (HeadlessException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}catch (ParseException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public void mouseClicked(MouseEvent event) {
if(event.getSource()==startDateValue) {
jcalendarFrame.setVisible(true);
startDateValue.setText(jcalendarFrame.getDate());
typee=1;
jcalendarFrame.setRentCalculatorFrame(this);
}
if(event.getSource()==endDateValue) {
jcalendarFrame.setVisible(true);
endDateValue.setText(jcalendarFrame.getDate());
typee=2;
jcalendarFrame.setRentCalculatorFrame(this);
}
}
public void mouseEntered(MouseEvent event) {
}
@Override
public void mouseExited(MouseEvent event) {
}
@Override
public void mousePressed(MouseEvent arg0) {
//TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent event) {
if(event.getSource()==startDateValue) {
jcalendarFrame.setVisible(false
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/44429.html
標籤:Eclipse
上一篇:資料互動握手報錯
下一篇:請教大佬們
