关于Java的一个小程序,懂得来帮个忙

我是刚学JAVA的新手,老师给留了一个作业,我怎么想也想不出来,希望在这里能有高手朋友给解答一下,谢谢了内容是:用JAVA作一个图形界面,其功能是输入年份,在用界面上的小... 我是刚学JAVA的新手,老师给留了一个作业,我怎么想也想不出来,希望在这里能有高手朋友给解答一下,谢谢了
内容是:
用JAVA作一个图形界面,其功能是输入年份,在用界面上的小数字键盘输入天数,就能计算出是这一年的几月几号星期几,并且还能计算出是不是润年。
有懂的帮个忙啊,要源代码啊,谢谢了
展开
 我来答
dakiler
2006-09-16 · TA获得超过456个赞
知道小有建树答主
回答量:324
采纳率:0%
帮助的人:341万
展开全部
这是我编过的程序
它用来计算两个日期之间间隔
某日期向前/后几天是什么日期(考虑闰年)
星期几不好算,肯定要找个基准点

import java.awt.*;
import java.awt.event.*;

public class dakiler
{
public static void main(String args[])
{
new mainpage();
}
}

class mainpage extends Frame implements ActionListener
{
private Button button2,button3,button4;
forward frame1;
backward frame2;
twodates frame3;
mainpage()
{
super("主选择页面");
this.setLayout(new FlowLayout());
this.setLocation(250,250);

button2=new Button("日期向后计算");
button3=new Button("日期向前计算");
button4=new Button("两个日期计算");

button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

this.add(button2);
this.add(button3);
this.add(button4);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

this.pack();
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2)
{
this.setLocation(0,0);
forward frame1=new forward();

}
if(e.getSource()==button3)
{
this.setLocation(0,0);
backward frame2=new backward();
}

if(e.getSource()==button4)
{
this.setLocation(0,0);
twodates frame3=new twodates();
}
this.pack();
}
}

*/

class mainpage extends Frame implements ItemListener
{
private Choice choice;
private Label label1;
forward frame1;
backward frame2;
twodates frame3;
mainpage () {
this.setLayout(null);
this.setBounds(250,250,150,90);

choice=new Choice();
label1=new Label("请选择");

choice.addItem("日期向后计算");
choice.addItem("日期向前计算");
choice.addItem("两个日期计算");

choice.addItemListener(this);
label1.setBounds(20,30,100,20);
choice.setBounds(20,50,100,20);
this.add(label1);
this.add(choice);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

this.show();
}

public void itemStateChanged(ItemEvent e)
{
if(choice.getSelectedItem().equals("日期向后计算"))
{
this.setLocation(0,0);
if (frame1!=null) frame1.dispose();
if (frame2!=null) frame2.dispose();
if (frame3!=null) frame3.dispose();
frame1=new forward();
}
if(choice.getSelectedItem().equals("日期向前计算"))
{
this.setLocation(0,0);
if (frame1!=null) frame1.dispose();
if (frame2!=null) frame2.dispose();
if (frame3!=null) frame3.dispose();
frame2=new backward();
}
if(choice.getSelectedItem().equals("两个日期计算"))
{
this.setLocation(0,0);
if (frame1!=null) frame1.dispose();
if (frame2!=null) frame2.dispose();
if (frame3!=null) frame3.dispose();
frame3=new twodates();
}
}

}

