gpt4 book ai didi

java - 如何实现JDatePicker的FocusListener?

转载 作者:行者123 更新时间:2023-12-01 11:29:06 29 4
gpt4 key购买 nike

我想在JDatePickerFocusEvent中执行一些功能。我正在使用下面的代码来实现 FocusListener

Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
UtilDateModel model = new UtilDateModel();
Calendar today=Calendar.getInstance();
Date todayDate=new Date();
today.setTime(todayDate);
model.setDate(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
model.setSelected(true);
JDatePanelImpl datePanel =new JDatePanelImpl(model, p);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel,new DateLabelFormatter());

datePicker.addFocusListener(new FocusListener() {

@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println("fcus lost");
}

@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
System.out.println("focus gained");
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
//repaint();
displayImage(categoryAttributeObj,imGroupObj);
}

});
}
});

此代码不起作用。这段代码有错误吗?

最佳答案

由于一些个人原因,我并不是 JDatePicker 的特别粉丝。

您可以实现自己的版本,为您提供所需的功能,或者您可以尝试 SwingLabs、SwingX JXDatePicker,例如

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.JXDatePicker;

public class Main {

public static void main(String[] args) {
new Main();
}

public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
add(new JButton("Before"));
JXDatePicker picker = new JXDatePicker();
picker.getEditor().addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
System.out.println("You have foucs");
}
});
add(picker);
add(new JButton("After"));
}

}
}

关于java - 如何实现JDatePicker的FocusListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30567744/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com