一个JAVABean的小例子

//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

鱼台论坛http://bbs.370827.org/forum-61-1.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值