class forward extends Frame implements ActionListener
{
private Button button5,button6;
private TextField yeartextfield,monthtextfield,daytextfield,timetextfield,answertextfield;
private Label yearlabel,monthlabel,daylabel,timelabel,answerlabel;
private double year,month,day,time;
private boolean isren,iscontinue;
forward()
{
super("日期向后计算");
this.setLayout(null);
this.setBounds(0,250,800,150);
this.setResizable(false);

button5=new Button("确定");
button6=new Button("清除");

yearlabel=new Label("请输入年份");
monthlabel=new Label("请输入月份");
daylabel=new Label("请输入日期");
timelabel=new Label("请输入向后几天");
answerlabel=new Label("答案是");

yeartextfield=new TextField(5);
monthtextfield=new TextField(5);
daytextfield=new TextField(5);
timetextfield=new TextField(5);
answertextfield=new TextField(20);

button5.addActionListener(this);
button6.addActionListener(this);

yeartextfield.addActionListener(this);
monthtextfield.addActionListener(this);
daytextfield.addActionListener(this);
timetextfield.addActionListener(this);

yearlabel.setBounds(0,50,100,20);
yeartextfield.setBounds(100,50,100,20);
monthlabel.setBounds(0,80,100,20);
monthtextfield.setBounds(100,80,100,20);
daylabel.setBounds(0,110,100,20);
daytextfield.setBounds(100,110,100,20);
timelabel.setBounds(200,80,100,20);
timetextfield.setBounds(300,80,100,20);
button5.setBounds(400,80,100,20);
button6.setBounds(500,80,100,20);
answerlabel.setBounds(600,80,100,20);
answertextfield.setBounds(700,80,100,20);

this.add(yearlabel);
this.add(yeartextfield);
this.add(monthlabel);
this.add(monthtextfield);
this.add(daylabel);
this.add(daytextfield);
this.add(timelabel);
this.add(timetextfield);
this.add(button5);
this.add(button6);
this.add(answerlabel);
this.add(answertextfield);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
);

this.show();
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button6)
{
yeartextfield.setText("");
monthtextfield.setText("");
daytextfield.setText("");
timetextfield.setText("");
answertextfield.setText("");
}
else if((e.getSource()==button5)||(e.getSource()==yeartextfield)||(e.getSource()==monthtextfield)||(e.getSource()==daytextfield)||(e.getSource()==timetextfield))
{

try
{
year=Double.parseDouble(yeartextfield.getText());
month=Double.parseDouble(monthtextfield.getText());
day=Double.parseDouble(daytextfield.getText());
time=Double.parseDouble(timetextfield.getText());
}
catch (Exception exc)
{
iscontinue=false;
answertextfield.setText("输入不正确");
}
function func=new function();
isren=func.ren(year);
iscontinue=false;
iscontinue=func.conti(month,day,isren);

if(iscontinue)
{
String str=func.plus(year,month,day,time);
answertextfield.setText(str);
}
else
{ answertextfield.setText("输入不正确");
}
}

}
}

class backward extends Frame implements ActionListener
{
private Button button5,button6;
private TextField yeartextfield,monthtextfield,daytextfield,timetextfield,answertextfield;
private Label yearlabel,monthlabel,daylabel,timelabel,answerlabel;
private double year,month,day,time;
private boolean isren,iscontinue;
backward()
{
super("日期向前计算");
this.setLayout(null);
this.setBounds(0,250,800,150);
this.setResizable(false);

button5=new Button("确定");
button6=new Button("清除");

yearlabel=new Label("请输入年份");
monthlabel=new Label("请输入月份");
daylabel=new Label("请输入日期");
timelabel=new Label("请输入向前几天");
answerlabel=new Label("答案是");

yeartextfield=new TextField(5);
monthtextfield=new TextField(5);
daytextfield=new TextField(5);
timetextfield=new TextField(5);
answertextfield=new TextField(20);

button5.addActionListener(this);
button6.addActionListener(this);
yeartextfield.addActionListener(this);
monthtextfield.addActionListener(this);
daytextfield.addActionListener(this);
timetextfield.addActionListener(this);

yearlabel.setBounds(0,50,100,20);
yeartextfield.setBounds(100,50,100,20);
monthlabel.setBounds(0,80,100,20);
monthtextfield.setBounds(100,80,100,20);
daylabel.setBounds(0,110,100,20);
daytextfield.setBounds(100,110,100,20);
timelabel.setBounds(200,80,100,20);
timetextfield.setBounds(300,80,100,20);
button5.setBounds(400,80,100,20);
button6.setBounds(500,80,100,20);
answerlabel.setBounds(600,80,100,20);
answertextfield.setBounds(700,80,100,20);

this.add(yearlabel);
this.add(yeartextfield);
this.add(monthlabel);
this.add(monthtextfield);
this.add(daylabel);
this.add(daytextfield);
this.add(timelabel);
this.add(timetextfield);
this.add(button5);
this.add(button6);
this.add(answerlabel);
this.add(answertextfield);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
);

this.show();
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button6)
{
yeartextfield.setText("");
monthtextfield.setText("");
daytextfield.setText("");
timetextfield.setText("");
answertextfield.setText("");
}
else if((e.getSource()==button5)||(e.getSource()==yeartextfield)||(e.getSource()==monthtextfield)||(e.getSource()==daytextfield)||(e.getSource()==timetextfield))
{

try
{
year=Double.parseDouble(yeartextfield.getText());
month=Double.parseDouble(monthtextfield.getText());
day=Double.parseDouble(daytextfield.getText());
time=Double.parseDouble(timetextfield.getText());
}
catch (Exception exc)
{
iscontinue=false;
answertextfield.setText("输入不正确");
}
function func=new function();
isren=func.ren(year);
iscontinue=false;
iscontinue=func.conti(month,day,isren);

if(iscontinue)
{
String str=func.minus(year,month,day,time);
answertextfield.setText(str);
}
else
{ answertextfield.setText("输入不正确");
}
}

}
}

