问题描述
我想双击JDateChooser使其启用.所以我使用了MouseListener:
I want to double click on a JDateChooser to make it enabled. So I use a MouseListener :
jDateChooser1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("mouse clicked");
}
});
但是此事件不会被触发,什么也没发生.
But this event doesn't get fired, nothing happend.
日期选择器是 com.toedter.calendar一个:
有什么建议吗?
JDateChooser是一个面板,我必须在面板中的组件上侦听鼠标事件. JDateChooser有一个getDateEditor(),女巫是文本字段.
The JDateChooser is a Panel, and I have to listen to a mouse event from on component in the panel. JDateChooser has a getDateEditor(), witch is the textfield.
这是解决方法:
this.jDateChooser1.getDateEditor().getUiComponent().addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount()==2){
Component c = ((Component)evt.getSource()).getParent();
c.setEnabled(!c.isEnabled());
}
}
});
推荐答案
JDateChooser类扩展了JPanel.我猜您正在单击的区域位于添加到根JPanel的另一个Container中.您应该尝试确定哪个容器是引发事件的容器,并将侦听器添加到其中.
The class JDateChooser extends JPanel. I guess that the area where you are clicking is found inside another Container that is added to the root JPanel. You should try to identify which Container is the one that fires the events and add the listener to it.
要测试这是否正确,请尝试将侦听器递归添加到所有容器,如果看到它被解雇,则可以删除侦听器的递归设置,并尝试找到需要添加MouseListener的侦听器中的哪一个到. (请注意,我无需测试即可直接编写代码,因此请纠正所有错误)
to test if this is correct, try to recursively add the listener to all containers and if you see that it gets fired, you can remove the recrusive setting of listeners and try to locate which one of them you need to add the MouseListener to. (Note i write the code directly without testing so please fix any mistake)
private void addMouseListenerRecrusively(Container container){
for (Component component:container.getComponents()){
if (component instanceof Container)
addMouseListenerRecrusively(component);
}
container.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("mouse clicked");
}
});
}
并在选择器上调用方法
addMouseListenerRecrusively(jDateChooser1);
这篇关于JDateChooser:不会触发MouseClicked事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!