概述:这是两个Java bean应用的例子,实现航空预定机票的功能。
预定构件功能说明:
1,预定构件应包含二个面板,预定座位面板与乘客面板
2,在装入预定构件时不应有可用的乘客面板
3,当用户打入要预定的旅行地点.旅行日期及座位号时:
(1)首先,应按以下格式生成乘客号:<place of travel> <date of travel> <count of the seat number>
(2)其次,在显示乘客号时应使乘客面板是可见的(注意:你可能重定BeanBox窗口中bean大小,以观看此乘客面板。而且,如果打入座位号为0,则不应显示乘客面板。)
(3)当用户点击乘客面板的submit按钮时,此面板的所有域控件中文本应置为空
(4)上面步骤被重复,直到所接收的乘客材料个数等于预定的座位数为止
4,在已预定了所需座位号之后,给出所预定座位号的消息框应被显示d
5,乘客面板应作为bean创建,可以把嵌入在铁路预定应用或航空应用中
6,你需接受以下格式之一的旅行日期"dd/mm/yy","mm/dd/yy","dd-Month-yy"
-----------------------------------------------
/*1
* Program file: PsEvent.java
* Creat time: 08/01/2004
* Creat by: InberKong
* Last Modify time:
* Modify by:
* Function describe: Event Class
*/
import java.util.*;
public class PsEvent extends EventObject
{
//
public String number;
public String name;
public String address;
public String phone;
public PsEvent(Object source,String m1,String m2,String m3,String m4)
{
super(source);
this.number=m1;
this.name=m2;
this.address=m3;
this.phone=m4;
}
}
------------------
/*2
* Program file: PsEventListener.java
* Creat time: 08/01/2004
* Creat by: InberKong
* Last Modify time:
* Modify by:
* Function describe: Event listener
*/
import java.util.*;
public interface PsEventListener extends EventListener
{
public void psPerformed(PsEvent ev);
}
---------------------------
/*3
* Program file: PsBean.java
* Creat time: 08/01/2004
* Creat by: InberKong
* Last Modify time:
* Modify by:
* Function describe: Passenger message bean
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PsBean extends JPanel implements ActionListener
{
JLabel lb_number;
JLabel lb_name;
JLabel lb_address;
JLabel lb_phone;
JTextField tf_number;
JTextField tf_name;
JTextField tf_address;
JTextField tf_phone;
JButton sbmt;
PsEventListener pl;//声明事件监听
public PsBean()
{
setLayout(new GridLayout(5,2));//设置布局管理器
lb_number=new JLabel("Passenger Number:");//创建对象
lb_name=new JLabel("Passenger Name:");
lb_address=new JLabel("Passenger Address:");
lb_phone=new JLabel("Passenger Phone:");
tf_number=new JTextField(15);
tf_name=new JTextField(15);
tf_address=new JTextField(25);
tf_phone=new JTextField(15);
sbmt=new JButton("Submit");
add(lb_number);
add(tf_number);
add(lb_name);
add(tf_name);
add(lb_address);
add(tf_address);
add(lb_phone);
add(tf_phone);
add(sbmt);
sbmt.addActionListener(this);//监听事件
tf_number.setEnabled(false);
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==sbmt)
{
PsEvent ev=new PsEvent(obj,tf_number.getText(),tf_name.getText(),tf_address.getText(),tf_phone.getText());
pl.psPerformed(ev);
}
}
public void PsListener(PsEventListener el)
{
pl=el;
}
public void setPnrno(String ptemp)
{
tf_number.setText(ptemp);
}
public void refresh()
{
tf_number.setText("");
tf_name.setText("");
tf_address.setText("");
tf_phone.setText("");
}
}
---------------------
/*4
* Program file: Bpanel.java
* Creat time: 08/01/2004
* Creat by: InberKong
* Last Modify time:
* Modify by:
* Function describe: Book panel bean
*/
import java.beans.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Bpanel extends JPanel implements PsEventListener,ActionListener
{
JPanel pnl;
BoxLayout blo;
JLabel label_Date;
JTextField text_Date;
JLabel label_Place;
JTextField text_Place;
JLabel label_NOS;
JTextField text_NOS;
JButton button_Book;
JLabel the_animation;
String bank_Name="FlyMyWay Airlines";
GridBagLayout gl_t;
GridBagConstraints gbcs;
//乘客面板javabean
PsBean pb;
int t_nos;
int t_count;
String t_passno;
//Date format property with getter and setter methods
private String the_datFormat="mm/dd/yy";
public String getDatFormat()
{
return the_datFormat;
}
public void setDatFormat(String temps)
{
the_datFormat=temps;
if(the_datFormat=="mm/dd/yy")
text_Date.setText("mm/dd/yy");
if(the_datFormat=="dd/mm/yy")
text_Date.setText("dd/mm/yy");
if(the_datFormat=="dd-Month-yy")
text_Date.setText("dd-Month-yy");
}
public Bpanel()
{
pnl=new JPanel();
the_animation=new JLabel(bank_Name);
label_Date=new JLabel("Enter Date of Travel:");
text_Date=new JTextField(20);
text_Date.setText(the_datFormat);
label_Place=new JLabel("Enter Place of Travel:");
text_Place=new JTextField(15);
label_NOS=new JLabel("Enter Number of seats to be booked");
text_NOS=new JTextField(5);
button_Book=new JButton("BOOK SEATS");
gl_t=new GridBagLayout();
gbcs=new GridBagConstraints();
pnl.setLayout(gl_t);
gbcs.gridx=3;
gbcs.gridy=10;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(the_animation,gbcs);
pnl.add(the_animation);
gbcs.gridx=2;
gbcs.gridy=20;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(label_Date,gbcs);
pnl.add(label_Date);
gbcs.gridx=4;
gbcs.gridy=20;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(text_Date,gbcs);
pnl.add(text_Date);
gbcs.gridx=2;
gbcs.gridy=30;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(label_Place,gbcs);
pnl.add(label_Place);
gbcs.gridx=4;
gbcs.gridy=30;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(text_Place,gbcs);
pnl.add(text_Place);
gbcs.gridx=2;
gbcs.gridy=40;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(label_NOS,gbcs);
pnl.add(label_NOS);
gbcs.gridx=4;
gbcs.gridy=40;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(text_NOS,gbcs);
pnl.add(text_NOS);
gbcs.gridx=3;
gbcs.gridy=50;
gbcs.anchor=GridBagConstraints.NORTHWEST;
gl_t.setConstraints(button_Book,gbcs);
pnl.add(button_Book);
button_Book.addActionListener(this);
textDisplay();
pb=new PsBean();
pb.PsListener(this);
blo=new BoxLayout(this,BoxLayout.Y_AXIS);
this.setLayout(blo);
blo.addLayoutComponent("Booking Panel",pnl);
blo.addLayoutComponent("Passenger Panel",pb);
this.add(pnl);
this.add(pb);
pb.setVisible(false);
}
public void textDisplay()
{
Font f=new Font("Times New Roman",Font.BOLD,30);
the_animation.setFont(f);
the_animation.setText(bank_Name);
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
try{
if(Integer.parseInt(text_NOS.getText())==0)
JOptionPane.showMessageDialog(this,new String("Number of Passengers >0"));
if(obj==button_Book && (Integer.parseInt(text_NOS.getText())>0))
{
text_Date.setEnabled(false);
text_Place.setEnabled(false);
text_NOS.setEnabled(false);
button_Book.setEnabled(false);
t_count=1;
t_passno=text_Place.getText().trim()+text_Date.getText().trim()+String.valueOf(t_count);
t_nos=Integer.parseInt(text_NOS.getText());
pb.setPnrno(t_passno);
pb.setVisible(true);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,new String("NumberFormat For input string is wrong,plese check it!"));
}
}
public void psPerformed(PsEvent evt)
{
t_count++;
if(t_count<=t_nos)
{
t_passno=text_Place.getText().trim()+text_Date.getText().trim()+String.valueOf(t_count);
pb.refresh();
pb.setPnrno(t_passno);
}
else
{
pb.setVisible(false);
t_count--;
JOptionPane.showMessageDialog(this,new String("Number of Passenger Added : "+t_count));
text_Date.setText(the_datFormat);
text_Place.setText("");
text_NOS.setText("");
text_Date.setEnabled(true);
text_Place.setEnabled(true);
text_NOS.setEnabled(true);
button_Book.setEnabled(true);
pb.refresh();
t_nos=0;
t_count=0;
}
}
}
------------------
/*5
* Program file: app.java
* Creat time: 08/01/2004
* Creat by: InberKong
* Last Modify time:
* Modify by:
* Function describe: FlyMyWay Airlines
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class app extends JFrame
{
Bpanel panel=new Bpanel();//javabean
public app()
{
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String args[])
{
new app();
}
}
------------------
如果朋友们看不出效果我可以把图发上。如有转载请与我联系。