当前位置: 首页 > 知识库问答 >
问题:

在DatePicker中选择日期时如何获取textfield值

许子平
2023-03-14

我将它创建为一个datepicker类,它扩展了JFXPanel,以便在swing应用程序中使用datepicker特性。一切都很顺利,直到我被困在如何将日期从文本字段输入到我的文本区…我尝试使用字符串字段n存储了使用toString()方法分配给它的LocalDate对象,但它总是返回为NULL。

代码如下:

public class FNAFrame extends JFrame {

public FNAFrame()
{
    super ("FNA Comments Generator");
    setLayout(new BorderLayout());

    setResizable(false);
    TextFrame comps = new TextFrame();
    add(comps);
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                // 
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
            {
                ex.printStackTrace();
            }


            new FNAFrame();

        }
    });
 }  
} // end of class FNA Frame


public class TextFrame extends JPanel {
// variable declarations
private JLabel newbLabel;

private JButton noChange_Button;

private JTextArea display_Area;

// end of variable declarations

public TextFrame()
{
    super(new GridBagLayout());

    setPreferredSize(new Dimension(300,200));
    setBackground(Color.white);

    init();  
  } // end of class constructor

    private void init()
    { 
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);

        // date picker
        DatePickin date_Picker = new DatePickin();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.EAST;
        add(date_Picker, gbc);

        // button to display date in textarea
        noChange_Button = new JButton("No Change");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        add(noChange_Button, gbc);

        ///////////////////// TEXT AREA ///////////////////////

        display_Area = new JTextArea();
        gbc.gridx = 0;
        gbc.gridy = 3;
        //gbc.weighty = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTHWEST;
        display_Area.setEditable(true);
        display_Area.setLineWrap(true);
        display_Area.setWrapStyleWord(true);

        JScrollPane scroll = new JScrollPane(display_Area);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setPreferredSize(new Dimension(0, 70));
        add(scroll, gbc);

        // adding listeners to components
        // registering all components with their respective listeners
        CompHandler compHandler = new CompHandler();
        noChange_Button.addActionListener(compHandler);
    }

    // class to handle text fields
    private class CompHandler implements ActionListener
    {
        DatePickin date = new DatePickin();

        private String newbDate;

        @Override
        public void actionPerformed(ActionEvent e)  
        {
            Object button_command = e.getActionCommand();

            try {

                if (button_command.equals("No Change"))
                {    
                    newbDate = date.strDate;

                    display_Area.setText("The date is " + newbDate);
                    display_Area.setFont(new Font("Serif", Font.BOLD, 18));
                    display_Area.setForeground(Color.black);
                }
            }
            catch (NullPointerException np)
            {
            }       
        }
       } // end component handler class 
} // end of TextFrame class


public class DatePickin extends javafx.embed.swing.JFXPanel{

private DatePicker date_Picker;
String strDate;
private VBox pane;

public DatePickin()
{
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(90, 30));

    init();   
} // end of class constructor

private void init()
{
    pane = new VBox();
    pane.setBackground(Background.EMPTY);
    pane.setAlignment(Pos.CENTER_LEFT);
    getDate();
    pane.getChildren().addAll(date_Picker);

    Platform.runLater(this::createScene);
}

public void getDate()
{
    date_Picker = new DatePicker();

    date_Picker.setShowWeekNumbers(false);

    date_Picker.setOnAction((e) -> {

        LocalDate ld;
        try
        {
            //This is where i the problem is
            ld = date_Picker.getValue();

            strDate = ld.toString();
        }
        catch(UnsupportedOperationException uoe)
        {
        }
    });
}

private void createScene()
{
    Scene scene = new Scene(pane);
    setScene(scene);
}    

}

共有1个答案

简意
2023-03-14

有很多问题,但首先是在Comphandler中创建DatePickin的新实例

private class CompHandler implements ActionListener
{
    DatePickin date = new DatePickin();

这与屏幕上的datepickin实例无关,因此它将始终为null

相反,您应该将datepickin的引用传递给comphandler

private class CompHandler implements ActionListener {

    private DatePickin pickin;

    public CompHandler(DatePickin pickin) {
        this.pickin = pickin;
    }

接下来,我不会获得localdatestring表示形式,您应该获得对localdate的引用,并根据需要设置格式。您还应该限制人们能够访问您的类字段,而选择getters

public class DatePickin extends javafx.embed.swing.JFXPanel {

    private DatePicker date_Picker;
    private LocalDate dateValue;
    //...

    public void getDate() {
        date_Picker = new DatePicker();
        date_Picker.setShowWeekNumbers(false);
        date_Picker.setOnAction((e) -> {

            try {
                dateValue = date_Picker.getValue();
            } catch (UnsupportedOperationException uoe) {
                uoe.printStackTrace();
            }
        });
    }

    public LocalDate getDateValue() {
        return dateValue;
    }

然后,单击no change按钮时,您只需获取localdate的值,并根据需要进行操作...

if (button_command.equals("No Change")) {
    LocalDate newbDate = pickin.getDateValue();

    display_Area.setText("The date is " + newbDate);
    display_Area.setFont(new Font("Serif", Font.BOLD, 18));
    display_Area.setForeground(Color.black);
}
 类似资料:
  • datePicker 日期选择或者时间选择 使用方法 AlipayJSBridge.call('datePicker', { beginDate: '2016-10-10', minDate: '2016-10-9', maxDate: '2017-10-9', mode: 1, }, function(e) { alert(JSON.stringify(e)); });

  • 日期选择组件。滚动选择交互,基于 Scrollpicker 实现。 Usage 全部引入 import { Datepicker } from 'beeshell'; 按需引入 import Datepicker from 'beeshell/dist/components/Datepicker'; Examples Datepicker 与 BottomModal 组合应用 Code 详细

  • 是否可以允许datePicker只显示日期而隐藏年和月? 我有两个活动,在活动A中,我有datePicker,它只显示月份和年份。在活动B中,我希望datePicker只显示日期,但无法从Internet上找到任何解决方案。 活动A截图 活动B

  • DatePicker 日期选择器 用于选择或输入日期 选择日 以「日」为基本单位,基础的日期选择控件 基本单位由type属性指定。快捷选项需配置picker-options对象中的shortcuts,禁用日期通过 disabledDate 设置,传入函数 <template> <div class="block"> <span class="demonstration">默认</spa

  • 用于选择或输入日期 选择日 以「日」为基本单位,基础的日期选择控件 基本单位由type属性指定。通过shortcuts配置快捷选项,禁用日期通过 disabledDate 设置,传入函数 <template> <div class="block"> <span class="demonstration">默认</span> <el-date-picker v-mod

  • 用于选择或输入日期 选择日 以「日」为基本单位,基础的日期选择控件 你可以在 [model] 中定义一个初始值,如果留空,则优先显示当前时间。 <el-date-picker (modelChange)="handle($event)" (clear-click)="clearClickHandle($event)"> </el-date-picker> <script type="text"