对于一个程序,当按下按钮"1"时,我正在使用KeyListener向ArrayList添加内容.此列表中的对象不断可视化.使用KeyListener,即使按下按钮,这也能流畅地工作.
后来,我在GUI中添加了一个JMenuBar.向ArrayList添加内容现在有一个自己的JMenuItem,其加速器设置为KeyStroke'1',ActionListener执行与之前的KeyListener相同的东西.但是,现在的表现非常糟糕.按下'1'将会非常滞后,与KeyListener相比,速度非常慢.
为什么这么慢?难道我做错了什么?有没有更好的办法?
...
AL al = new AL();
menu.add(createMenuItem("Add", KeyEvent.VK_1, al));
}
private JMenuItem createMenuItem(String text, int key, ActionListener al){
JMenuItem menuItem = new JMenuItem(text);
menuItem.setAccelerator(KeyStroke.getKeyStroke(key, 0));
menuItem.addActionListener(al);
return menuItem;
}
private class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
int keycode = ((JMenuItem)e.getSource()).getAccelerator().getKeyCode();
bla(keycode);
}
}
Run Code Online (Sandbox Code Playgroud) 要获得上一个星期日的日期,可以使用date --date="last sun"Linux和date -j -v-sunBSD.
如果我有周数(1到7)而不是字符串,怎么办呢?我总是可以使用case语句将数字转换为字符串,但我敢打赌,有更好的解决方案吗?!
谢谢你的帮助.