不习惯写博客 要审核后才能发表。。。不喜欢这点 希望接下去能坚持写吧。。
编程实现如下的功能,接收用户输入的一个0~8000之间的年份,输出这一年是否为闰年。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class JudgeYearApplet extends Applet implements ActionListener {
<span style="white-space:pre"> </span>Label prompt;
<span style="white-space:pre"> </span>TextField input,output;
<span style="white-space:pre"> </span>Button btn;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void init(){
<span style="white-space:pre"> </span>prompt=new Label("请输入一个0~8000之间的年份:");
<span style="white-space:pre"> </span>input=new TextField(6);
<span style="white-space:pre"> </span>output=new TextField(6);
<span style="white-space:pre"> </span>btn=new Button("判断");
<span style="white-space:pre"> </span>add(prompt);
<span style="white-space:pre"> </span>add(input);
<span style="white-space:pre"> </span>add(btn);
<span style="white-space:pre"> </span>add(output);
<span style="white-space:pre"> </span>btn.addActionListener(this);
<span style="white-space:pre"> </span>output.setEditable(false);//使调用这个函数的控件不能被编辑,如果设为true或者不调用这个函数,则控件可以编辑
<span style="white-space:pre"> </span>}//这里是把output设成不可编辑,即只能输出,不能输入
<span style="white-space:pre"> </span>public void actionPerformed(ActionEvent e){
<span style="white-space:pre"> </span>if(!(e.getSource().equals(btn)))
return;//若未单击按扭,则不做处理
<span style="white-space:pre"> </span>int in;
<span style="white-space:pre"> </span>in=Integer.parseInt(input.getText());//讲input的String转成int型
<span style="white-space:pre"> </span>if((in%4==0 && in%100!=0)||(in%400==0))//判断是否闰年
<span style="white-space:pre"> </span>output.setText("闰年");
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>output.setText("平年");
<span style="white-space:pre"> </span>}
}
</pre><pre name="code" class="java">