JDateChooser focusListener



我试图添加一个focusListener到JDateChooser,这样当用户选择出生日期时,当焦点丢失时,它会在文本字段上显示年龄。但这并没有奏效这是我尝试过的方法,

进口java.awt.BorderLayout;

public class agecalc extends JFrame {
    private JTextField age;
    private JDateChooser dOB;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    agecalc frame = new agecalc();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public agecalc() {
        JPanel panel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 290, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(134, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(20)
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(47, Short.MAX_VALUE))
        );
        panel.setLayout(null);
        dOB = new JDateChooser();
        dOB.setBounds(97, 52, 95, 20);
        panel.add(dOB);
        age = new JTextField();
        age.setBounds(97, 83, 86, 20);
        panel.add(age);
        age.setColumns(10);
        getContentPane().setLayout(groupLayout);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
                    AGECalc ageCalc=new AGECalc();
        dOB.addFocusListener(ageCalc);
    }
    private class AGECalc implements FocusListener{
        @Override
        public void focusGained(FocusEvent e) {         
        }
        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub
            Calendar fromDate;
               Calendar toDate;
               Date dateOfBirth= dOB.getDate();
               Date now = new Date();
               fromDate=Calendar.getInstance();
               toDate=Calendar.getInstance();
                   fromDate.setTime(dateOfBirth);
                   toDate.setTime(now);
                   int ageYear = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) );
                   age.setText(Integer.toString(ageYear));
                   System.out.println(ageYear);   

            }
        }



    public JTextField getAge() {
        return age;
    }
    public void setAge(JTextField age) {
        this.age = age;
    }
    public JDateChooser getdOB() {
        return dOB;
    }
    public void setdOB(JDateChooser dOB) {
        this.dOB = dOB;
    }
}

我试图添加一个focusListener到JDateChooser,所以当用户选择出生日期时,在文本字段中显示年龄注意力不集中

我建议您实现PropertyChangeListener并将其附加到日期选择器。这样,每当用户通过选择日期或手动输入新日期来更新所选值时,文本字段将被更新,而不管日期选择器是否失去焦点。

例如:

final JTextField ageTextField = new JTextField(20);
ageTextField.setEditable(false);
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.addPropertyChangeListener("date", new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        Calendar dateofBirth = Calendar.getInstance();
        dateofBirth.setTime((Date)evt.getNewValue());
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - dateofBirth.get(Calendar.YEAR);
        if(today.get(Calendar.MONTH) < dateofBirth.get(Calendar.MONTH) ||
          (today.get(Calendar.MONTH) == dateofBirth.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) < dateofBirth.get(Calendar.DAY_OF_MONTH))) {                    
            age--;
        }
        ageTextField.setText(String.valueOf(age));
    }
});

看这个问题的答案中的另一个例子:

    在JDialog中使用JCalendar

八卦

关于这行(以及其他类似的行):

dOB.setBounds(97, 52, 95, 20);

使用Swing时,强烈建议使用setBounds()setLocation()setSize()等方法,因为这些方法会导致组件的精确位置。布局管理器负责处理组件的大小和定位。这些都是用来强调这一点的有趣的话题:

  • 我应该避免使用set(Preferred|Maximum|Minimum)Size方法在Java Swing?
  • 在Swing GUI中提供留白
  • 嵌套布局方法

焦点侦听器未添加到JDateChooser

相关内容

  • 没有找到相关文章

最新更新