第一次聽說監聽是三年前,做一個webGIS的項目,當時對Listener的印象就是個“監視器”,監視著界面的一舉一動,一有動靜就觸發對應的響應。
通過對界面的某一或某些操作添加監聽,可以自發地調用監聽函數/監聽類,對操作作出反應。舉個栗子:被監聽的操作是“你惹你媽生氣了”,添加的響應是“你媽給你爸打電話,你爸回家了,你爸打你一頓 ”。所以不管什麼時候,只要“你惹你媽生氣了”,都會觸發這個操作的監聽,最終結果是每次你都會被你爸打一頓。屢試不爽,無一例外。這就是事件監聽,java.util.EventListener。
Listener分很多種,但我總結不出來一個宏觀框架,這裡只把常用的CS監聽列出來講講,不足留待以後補充:ActionListener,MouseListener,PopMenuListener,windowListener等等。
如:給“保存按鈕”添加ActionListener監聽
1 saveCharaEditButton.addActionListener(new ActionListener() {
2
3 @Override
4 public void actionPerformed(ActionEvent e) {
5 // TODO Auto-generated method stub
6 save();
7 }
8 });
添加事件監聽時只需要 添加這個監聽類就ok了。【PS:這個內部類與函數並列寫就可以,不用另建類文件,只是前面注意不要加public修飾符。默認只能有一個public類。】
1 public JPanel setRightPanel(){
2 JPanel rightPanel = new JPanel();
3 double[][] size = {{15,TableLayout.FILL,30,TableLayout.FILL,35},{10,30,10,TableLayout.FILL}};
4 rightPanel.setLayout(new TableLayout(size));
5 JButton saveCharaEditButton = new JButton("保存編輯");
6 JButton enableCharaEditButton = new JButton("啟用編輯");
7 rightPanel.add(enableCharaEditButton, "1,1");
8 rightPanel.add(saveCharaEditButton, "3,1");
9 rightPanel.add(setCharaJCTablePanel(), "0,3,4,3");
10 saveCharaEditButton.addActionListener(new saveEditToFloodCharacterValueListener());//添加監聽
11 return rightPanel;
12 }
1 class saveEditToFloodCharacterValueListener implements ActionListener{
2
3 @Override
4 public void actionPerformed(ActionEvent e) {
5 // TODO Auto-generated method stub
6 FloodCharacterValue floodCharacterValue = new FloodCharacterValue();
7 floodCharacterValue = FloodCharacterValue.transFldPropToFloodCharacterValue(editCharaFldprop);
8 m_doc.m_floodExtractCharacterValue = floodCharacterValue;
9 }
10 }
首先說一下適配器和監聽接口的不同:new一個監聽接口,需要重載該接口下面的所有方法,你刪除任何一個,都會報錯;但是適配器可以只重載你需要的那個方法。
以windowListener為例,給對話框dialog添加監聽addWindowListerner,然後監聽內容為窗口適配器WindowAdapter。對比如下:
dialog.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
1 dialog.addWindowListener(new WindowAdapter() {
2 @Override
3 public void windowClosed(WindowEvent e) {
4 // TODO Auto-generated method stub
5 JDialog dialog2 = (JDialog) e.getSource();
6 set_infoSectionList(update_infoSectionListByProp(prop));
7 if (basinCheckList != null && !basinCheckList.isEmpty() && basinCheckList.get(0).getCHECKED()==1) {
8 tableCharaShowPanel.updateData(schemeType, get_infoSectionList(), false);
9 }else {
10 tableCharaShowPanel.updateData(schemeType, get_infoSectionList(), true);
11 }
12 super.windowClosed(e);
13 dialog2.dispose();
14 }
15 });
另外,關於添加哪些適配器,可以選擇WindowAdapter,右鍵菜單中選擇-resources-override/implement methods... - 勾選需要添加的方法即可。
四、小結
以上是一些Java事件監聽與使用方法的一些介紹,屬於入門級別。高級應用後期再予以學習與記錄。
寫於2017年1月6日