java question!
compose a java class having relevant operation that shows a calender for a year and months. inpur for year and months are by parameter.
example:
SEPTEMBER 2007
sun mon tue wed thu fri sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
thanks
[解决办法]
Why not using chinese to ask question?
Here is a example.I hope it could suit to your requirement ! GOOD LUCK~-.-
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
public final class winDateChoose
extends JPanel {
private static final long serialVersionUID = 1L;
private static final int startX = 10;
private static final int startY = 60;
private static final Font smallFont = new Font( "Arial ", Font.PLAIN, 10);
private static final Font largeFont = new Font( "Arial ", Font.PLAIN, 12);
private static final Insets insets = new Insets(2, 2, 2, 2);
private static final Color highlight = new Color(255, 0, 0);
private static final Color white = new Color(255, 255, 255);
private static final Color gray = new Color(204, 204, 204);
private Component selectedDay = null;
private GregorianCalendar selectedDate = null;
private GregorianCalendar originalDate = null;
private boolean hideOnSelect = true;
private final JButton backButton = new JButton();
private final JLabel monthAndYear = new JLabel();
private final JButton forwardButton = new JButton();
private JDialog mParentWin;
public boolean mOkClick = false;
public boolean mTimeShowFlag = true;
private final JLabel[] dayHeadings = new JLabel[] {
new JLabel( "Sun "),
new JLabel( "Mon "),
new JLabel( "Tur "),
new JLabel( "Wen "),
new JLabel( "Thu "),
new JLabel( "Fri "),
new JLabel( "Sat ")};
private final JLabel[][] daysInMonth = new JLabel[][] {
{
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
, {
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
};
private final JButton todayButton = new JButton();
private final JButton cancelButton = new JButton();
//Time Panel
JSpinner Timespinner;
public winDateChoose() {
super();
selectedDate = getToday();
init();
}
public void setTimeShowFlag(boolean flag){
mTimeShowFlag = flag;
if(!flag){
this.remove(this.Timespinner);
this.repaint();
}
}
public void setParentWin(JDialog win) {
this.mParentWin = win;
}
public winDateChoose(final Date initialDate) {
super();
if (null == initialDate) {
selectedDate = getToday();
}
else {
(selectedDate = new GregorianCalendar()).setTime(initialDate);
}
originalDate = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DATE));
init();
}
public boolean isHideOnSelect() {
return hideOnSelect;
}
public void setHideOnSelect(final boolean hideOnSelect) {
if (this.hideOnSelect != hideOnSelect) {
this.hideOnSelect = hideOnSelect;
initButtons(false);
}
}
public Date getDate() {
if (null != selectedDate) {
return selectedDate.getTime();
}
return null;
}
public String getTime() {
if (this.Timespinner != null) {
String t = Timespinner.getValue().toString();
if (t != null) {
String[] tmp = t.split( " ");
String time = "0:0:0 ";
if (tmp.length > = 4) {
time = tmp[3];
}
return time;
}
}
return "0:0:0 ";
}
private void init() {
setLayout(null);
setBorder(null);
this.setMinimumSize(new Dimension(230, 250));
this.setMaximumSize(getMinimumSize());
this.setPreferredSize(getMinimumSize());
this.setBorder(new BorderUIResource.EtchedBorderUIResource());
backButton.setFont(smallFont);
backButton.setText( " < ");
backButton.setMargin(insets);
backButton.setDefaultCapable(false);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onBackClicked(evt);
}
});
//add(backButton, new AbsoluteConstraints(10, 10, 20, 20));
backButton.setBounds(30, 10, 30, 20);
add(backButton);
//add(backButton, new Border(10, 10, 20, 20));
monthAndYear.setFont(largeFont);
monthAndYear.setHorizontalAlignment(JTextField.CENTER);
monthAndYear.setText(formatDateText(selectedDate.getTime()));
//add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));
monthAndYear.setBounds(60, 10, 100, 20);
add(monthAndYear);
[解决办法]
是不是还有代码啊?我如果不写点,你的代码帖不完了吧。楼上的
[解决办法]
import java.util.Calendar;
public class TestCalender
{
public TestCalender(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month,1);
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println( "Sunday Monday Tuesday Wednesday Thursday Friday Saturday ");
for(int i=0;i <(week-1)%7;i++)
{
System.out.print( " ");
}
for(int i=1;i <=DaysInMonth(year,month);i++)
{
if((week+i-2)%7==0)
System.out.println();
if(i <=9)
{
System.out.print(i+ " ");
}
else
{
System.out.print(i+ " ");
}
}
}
public int DaysInMonth(int year,int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(year+1900,month+1,0);
return calendar.get(Calendar.DAY_OF_MONTH);
}
public static void main(String[]args)
{
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
System.out.println(year+ " "+month+ " ");
new TestCalender(year-1,month-1);
}
}
[解决办法]
it is good
[解决办法]
it is very good
[解决办法]
授人鱼不如授人以渔
光给代码,何时是个尽头,再遇到一个类似问题,又得过来问,再贴一大段代码。
楼主应该学会如何自定义组件,而不是老靠着别人给你写一段代码。
这种问题实在太多了,时间长了,谁都会失去兴趣的。
[解决办法]
哈哈,大家英文真好
[解决办法]
i fu le you
[解决办法]
please speak chinese,thanks!
[解决办法]
请问china8848(永远在一起) :
calendar.set(year+1900,month,1);的year+1900
new TestCalender(year-1,month-1);的year-1
分别是什么意思?
谢谢