📄 calenda.java
字号:
package two;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Calenda extends WindowAdapter implements ActionListener,ItemListener,TextListener{
private Choice c;
private TextArea t= new TextArea(8, 30);
private Frame f;
private Button b1;
private Button b2;
private TextField t1;
private Panel p = new Panel();
public Calenda()
{
f = new Frame("Calendar");
f.setLayout(new FlowLayout(FlowLayout.LEFT));
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.setSize(5,10);
b1 = new Button();
b2 = new Button();
t1 = new TextField("2005",10);
b1.setLabel("+");
b1.setSize(5,5);
b2.setSize(5,5);
b2.setLabel("-");
c = new Choice();
p.add(b1);
p.add(b2);
c.setSize(150,10);
t1.setSize(170,20);
t.setBackground(Color.cyan);
c.add("一月");
c.add("二月");
c.add("三月");
c.add("四月");
c.add("五月");
c.add("六月");
c.add("七月");
c.add("八月");
c.add("九月");
c.add("十月");
c.add("十一月");
c.add("十二月");
c.select(1);
f.add(c);
f.add(t1);
f.add(p);
f.add(t);
f.setResizable(false);
f.setSize(250,240);
f.setVisible(true);
t.setEditable(false);
t.setText(getString(Integer.parseInt(t1.getText()),positionmonth(c.getSelectedItem()),1));
b1.addActionListener(this);
b2.addActionListener(this);
c.addItemListener(this);
f.addWindowListener(this);
t1.addTextListener(this);
}
public String getString(int iselYear, int iselMonth, int day)
{
GregorianCalendar gc = (GregorianCalendar)GregorianCalendar.getInstance();
String result = "";
int todays = 30;
gc.set(iselYear,iselMonth-1,day);
result += "日 一 二 三 四 五 六";
iselMonth = iselMonth-1;
if(iselMonth==0 || iselMonth==2 || iselMonth==4 || iselMonth==6 || iselMonth== 7 ||iselMonth==9 || iselMonth==11)
{
todays=31;
}
if(iselMonth==3 || iselMonth==5 || iselMonth==8 || iselMonth==10)
{
todays=30;
}
if(gc.isLeapYear(iselYear)&& iselMonth==1)
{
todays=29;
}
if( !gc.isLeapYear(iselYear) && iselMonth==1)
{
todays=28;
}
int weeks=gc.get(Calendar.DAY_OF_WEEK)-1;
String[] count=new String[weeks+todays];
for(int i=0;i<weeks;i++)
{
count[i]=" ";
}
for(int i=weeks,n=1;i<weeks+todays;i++)
{
if(n<=9)
count[i]=" "+String.valueOf(n);
else
count[i]=String.valueOf(n);
n++;
}
for(int i=0;i<weeks+todays;i++)
{
if(i%7==0)
{
result += "\r\n\r\n";
result += count[i].toString()+ " ";
}
else
result += count[i].toString()+ " ";
}
return result;
}
public int positionmonth(String a)
{
if(a == "一月")
return 1;
if(a == "二月")
return 2;
if(a == "三月")
return 3;
if(a == "四月")
return 4;
if(a == "五月")
return 5;
if(a == "六月")
return 6;
if(a == "七月")
return 7;
if(a == "八月")
return 8;
if(a == "九月")
return 9;
if(a == "十月")
return 10;
if(a == "十一月")
return 11;
if(a == "十二月")
return 12;
return 0;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int temp = 0;
if(source == b1)
{
temp = Integer.parseInt(t1.getText());
temp = temp+1;
t1.setText(Integer.toString(temp));
t.setText(getString(temp,positionmonth(c.getSelectedItem()),1));
}
else if(source == b2)
{
temp = Integer.parseInt(t1.getText());
temp = temp-1;
t1.setText(Integer.toString(temp));
t.setText(getString(temp,positionmonth(c.getSelectedItem()),1));
}
}
public void itemStateChanged(ItemEvent e)
{
int temp = 0 ;
temp = Integer.parseInt(t1.getText());
t.setText(getString(temp,positionmonth(c.getSelectedItem()),1));
}
public void textValueChanged(TextEvent e)
{
Object source = e.getSource();
int temp = 0;
if(source == t1)
{
temp = Integer.parseInt(t1.getText());
t.setText(getString(temp,positionmonth(c.getSelectedItem()),1));
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String[] args)
{
@SuppressWarnings("unused")
Calenda calenda = new Calenda();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -