鼠标事件
//在按钮上添加一个鼠标事件与活动监听时,鼠标事件先发生
button.addMouseListener(new MouseAdapter()//新建
覆写方法有
eg: mouseEntered//鼠标进入按钮时
MouseEvent有获取点击次数方法,可实现双击功能
键盘事件
//基本同上
button.addKeyListener(new KeyAdapter()//新建
覆写方法eg:
keyPressed()//键盘按下时触发对应事件
KeyEvent类方法有:
//KeyEvent下将键盘按键都封装成为对应的常量
isControDown()//判断是否在ctrl情况下的组合键
getCode()//获取引起事件的键盘录入
可应用于文本框判断输入字符是否违法
rField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)) {
System.out.println("输入非法");
e.consume();
}
}
});
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)) {
System.out.println("输入非法");
e.consume();
}
}
});
Dialog
//对话框,一般用于当用户进行某种不当操作的提示信息
//自身为一个窗体,所以可使用add方法
//当需要其浮现时只需要修改setVisivle方法为(true)
//一般其文字信息用lable显示
dialog= new Dialog(frame,"提示信息-selg",true);//通过其与frame窗体连接
dialog.setBounds(200,200,200, 200);
dialog.setLayout(new FlowLayout());
dialog.add(label);
dialog.add(okButton);
dialog.setBounds(200,200,200, 200);
dialog.setLayout(new FlowLayout());
dialog.add(label);
dialog.add(okButton);
FileDialog
//文件对话框,方便操作
openDia = new FileDialog(frame, "我要打开", mode);
//当mode为
FileDialog.LOAD时,启用文件对话框的打开功能
//当mode为
FileDialog.SAVE时,启用文件对话框的保存功能
//提供getDirectory和getFile操作当前目录
练习:
//通过按钮的活动监听,textField.getText(),textArea.append()可列出指定目录下的所有文件
//当出现错误时,通过dialog显示警告信息
private void myEvent() {
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dialog.setVisible(false);
}
});
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
//也可以键盘为源,当输入回车时,自动检索
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dirpath = textField.getText();
File dir = new File(dirpath);
if(dir.exists()&&dir.isDirectory()) {
textArea.setText("");
String[] names = dir.list();
for(String name:names) {
textArea.append(name+"\r\n");
}
}
else {
String info = "您输入的信息"+dirpath+"有误";
label.setText(info);
dialog.setVisible(true);
}
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void windowClosing(WindowEvent e) {
dialog.setVisible(false);
}
});
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
//也可以键盘为源,当输入回车时,自动检索
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dirpath = textField.getText();
File dir = new File(dirpath);
if(dir.exists()&&dir.isDirectory()) {
textArea.setText("");
String[] names = dir.list();
for(String name:names) {
textArea.append(name+"\r\n");
}
}
else {
String info = "您输入的信息"+dirpath+"有误";
label.setText(info);
dialog.setVisible(true);
}
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
菜单
//菜单的层次关系MenuBar->Menu->MenuItem
//Menu上还可以添加Menu->多级菜单
subMenu.add(subItem);
menu.add(subMenu);
menu.add(subItem);
menu.add(closeItem);
mBar.add(menu);
//MenuItem上可添加活动监听