一、让对话框具备退出功能按键(窗口监听):
dialog.addWindowListener(new WindowAdapter( ){
public void windowClosing( WindowEvent e ){ //点击对话框X键时退出
dialog.setVisible( false ); // 让对话框不再显示
});
二、上例的活动监听:
dialog.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //点击对话框X键时退出
System.out.println("退出");
System.exit(0);
}
});
三、按钮在被鼠标进入和点击时显示输出(鼠标监听):
button.addMouseListener(new MouseAdapter(){
private int count=1;
private int clickCount=1;
public void mouseEntered(MouseEvent e) { //鼠标进入按钮区域时输出
System.out.println("鼠标进入该组件"+count++);
}
public void mouseClicked(MouseEvent e){ //鼠标点击两次时输出
if(e.getClickCount()==2)
System.out.println("双击动作"+clickCount++);
四、键盘输入数据进行相应操作(键盘监听):
button.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
System.out.println(e.getKeyChar()+"...."+e.getKeyCode());
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//触发Ctrl+Enter键后按钮确认退出
System.exit(0);
}
});
|
|