//Loan.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.beans.*;
publicclassLoanextendsJPanelimplementsActionListener
{
JLabellType,lAmount,lmonth,lInstlmnt;
JTextFieldtAmount,tmonth,tInstlmnt;
JListtType;
JButtonsubmit;
StringloanType;
String[]interestRates={"5","5","4"};
publicString[]getInterestRates()
{
returninterestRates;
}
publicvoidsetInterestRates(String[]interestRates)
{
this.interestRates=interestRates;
}
publicLoan()
{
lType=newJLabel("贷款类型:");
lAmount=newJLabel("贷款金额:");
lmonth=newJLabel("贷款月数:");
lInstlmnt=newJLabel("每月应付金额:");
tAmount=newJTextField(10);
tmonth=newJTextField(10);
tInstlmnt=newJTextField(10);
String[]Type={"Housing","Personal","Car"};
tType=newJList(Type);
submit=newJButton("È?¶¨");
setLayout(newGridLayout(5,2));
add(lType);add(tType);
add(lAmount);add(tAmount);
add(lmonth);add(tmonth);
add(lInstlmnt);add(tInstlmnt);
add(submit);
lInstlmnt.setVisible(false);
tInstlmnt.setVisible(false);
submit.addActionListener(this);
}
publicvoidactionPerformed(ActionEventevt)
{
floatintlmnt=0;
intcarloan=0;
intpersonalloan=0;
inthouseloan=0;
intamount;
intnoofmonth;
JButtonsource=(JButton)evt.getSource();
amount=Integer.parseInt(tAmount.getText());
noofmonth=Integer.parseInt(tmonth.getText());
//loanType=(String)tType.getSelectedValue();
//tInstlmnt.setText("sample");
intamtpermonth=amount/noofmonth;
if(!(interestRates[0]).equals(""))
{
carloan=Integer.parseInt(interestRates[0]);
}
if(!(interestRates[1]).equals(""))
{
personalloan=Integer.parseInt(interestRates[1]);
}
if(!(interestRates[2]).equals(""))
{
houseloan=Integer.parseInt(interestRates[2]);
}
loanType=(String)tType.getSelectedValue();
if(loanType.equals("Personal"))
{
intlmnt=(personalloan*amount)/(100*noofmonth);
}
if(loanType.equals("Housing"))
{
intlmnt=(houseloan*amount)/(100*noofmonth);
}
if(loanType.equals("Car"))
{
intlmnt=(carloan*amount)/(100*noofmonth);
}
tInstlmnt.setText(String.valueOf(intlmnt amtpermonth));
lInstlmnt.setVisible(true);
tInstlmnt.setVisible(true);
tInstlmnt.setEnabled(false);
}
}
//LoanEditor.java
importjava.beans.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassLoanEditorextendsJPanelimplementsPropertyEditor,ActionListener
{
JLabellcar,lpersonal,lhouse;
JTextFieldtcar,tpersonal,thouse;
String[]interestRates={"","",""};
JPanelPanel;
publicObjectgetValue()
{
interestRates[0]=tcar.getText();
interestRates[1]=tpersonal.getText();
interestRates[2]=thouse.getText();
returninterestRates;
}
publicvoidsetValue(Objectctemp)
{
interestRates=(String[])ctemp;
tcar.setText(interestRates[0]);
tpersonal.setText(interestRates[1]);
thouse.setText(interestRates[2]);
}
publicComponentgetCustomEditor(){returnthis;}
publicLoanEditor()
{
lcar=newJLabel("Æû³µ´û¿î");
lpersonal=newJLabel("¸öÈË´û¿î");
lhouse=newJLabel("?¿ÎÝ´û¿î");
tcar=newJTextField(10);
tpersonal=newJTextField(10);
thouse=newJTextField(10);
setLayout(newBorderLayout());
Panel=newJPanel();
Panel.setLayout(newGridLayout(3,2));
Panel.add(lcar);Panel.add(tcar);
Panel.add(lpersonal);Panel.add(tpersonal);
Panel.add(lhouse);Panel.add(thouse);
add(newJPanel(),BorderLayout.WEST);
add(newJPanel(),BorderLayout.EAST);
add(Panel,BorderLayout.CENTER);
tcar.addActionListener(this);
tpersonal.addActionListener(this);
thouse.addActionListener(this);
}
publicvoidactionPerformed(ActionEventevt)
{
interestRates[0]=tcar.getText();
interestRates[1]=tpersonal.getText();
interestRates[2]=thouse.getText();
}
publicbooleanisPaintable(){returntrue;}
publicvoidpaintValue(Graphicsg,Rectangler){}
publicStringgetJavaInitializationString(){returnnull;}
publicStringgetAsText(){returnnull;}
publicvoidsetAsText(Strings)throws
IllegalArgumentException{}
publicString[]getTags(){returnnull;}
publicbooleansupportsCustomEditor(){returntrue;}
}
//LoanBeanInfo.java
importjava.beans.*;
importjava.awt.*;
publicclassLoanBeanInfoextendsSimpleBeanInfo
{
publicPropertyDescriptor[]getPropertyDescriptors()
{
try
{
PropertyDescriptorinterestRates=newPropertyDescriptor("interestRates",Loan.class);
interestRates.setPropertyEditorClass(LoanEditor.class);
PropertyDescriptorarr[]=newPropertyDescriptor[1];
arr[0]=interestRates;
returnarr;
}
catch(Exceptione)
{
System.out.println(e);
returnnull;
}
}
}
//Loan.mft
Name:Loan.class
Java-Bean:True
Name:LoanEditor.class
Java-Bean:False
Name:LoanBeanInfo.class
Java-Bean:False
一个JAVABean的小例子
最新推荐文章于 2022-11-07 12:09:11 发布