通过组合框将天数添加到Java文本字段中 [英] Add days to a Java textfield through combobox
问题描述
此处链接到GUI图像-------> http://imgur.com/uPD0K5S
LINK TO GUI IMAGE HERE -------> http://imgur.com/uPD0K5S
public class MainMenu extends javax.swing.JFrame {
public MainMenu() {
initComponents();
cmbRoomNumber.setEnabled(false);
jPanel1.setVisible(false);
btnBook.setEnabled(false);
//SETTING COMBOBOXES TO NONE
cmbPhotoId.setSelectedIndex(-1);
cmbStayDuration.setSelectedIndex(-1);
//LABELS VALIDATION
jlblNameVer.setVisible(false);
//SETTING DATE TODAY
Date now = new Date();
//Set date format as you want
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
this.ftxtCheckinDate.setText(sf.format(now));
}
如您所见,我想根据组合框(cmbStayDuration)中选择的天数,将天数添加到结帐日期(ftxtCheckOutDate)
As you can see i want to add days to Check-out Date(ftxtCheckOutDate) depending on how many days selected in the combobox(cmbStayDuration)
我正在使用netbeans JFrame
Im using netbeans JFrame
谢谢:)
private void cmbStayDurationActionPerformed(java.awt.event.ActionEvent evt) {
}
推荐答案
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, combobox number);
基本上,Calendar类具有添加日期的功能. 立即获取日期,获取组合框日期,然后添加日期.
Basically Calendar class has a function to add days. Get the date now, get the combo box day, then add it.
例如:
public static void main(String[] args) {
// TODO code application logic here
Calendar c = Calendar.getInstance();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(d);
System.out.println(sdf.format(c.getTime()));
c.setTime(d);
c.add(Calendar.DATE, 10);
System.out.println(sdf.format(c.getTime()));
}
输出:
05/11/2015
15/11/2015
关于随着ComboBox更改而更改签出日期"窗体的值,可以添加一个ActionListener来侦听它的更改. 示例
As for changing the value of Check-out Date form as the ComboBox changes, you can add either an ActionListener to listen to it change. Example
这篇关于通过组合框将天数添加到Java文本字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!