SWing事件呼叫

mouqj發表於2007-09-01
1,事件響應:

testButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JButton source = (JButton)e.getSource();

source.setText("ok");

}

});

2,回撥

ButtonAction action = new ButtonAction();

action.putValue(AbstractAction.NAME,"action");

testButton.setAction(action);

.............................

.........................

class ButtonAction extends AbstractAction{

public void actionPerformed(ActionEvent e ){

System.out.println("clicked");

}

例子:回撥程式碼

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
* Date: 2007-4-23
* Time: 22:51:38
* 管理分頁的按鈕
* @author MouChen
* @version 1.0
*/
public class PageBeanBar extends JToolBar{
DataOperateFrame container;
JButton rightButton = new JButton(),
leftButton = new JButton(),
firstButton = new JButton(),
endButton = new JButton();
//runPageButton = new JButton();
ImageIcon leftImg,
rightImg,
firstImg,
endImg,
runPageImg;

//JTextField pageNumField = new JTextField(4);
JComboBox sqlStrBox = new JComboBox();

public PageBeanBar(DataOperateFrame container){
this.container = container;
unit();
}

public void unit(){
// leftImg = new ImageIcon(dataconn.view.IconMenuBar.class.getResource("images/left.png"));
// rightImg = new ImageIcon(dataconn.view.IconMenuBar.class.getResource("images/right.png"));
// firstImg = new ImageIcon(dataconn.view.IconMenuBar.class.getResource("images/first.png"));
// endImg = new ImageIcon(dataconn.view.IconMenuBar.class.getResource("images/end.png"));
// runPageImg = new ImageIcon(dataconn.view.IconMenuBar.class.getResource("images/run_page.png"));

leftButton.setToolTipText("向前一頁");
leftButton.setAction(new RunPageAction(1,dataconn.view.IconMenuBar.class.getResource("images/left.png").getPath()));

rightButton.setToolTipText("向後一頁");
rightButton.setAction(new RunPageAction(2,dataconn.view.IconMenuBar.class.getResource("images/right.png").getPath()));

firstButton.setToolTipText("第一頁");
firstButton.setAction(new RunPageAction(3,dataconn.view.IconMenuBar.class.getResource("images/first.png").getPath()));

endButton.setToolTipText("最後一頁");
endButton.setAction(new RunPageAction(4,dataconn.view.IconMenuBar.class.getResource("images/end.png").getPath()));

//runPageButton.setIcon(runPageImg);
//runPageButton.setToolTipText("執行");
//runPageButton.addActionListener(new SaveFile_ActionAdapter(container,2));

this.add(firstButton);
this.add(leftButton);
// this.add(pageNumField);
// this.add(runPageButton);
this.add(rightButton);
this.add(endButton);
this.addSeparator();
this.add(sqlStrBox);
}

class RunPageAction extends AbstractAction {
private String errorMsg;
private int code;

public RunPageAction(int code,String path) { //1, leftImg ;2, rightImg; 3,firstImg; 4 endImg
super("",new ImageIcon(path));
this.code = code;
errorMsg = getErrorMsg();
}
//動作的處理程式碼
public void actionPerformed(ActionEvent e) {
if(errorMsg != null)
JOptionPane.showMessageDialog(null,errorMsg,"查詢錯誤",JOptionPane.WARNING_MESSAGE);
//else
}
public String getErrorMsg(){
if(DataUtil.TOTAL_ROWS == -1)
return "當前未執行任何查詢";

if(code == 1 || code == 3)
if(DataUtil.CUR_PAGE == 1)
return "當前為第一頁";
else if(code==3)
DataUtil.CUR_PAGE = 1;
else
DataUtil.CUR_PAGE--;
if(code == 2 || code == 4)
if(DataUtil.CUR_PAGE == DataUtil.MAX_PAGE)
return "當前為最後頁";
else if(code == 2)
DataUtil.CUR_PAGE++;
else
DataUtil.CUR_PAGE = DataUtil.MAX_PAGE;

return null;
}
}


}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11049438/viewspace-967517/,如需轉載,請註明出處,否則將追究法律責任。

相關文章