ITPub博客

首页 > Linux操作系统 > Linux操作系统 > SWing事件调用

SWing事件调用

原创 Linux操作系统 作者:mouqj 时间:2007-09-01 17:51:28 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: jar的使用
下一篇: Java界面编程
请登录后发表评论 登录
全部评论
  • 博文量
    33
  • 访问量
    116172