class twodates extends Frame implements ActionListener
{
private Button button5,button6;
private TextField yeartextfield1,monthtextfield1,daytextfield1,answertextfield,yeartextfield2,monthtextfield2,daytextfield2;
private Label yearlabel1,monthlabel1,daylabel1,yearlabel2,monthlabel2,daylabel2,answerlabel;
private double year1,month1,day1,year2,month2,day2;
private boolean isren1,isren2,iscontinue;
private int days;

public twodates()
{
super("两个日期计算");
this.setLayout(null);
this.setBounds(0,250,100,800);
this.setSize(800,150);
this.setResizable(false);

button5=new Button("确定");
button6=new Button("清除");

yearlabel1=new Label("请输入第一个年份");
monthlabel1=new Label("请输入第一个月份");
daylabel1=new Label("请输入第一个日期");

yearlabel2=new Label("请输入第二个年份");
monthlabel2=new Label("请输入第二个月份");
daylabel2=new Label("请输入第二个日期");

answerlabel=new Label("答案是");

yeartextfield1=new TextField(5);
monthtextfield1=new TextField(5);
daytextfield1=new TextField(5);

yeartextfield2=new TextField(5);
monthtextfield2=new TextField(5);
daytextfield2=new TextField(5);

answertextfield=new TextField(20);

button5.addActionListener(this);
button6.addActionListener(this);
yeartextfield1.addActionListener(this);
monthtextfield1.addActionListener(this);
daytextfield1.addActionListener(this);
yeartextfield2.addActionListener(this);
monthtextfield2.addActionListener(this);
daytextfield2.addActionListener(this);

yearlabel1.setBounds(0,50,100,20);
yeartextfield1.setBounds(100,50,100,20);
monthlabel1.setBounds(0,80,100,20);
monthtextfield1.setBounds(100,80,100,20);
daylabel1.setBounds(0,110,100,20);
daytextfield1.setBounds(100,110,100,20);
yearlabel2.setBounds(200,50,100,20);
yeartextfield2.setBounds(300,50,100,20);
monthlabel2.setBounds(200,80,100,20);
monthtextfield2.setBounds(300,80,100,20);
daylabel2.setBounds(200,110,100,20);
daytextfield2.setBounds(300,110,100,20);
button5.setBounds(400,80,100,20);
button6.setBounds(500,80,100,20);
answerlabel.setBounds(600,80,100,20);
answertextfield.setBounds(700,80,100,20);

this.add(yearlabel1);
this.add(yeartextfield1);
this.add(monthlabel1);
this.add(monthtextfield1);

this.add(daylabel1);
this.add(daytextfield1);

this.add(yearlabel2);
this.add(yeartextfield2);
this.add(monthlabel2);
this.add(monthtextfield2);
this.add(daylabel2);
this.add(daytextfield2);

this.add(button5);
this.add(button6);
this.add(answerlabel);
this.add(answertextfield);

this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
);

this.show();
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button6)
{
this.setSize(800,150);
yeartextfield1.setText("");
monthtextfield1.setText("");
daytextfield1.setText("");
yeartextfield2.setText("");
monthtextfield2.setText("");
daytextfield2.setText("");

answertextfield.setText("");
}
else if((e.getSource()==button5)||(e.getSource()==yeartextfield1)||(e.getSource()==monthtextfield1)||(e.getSource()==daytextfield1)||(e.getSource()==yeartextfield2)||(e.getSource()==monthtextfield2)||(e.getSource()==daytextfield2))
{
this.setSize(800,150);
try
{
year1=Double.parseDouble(yeartextfield1.getText());
month1=Double.parseDouble(monthtextfield1.getText());
day1=Double.parseDouble(daytextfield1.getText());
year2=Double.parseDouble(yeartextfield2.getText());
month2=Double.parseDouble(monthtextfield2.getText());
day2=Double.parseDouble(daytextfield2.getText());
}
catch (Exception exc)
{
iscontinue=false;
answertextfield.setText("输入不正确");
}

function func=new function();
isren1=func.ren(year1);
isren2=func.ren(year2);
iscontinue=false;
iscontinue=(func.conti(month1,day1,isren1))&&(func.conti(month2,day2,isren2));

if(iscontinue)
{
days=func.between(year1,month1,day1,year2,month2,day2);
answertextfield.setText("差"+days+"天");
}
else
{ answertextfield.setText("输入不正确");
}
}

}
}

