導航:首頁 > 編程語言 > javaswing代碼

javaswing代碼

發布時間:2023-06-29 22:38:59

㈠ 求一個java swing帶界面的萬年歷代碼

按照你的要求編寫的Java swing 帶界面的萬年歷代碼如下

//日歷
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
JButtonjb1=newJButton("<<");
JButtonjb2=newJButton("<");
JButtonjb3=newJButton(">");
JButtonjb4=newJButton(">>");
JPaneljp1=newJPanel();
JPaneljp2=newJPanel();
JPaneljp3=newJPanel();
JPaneljp4=newJPanel();
JLabeljl1=newJLabel();
JLabeljl2=newJLabel();
JLabel[]jl=newJLabel[49];
String[]week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendarc=Calendar.getInstance();
intyear,month,day;
intnowyear,nowmonth,nowday;
CCI(){
super("簡單日歷");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
Strings=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(newFont(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(newFont(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==jb1){
year=year-1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
publicstaticvoidmain(String[]args){
newCCI();
}
publicintgetMonthDays(intyear,intmonth){
switch(month){
case1:
case3:
case5:
case7:
case8:
case10:
case12:
return31;
case2:
if((year%4==0&&year%100!=0)||year%400==0){
return29;
}else{
return28;
}
default:
return30;
}
}
publicvoidcreateMonthPanel(){
c.set(year,month-1,getMonthDays(year,month));
intweekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(newGridLayout(7,7));
jp3.setBounds(50,20,420,350);
}else{
jp3.setLayout(newGridLayout(6,7));
jp3.setBounds(50,20,420,300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(inti=0;i<7;i++){
jl[i]=newJLabel(week[i],JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year,month-1,1);
intemptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
intdaysOfMonth=getMonthDays(year,month);
for(inti=6+emptyFirst;i>=7;i--){
intintyear=year;
intintmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
intintdays=getMonthDays(intyear,intmonth);
jl[i]=newJLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(inti=7+emptyFirst;i<daysOfMonth+7+emptyFirst;i++){
jl[i]=newJLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
if((i+1)%7==0||(i+1)%7==1){
jl[i].setForeground(Color.RED);
}elseif((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);

jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(inti=48;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(inti=41;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
閱讀全文

與javaswing代碼相關的資料

熱點內容
安卓手機電話簿怎麼導出到蘋果手機 瀏覽:761
php實現投票 瀏覽:331
手機爆力解壓加密視頻文件 瀏覽:930
東方財富app怎麼看北上資金圖解 瀏覽:416
邢昭林程序員那麼可愛拍現場 瀏覽:169
安卓什麼應用可以免費看電視劇 瀏覽:504
合適pdf 瀏覽:293
app監測睡眠怎麼選擇 瀏覽:642
老人家用什麼安卓手機好 瀏覽:955
解壓包能不能送女朋友 瀏覽:701
好看發卡網源碼 瀏覽:51
水平集演算法matlab 瀏覽:769
區域網如何用ftp伺服器配置 瀏覽:74
程序員慣性思考模式 瀏覽:441
如何在個稅app上查身份證號 瀏覽:7
電視家app安裝在電視上怎麼安 瀏覽:889
怎麼將pdf格式轉化為圖片格式 瀏覽:639
伺服器拔掉raid卡怎麼裝系統 瀏覽:234
區域對稱加密演算法 瀏覽:247
數字轉漢字php 瀏覽:735