📄 addressentrypanel.java
字号:
package elegate.cn.edu.nju;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.*;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.InternationalFormatter;
import java.io.*;
/**
* a panel to get input of an address entry
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
public class AddressEntryPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField txtName;
private JTextField txtNickname;
private JComboBox comboSex;
private JFormattedTextField txtMobilePhone;
private JFormattedTextField txtHomeTelephone;
private JFormattedTextField txtOfficeTelephone;
private JComboBox comboYear;
private JComboBox comboMonth;
private JComboBox comboDay;
private JFormattedTextField txtEmail;
private JComboBox comboIcon;
private JComboBox comboProfession;
private JComboBox comboCity;
private JComboBox comboGroup;
private JButton btnOk;
private JButton btnCancel;
private JButton btnHelp;
private AddressBookModel addressBookModel=null;
private AddressEntry addressEntry=null;
private JDialog parent=null;
private boolean isModify=false;
public AddressEntryPanel(JDialog parent,AddressBookModel addressBookModel)
{
this.parent=parent;
this.addressBookModel=addressBookModel;
JLabel lblName=new JLabel("*Name:");
lblName.setDisplayedMnemonic('N');
txtName=new JTextField(10);
lblName.setLabelFor(txtName);
JLabel lblNickname=new JLabel("Nickname:");
lblNickname.setDisplayedMnemonic('K');
txtNickname=new JTextField(10);
lblNickname.setLabelFor(txtNickname);
JLabel lblSex=new JLabel("Sex:");
lblSex.setDisplayedMnemonic('S');
String[] sexType={"Male","Female","Secret"};
comboSex=new JComboBox(sexType);
lblSex.setLabelFor(comboSex);
JLabel lblMobilePhone=new JLabel("Mobile Phone:");
lblMobilePhone.setDisplayedMnemonic('M');
NumberFormat nf=NumberFormat.getNumberInstance();
nf.setGroupingUsed(false);
txtMobilePhone=new JFormattedTextField(nf);
txtMobilePhone.setValue(13);
lblMobilePhone.setLabelFor(txtMobilePhone);
JLabel lblHomePhone=new JLabel("Home Tel:");
lblHomePhone.setDisplayedMnemonic('T');
InternationalFormatter formatter=new InternationalFormatter()
{
private static final long serialVersionUID = 1L;
protected DocumentFilter getDocumentFilter()
{
return filter;
}
private DocumentFilter filter=new IntFilter();
};
FixedLineTelephoneNumberVerifier verifier=new FixedLineTelephoneNumberVerifier();
txtHomeTelephone=new JFormattedTextField(formatter);
txtHomeTelephone.setInputVerifier(verifier);
lblHomePhone.setLabelFor(txtHomeTelephone);
JLabel lblOfficePhone=new JLabel("Office Tel:");
lblOfficePhone.setDisplayedMnemonic('F');
txtOfficeTelephone=new JFormattedTextField(formatter);
txtOfficeTelephone.setInputVerifier(verifier);
lblOfficePhone.setLabelFor(txtOfficeTelephone);
JLabel lblBirthday=new JLabel("Birthday:");
lblBirthday.setDisplayedMnemonic('B');
ComboFocusYearOrMonthL focusL=new ComboFocusYearOrMonthL();
comboYear=new JComboBox(new YearComboBoxModel());
comboYear.addFocusListener(focusL);
lblBirthday.setLabelFor(comboYear);
JLabel lblYear=new JLabel("Year");
Integer[] month={1,2,3,4,5,6,7,8,9,10,11,12};
comboMonth=new JComboBox(month);
comboMonth.addFocusListener(focusL);
JLabel lblMonth=new JLabel("Month");
Integer[] day={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
,18,19,20,21,22,23,24,25,26,27,28,29,30,31
};
comboDay=new JComboBox(day);
JLabel lblDay=new JLabel("Day");
JLabel lblEmail=new JLabel("Email:");
lblEmail.setDisplayedMnemonic('E');
txtEmail=new JFormattedTextField();
txtEmail.setInputVerifier(new EmailAddressFormatVerifier());
lblEmail.setLabelFor(txtEmail);
ImageComboBoxModel model=new ImageComboBoxModel();
JLabel lblIcon=new JLabel("Icon:");
lblIcon.setDisplayedMnemonic('I');
comboIcon=new JComboBox(model);
lblIcon.setLabelFor(comboIcon);
this.comboIcon.setSelectedIndex
((int)(Math.random()*this.comboIcon.getItemCount()));
String[] professions={"Student","Education or Research"
,"Government","Finance or Insurance","IT","Commerce"
,"Manufacture","Service","Medical","Welfare","Personal"
,"Others"
};
JLabel lblProfession=new JLabel("Profession:");
lblProfession.setDisplayedMnemonic('P');
comboProfession=new JComboBox(professions);
comboProfession.setSelectedIndex(0);
comboProfession.setEditable(true);
lblProfession.setLabelFor(comboProfession);
String[] cities={"Shanghai","Beijing","Nanjing","Tianjin"
,"Chongqing","Anhui","Gansu","Fujian","Guandong"
,"Xuzhou","Hainan","Suqian","Nantong","Yancheng"
,"Yixing","Suzhou","Huaian","Zhejiang","Jilin"
,"Heilongjiang","Shandong","Shānxi","Jiangxi"
,"Mongolia","Taiwan","Hebei","Henan","Hunan","Hubei"
,"Ningxia","Qinghai","Hongkong","Macao","Others"
};
JLabel lblCity=new JLabel("City:");
lblCity.setDisplayedMnemonic('y');
comboCity=new JComboBox(cities);
comboCity.setEditable(true);
comboCity.setSelectedIndex(0);
lblCity.setLabelFor(comboCity);
JLabel lblGroup=new JLabel("Group:");
lblGroup.setDisplayedMnemonic('G');
Collection<GroupEntry> c=addressBookModel.getAllGroupEntries();
comboGroup=new JComboBox(c.toArray());
lblGroup.setLabelFor(comboGroup);
btnOk=new JButton("OK");
btnOk.setMnemonic('O');
btnOk.addActionListener(new BtnOkL());
btnOk.setToolTipText("Click me to add this entry");
btnCancel=new JButton("Cancel");
btnCancel.setMnemonic('C');
btnCancel.addActionListener(new BtnCancelL());
btnCancel.setToolTipText("Click me to cancel");
btnHelp =new JButton("Help");
btnHelp.setMnemonic('H');
btnHelp.addActionListener(new BtnHelpL());
btnHelp.setToolTipText("Click me to get help");
GridBagLayout gbLayout=new GridBagLayout();
this.setLayout(gbLayout);
GridBagConstraints gbCon=new GridBagConstraints();
gbCon.weightx=100;
gbCon.fill=GridBagConstraints.BOTH;
JLabel lblEntry=new JLabel("Address Entry");
lblEntry.setFont(new Font(this.getFont().getFontName()
,Font.BOLD,24));
this.addComponent(lblEntry,1,0,7,1,gbCon);
this.addComponent(lblName,0,1,1,1,gbCon);
this.addComponent(txtName,1,1,1,1,gbCon);
this.addComponent(lblNickname,0,2,1,1,gbCon);
this.addComponent(txtNickname,1,2,1,1,gbCon);
this.addComponent(lblSex,0,3,1,1,gbCon);
this.addComponent(comboSex,1,3,1,1,gbCon);
this.addComponent(lblMobilePhone,0,4,1,1,gbCon);
this.addComponent(txtMobilePhone,1,4,1,1,gbCon);
this.addComponent(new JLabel("E.g.: 139133XXXXX"),2,4,1,1,gbCon);
this.addComponent(lblHomePhone,0,5,1,1,gbCon);
this.addComponent(txtHomeTelephone,1,5,1,1,gbCon);
this.addComponent(new JLabel("E.g.: 0527-458XXXXX"),2,5,1,1,gbCon);
this.addComponent(lblOfficePhone,0,6,1,1,gbCon);
this.addComponent(txtOfficeTelephone,1,6,1,1,gbCon);
this.addComponent(new JLabel("E.g.: 0527-458XXXXX"),2,6,1,1,gbCon);
this.addComponent(lblBirthday,0,7,1,1,gbCon);
this.addComponent(comboYear,1,7,1,1,gbCon);
this.addComponent(lblYear,2,7,1,1,gbCon);
this.addComponent(comboMonth,3,7,1,1,gbCon);
this.addComponent(lblMonth,4,7,1,1,gbCon);
this.addComponent(comboDay,5,7,1,1,gbCon);
this.addComponent(lblDay,6,7,1,1,gbCon);
this.addComponent(lblEmail,0,8,1,1,gbCon);
this.addComponent(txtEmail,1,8,1,1,gbCon);
this.addComponent(new JLabel("E.g.: elegate@gmail.com"),2,8,1,1,gbCon);
this.addComponent(lblIcon,0,9,1,1,gbCon);
this.addComponent(comboIcon,1,9,1,1,gbCon);
this.addComponent(lblProfession,0,10,1,1,gbCon);
this.addComponent(comboProfession,1,10,1,1,gbCon);
this.addComponent(lblCity,0,11,1,1,gbCon);
this.addComponent(comboCity,1,11,1,1,gbCon);
this.addComponent(lblGroup,0,12,1,1,gbCon);
this.addComponent(comboGroup,1,12,1,1,gbCon);
JPanel btnPanel=new JPanel();
btnPanel.add(btnOk);
btnPanel.add(btnCancel);
btnPanel.add(btnHelp);
this.addComponent(btnPanel,1,13,1,1,gbCon);
}
protected JButton getDefaultButton()
{
return this.btnOk;
}
protected void updateAddressBookModel( )
{
this.comboIcon.setSelectedIndex
((int)(Math.random()*this.comboIcon.getItemCount()));
Collection<GroupEntry> c=addressBookModel.getAllGroupEntries();
DefaultComboBoxModel groupModel=(DefaultComboBoxModel)comboGroup.getModel();
groupModel.removeAllElements();
for(GroupEntry ele:c)
groupModel.addElement(ele);
}
private void addComponent(Component c,int gridx,int gridy,
int gridwidth,int gridheight,GridBagConstraints gbCon)
{
gbCon.gridx=gridx;
gbCon.gridy=gridy;
gbCon.gridwidth=gridwidth;
gbCon.gridheight=gridheight;
this.add(c,gbCon);
}
protected void updateAddressEntry(AddressEntry addressEntry)
{
int year=addressEntry.getBirthday().get
(GregorianCalendar.YEAR);
int month=addressEntry.getBirthday().get
(GregorianCalendar.MONTH)+1;
int day=addressEntry.getBirthday().get
(GregorianCalendar.DATE);
this.txtEmail.setText(addressEntry.getEmail());
this.txtHomeTelephone.setText
(addressEntry.getHomeTelephone());
this.txtMobilePhone.setText
(addressEntry.getMobilePhone());
this.txtName.setText(addressEntry.getName());
this.txtNickname.setText(addressEntry.getNickname());
this.comboSex.setSelectedItem(addressEntry.getSex());
this.txtOfficeTelephone.setText(addressEntry.getOfficeTelephone());
this.comboCity.setSelectedItem(addressEntry.getCity());
this.comboDay.setSelectedIndex(day-1);
this.comboIcon.setSelectedIndex(addressEntry.getImageId()-1);
this.comboMonth.setSelectedIndex(month-1);
this.comboProfession.setSelectedItem
(addressEntry.getProfession());
this.comboYear.setSelectedItem(year);
this.isModify=true;
this.addressEntry=addressEntry;
if(addressEntry.getGroupsBelongsTo().size()>1)
this.comboGroup.setSelectedItem
(addressEntry.getGroupsBelongsTo().get(1));
else
this.comboGroup.setSelectedItem
(addressEntry.getGroupsBelongsTo().get(0));
}
public AddressEntry getAddressEntry()
{
AddressEntry tmp=this.addressEntry;
this.addressEntry=null;
this.isModify=false;
return tmp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -