使用NetBeans自动填充jTextField [英] Auto Populate a jTextField with NetBeans

查看:77
本文介绍了使用NetBeans自动填充jTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用NetBeans创建了一个jFrame,当单击主GUI上的按钮以添加新条目时,该jFrame将打开.我想知道是否有一种方法可以为显示jFrame表单时显示的每个新条目提供唯一的ID.除了唯一的ID外,我还希望有一个createdOn文本字段,该文本字段会自动填充当前日期.

I have created a jFrame using NetBeans that opens when a button on the main GUI is clicked to add a new entry. I want to know if there is a way to supply a unique id for each new entry that shows when the jFrame form is displayed. Along with a unique id I want to also have a text field createdOn that is auto populated with the current date.

推荐答案

在执行该程序的JVM的生命周期中, hashCode()可以用作唯一的ID. UUID 是另一种选择.该示例在每次按下按钮时显示一个新的 Date .

For the life of the JVM executing the program, hashCode() may serve as a a unique ID; UUID is an alternative. The example shows a new Date each time the button is pressed.

附录:仔细检查后,不要求,如果两个对象根据equals(java.lang.Object)方法不相等,则分别对每个对象调用 hashCode 方法两个对象必须产生不同的整数结果."您也许可以使用 getTime()中的 long ,但以毫秒为单位.

Addendum: On closer scrutiny, the hashCode() method of java.util.Date may not be unique. In particular, "It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results." You may be able to use the long from getTime(), subject to the one millisecond resolution.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/questions/4128432 */
public class AddTest extends JPanel {

    private static final DateFormat format =
        new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss.SSS");
    private final List<TestPanel> panels = new ArrayList<TestPanel>();

    public AddTest() {
        this.setLayout(new GridLayout(0, 1));
        TestPanel tp = new TestPanel();
        panels.add(tp);
        this.add(tp);
        this.validate();
        Dimension d = tp.getPreferredSize();
        this.setPreferredSize(new Dimension(d.width, d.height * 8));
    }

    private static class TestPanel extends JPanel {

        public TestPanel() {
            Date date = new Date();
            this.add(new JLabel(String.valueOf(date.hashCode())));
            this.add(new JTextField(format.format(date)));
        }
    }

    private void display() {
        JFrame f = new JFrame("AddTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this, BorderLayout.CENTER);
        JButton button = new JButton(new AbstractAction("New") {

            @Override
            public void actionPerformed(ActionEvent e) {
                TestPanel tp = new TestPanel();
                panels.add(tp);
                AddTest.this.add(tp);
                AddTest.this.revalidate();
                AddTest.this.repaint(); // may be required
            }
        });
        f.add(button, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new AddTest().display();
            }
        });
    }
}

这篇关于使用NetBeans自动填充jTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