java日历代码大全 java编写一个日历( 五 )


return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
c.set(Calendar.DAY_OF_MONTH, newDay);
}
public void setDate(Date date) {
jFormattedTextField.setText(getDefaultDateFormat().format(date));
}
public Date getDate() {
try {
String dateString = jFormattedTextField.getText();
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
} catch (Exception ee) {
return getNowDate();
}
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy-MM-ddHH:mm");
}
}
测试程序:
package Demo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoForDateChooser extends JFrame {
private JFormattedTextField formattedTextField;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("日期选择框");
getContentPane().setLayout(null);
setBounds(100, 100, 383, 137);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 375, 107);
getContentPane().add(panel);
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(68, 48, 175, 23);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateChooser mDateChooser=new DateChooser(formattedTextField);
Point p = button.getLocationOnScreen();
p.y = p.y + 30;
mDateChooser.showDateChooser(p);
formattedTextField.requestFocusInWindow();
}
});
button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button);
final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label);
panel.add(formattedTextField);
//
}
}
关于java日历代码大全和java编写一个日历的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读