class function
{
private String str;
private int year1,month1,day1,time1;
private int days=0;
private double temp;

public boolean ren(double year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
return true;
else return false;
}

public boolean conti(double month,double day,boolean isren)
{
boolean boo=true;
if((month<=0)||(month>12))
{ boo=false;}
else
{
switch ((int)month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: if ((day>31)||(day<=0)) boo=false;
else boo=true;
break;

case 4:
case 6:
case 9:
case 11: if ((day>30)||(day<=0)) boo=false;
else boo=true;
break;

case 2: if (isren)
{
if ((day>29)||(day<=0)) boo=false;
else boo=true;

}
else
{
if ((day>28)||(day<=0)) boo=false;
else boo=true;
}
break;
}
}
return boo;

}

public String plus(double year,double month,double day,double time)
{
year1=(int)year;
month1=(int)month;
day1=(int)day;
time1=(int)time;
for (int i=1;i<=time1;i++)
{
if ((month1==12)&&(day1==31))
{
year1++;
month1=1;
day1=1;
}

else if (((month1==1)||(month1==3)||(month1==5)||(month1==7)||(month1==8)||(month1==10))&&(day1==31))
{
month1++;
day1=1;
}
else if(((month1==4)||(month1==6)||(month1==9)||(month1==11))&&(day1==30))
{
leeps_my
2006-09-18 · TA获得超过807个赞
知道小有建树答主
回答量:212
采纳率:0%
帮助的人:0
展开全部
 
 
 
Java 库里有日历类(GregorianCalendar),要好好利用(想知道具体算法的话可以看看随 JDK 一起来的源代码):

import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Cal extends JFrame {
    private JTextField year = new JTextField( ),
                       day = new JTextField( ),
                       result = new JTextField( 20 );
    public Cal( ) {
        Container c = getContentPane( );
        c.setLayout( new GridLayout( 4, 2 ) );

        c.add( new JLabel( "年:" ) ); c.add( year );
        c.add( new JLabel( "天:" ) ); c.add( day );

        c.add( new JButton( new AbstractAction( "求结果" ) {
            public void actionPerformed( ActionEvent ae ) {
                int y, d;
                try {
                    y = Integer.parseInt( year.getText( ).trim( ) );
                    d = Integer.parseInt( day.getText( ).trim( ) );
                } catch ( NumberFormatException e ) {
                    JOptionPane.showMessageDialog( Cal.this, "年或天无值或有格式错误" );
                    return;
                }
                GregorianCalendar cal = new GregorianCalendar( y, Calendar.JANUARY, d );
                DateFormat df = new SimpleDateFormat( "M月d日,E,", Locale.CHINESE );
                String leap = ( cal.isLeapYear( y ) ? "" : "不" ) + "是闰年";
                result.setText( df.format( cal.getTime( ) ) + leap );
            }
        } ) );
        c.add( new JLabel( ) );  // 填空

        c.add( new JLabel( "结果:" ) ); c.add( result );

        pack( );
        setLocation( 100, 100 );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        setVisible( true );
    }

    public static void main( String[ ] args ) { new Cal( ); }
}
 
 
 
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
蛮蛮工作室
2006-09-16 · TA获得超过367个赞
知道小有建树答主
回答量:800
采纳率:0%
帮助的人:438万
展开全部
如果判断润年,我想大家都知道,只要
year % 4 == 0 && year % 400 != 0
就可以,然后几月几号都可以很容易控制,每个月的天数是一定的,除了润年的二月。
至于你说的星期几,就不容易了,至少我觉得不太容易,如果你自己写函数判断的话。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式