📄 addressentrypanel.java
字号:
private boolean checkInput()
{
String name=txtName.getText().trim();
if(name.length()==0)
{
JOptionPane.showMessageDialog(this,"Illegal name"
,"Warning",JOptionPane.ERROR_MESSAGE);
txtName.setText("");
txtName.requestFocusInWindow();
return false;
}
int year=((Integer)comboYear.getSelectedItem()).intValue();
int month=((Integer)comboMonth.getSelectedItem()).intValue();
int day=((Integer)comboDay.getSelectedItem()).intValue();
GregorianCalendar g=new GregorianCalendar(year,month-1,day);
if(!isModify)
this.addressEntry=new AddressEntry(name,txtNickname.getText()
,comboSex.getSelectedItem().toString()
,txtMobilePhone.getText(),txtHomeTelephone.getText()
,txtOfficeTelephone.getText(),g,txtEmail.getText()
,comboIcon.getSelectedIndex()+1
,comboProfession.getSelectedItem().toString()
,comboCity.getSelectedItem().toString()
,this.addressBookModel.getGroupEntry
(comboGroup.getSelectedItem().toString())
);
else
{
this.addressEntry.setAddressEntry(name,txtNickname.getText()
,comboSex.getSelectedItem().toString()
,txtMobilePhone.getText(),txtHomeTelephone.getText()
,txtOfficeTelephone.getText(),g,txtEmail.getText()
,comboIcon.getSelectedIndex()+1
,comboProfession.getSelectedItem().toString()
,comboCity.getSelectedItem().toString()
,this.addressBookModel.getGroupEntry
(comboGroup.getSelectedItem().toString()));
}
return true;
}
private void clearInput()
{
this.txtName.setText("");
this.txtEmail.setText("");
this.comboSex.setSelectedIndex(0);
this.txtHomeTelephone.setValue(null);
this.txtMobilePhone.setValue(13);
this.txtNickname.setText("");
this.txtOfficeTelephone.setValue(null);
this.comboCity.setSelectedIndex(0);
this.comboDay.setSelectedIndex(0);
this.comboGroup.setSelectedIndex(0);
this.comboIcon.setSelectedIndex(0);
this.comboMonth.setSelectedIndex(0);
this.comboProfession.setSelectedIndex(0);
this.comboYear.setSelectedIndex(0);
}
class BtnOkL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
boolean b=checkInput();
if(b)
{
clearInput();
if(parent!=null)
parent.setVisible(false);
}
else
{
addressEntry=null;
}
}
}
class BtnCancelL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
clearInput();
addressEntry=null;
if(parent!=null)
{
parent.setVisible(false);
}
}
}
class BtnHelpL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
File file=new File("config"+File.separator+"help"
+File.separator+"help.txt");
if(file.exists())
{
new NotePad(file.getAbsolutePath()).setVisible(true);
}
}
}
protected void getFocus()
{
this.txtName.requestFocusInWindow();
}
private void updateDaysOfMonth()
{
Integer intYear=(Integer)comboYear.getSelectedItem();
int year=intYear.intValue();
int month=comboMonth.getSelectedIndex()+1;
GregorianCalendar g=new GregorianCalendar(year,1,1);
boolean leapYear=g.isLeapYear(year);
DefaultComboBoxModel model=(DefaultComboBoxModel)comboDay.getModel();
model.removeAllElements();
if(month==2)
{
int days=28;
if(leapYear)
{
days=29;
}
for(int i=1;i<=days;i++)
model.addElement(new Integer(i));
}
else if(month==1||month==3||month==5||month==7||month==8
||month==10||month==12)
{
for(int i=1;i<=31;i++)
model.addElement(new Integer(i));
}
else
{
for(int i=1;i<=30;i++)
model.addElement(new Integer(i));
}
}
class ComboFocusYearOrMonthL implements FocusListener
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
updateDaysOfMonth();
}
}
class ComboBoxYearOrMonthL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
updateDaysOfMonth();
}
}
}
/**
* a ComboBox model to show the years between now and 150 years before now
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
class YearComboBoxModel extends DefaultComboBoxModel
{
/**
*
*/
private static final long serialVersionUID = 1L;
public YearComboBoxModel()
{
GregorianCalendar g=(GregorianCalendar) GregorianCalendar.getInstance();
int year=g.get(GregorianCalendar.YEAR);
for(int i=year;i>year-151;i--)
{
this.addElement(new Integer(i));
}
}
}
/**
* a user defined ComboBoxModel for displaying images to be choosed by user
* @author Elegate
* @author cs department of NJU
*/
class ImageComboBoxModel extends DefaultComboBoxModel
{
private static final long serialVersionUID = 1L;
public ImageComboBoxModel()
{
for(int i=1;i<=86;i++)
{
addElement(new ImageIcon("config"+File.separator+"face"
+File.separator+i+"-1.gif"));
}
}
}
/**
* a simple email address format verifier
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
class EmailAddressFormatVerifier extends InputVerifier
{
public boolean verify(JComponent component)
{
JFormattedTextField field=(JFormattedTextField)component;
if(field.getText().length()==0)
return true;
try
{
Pattern pattern=Pattern.compile(".+@.+\\..+");
if(pattern.matcher(field.getText()).matches())
return true;
else
return false;
}
catch(Exception e)
{
return false;
}
}
}
/**
* a simple fixed line telephone number verifier
* @author Elegate,elegat@gmail.com
* @author cs department of NJU
*/
class FixedLineTelephoneNumberVerifier extends InputVerifier
{
public boolean verify(JComponent component)
{
JFormattedTextField field=(JFormattedTextField)component;
if(field.getText().length()==0)
return true;
try
{
Pattern pattern=Pattern.compile("[0-9]+-[0-9]+");
if(pattern.matcher(field.getText()).matches())
return true;
else
return false;
}
catch(Exception e)
{
return false;
}
}
}
/**
* a filter to accept only digit and '-'
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
class IntFilter extends DocumentFilter
{
public void insertString(FilterBypass fb,int offset,
String string,AttributeSet attr)
throws BadLocationException
{
int i=0;
while(i<string.length())
{
char ch=string.charAt(i);
if(Character.isDigit(ch)||ch=='-')i++;
else
{
break;
}
}
super.insertString(fb,offset,string.substring(0,i),attr);
}
public void replace(FilterBypass fb,int offset,int length,String string,AttributeSet attr)
throws BadLocationException
{
if(string!=null)
{
int i=0;
while(i<string.length())
{
char ch=string.charAt(i);
if(Character.isDigit(ch)||ch=='-')i++;
else
break;
}
string=string.substring(0,i);
}
super.replace(fb,offset,length,string,attr);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -