当我在 DatePicker 中选择日期时如何获取文本字段值
how to get the textfield value when i choose a date in DatePicker
我将其设为日期选择器 class,它扩展了 JFXPanel,因此我可以在我的 swing 应用程序中使用日期选择器功能。一切都很顺利,直到我陷入了如何将日期从文本字段获取到我的文本区域的问题。我尝试使用字符串字段 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);
}
}
有很多问题,但您的第一个问题是在 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;
}
接下来,我不会获得 LocalDate
的 String
表示,您应该根据需要获得对 LocalDate
和格式的引用。您还应该限制人们访问您的 class 字段并转而支持 getter
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);
}
好吧,我想通了..我按照 madpro 的建议做了,但是 FNAComponents class 内的 CompHandler class 声明中有一个错误,在那里我将 Jcomponents 初始化为它们各自的动作侦听器...
这是我所做的
private void init()
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
// date picker
DatePickin date_Picker = new DatePickin(); /// here i used the same object i created here in the CompHandler class as the arg
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(date_Picker); // here u can see i added the reference of DatePicker to the class as an arg
noChange_Button.addActionListener(compHandler);
}
我创建了一个对象 date_Picker 以在 init 方法中将面板放置在我的 swing 中。我将同一个对象用作 CompHandler class 的参数。感谢 madpro 的指导
我将其设为日期选择器 class,它扩展了 JFXPanel,因此我可以在我的 swing 应用程序中使用日期选择器功能。一切都很顺利,直到我陷入了如何将日期从文本字段获取到我的文本区域的问题。我尝试使用字符串字段 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);
}
}
有很多问题,但您的第一个问题是在 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;
}
接下来,我不会获得 LocalDate
的 String
表示,您应该根据需要获得对 LocalDate
和格式的引用。您还应该限制人们访问您的 class 字段并转而支持 getter
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);
}
好吧,我想通了..我按照 madpro 的建议做了,但是 FNAComponents class 内的 CompHandler class 声明中有一个错误,在那里我将 Jcomponents 初始化为它们各自的动作侦听器...
这是我所做的
private void init()
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10,10,10,10);
// date picker
DatePickin date_Picker = new DatePickin(); /// here i used the same object i created here in the CompHandler class as the arg
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(date_Picker); // here u can see i added the reference of DatePicker to the class as an arg
noChange_Button.addActionListener(compHandler);
}
我创建了一个对象 date_Picker 以在 init 方法中将面板放置在我的 swing 中。我将同一个对象用作 CompHandler class 的参数。感谢 madpro 的指导