事件处理
import java.awt.*;import java.text.*;
import javax.swing.*;
import java.awt.event.*;
public class NianFen
{
public static void main(String args[])
{
NianFrame frame=new NianFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class NianFrame extends JFrame implements ActionListener
{
JTextField T1,T2;
JButton A;
JLabel a,b;
JPanel p;
public NianFrame()
{
setTitle("Nian Fen");
Container con=getContentPane();
con.setLayout(new BorderLayout());
a=new JLabel("input year:");
p=new JPanel();
p.setLayout(new GridLayout(5,1));
p.add(a);
T1=new JTextField(20);
p.add(T1);
b=new JLabel("result:");
p.add(b);
T2=new JTextField(20);
p.add(T2);
A=new JButton("Yes");
p.add(A);
con.add(p,BorderLayout.CENTER);
A.addActionListener(this);
setVisible(true);
setSize(200,200);
pack();
}
public void actionporformed(ActionEvent e)
{
double t;
try
{
t=Double.parseDouble(T1.getText());
}
catch ( NumberFormatException numberFormat )
{
JOptionPane.showMessageDialog( this,"Invalid Operand", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
return;
}
if(e.getSource()==A)
{
if((t%400==0||(t%4==0&&t%100!=0)))
T2.setText("Yes");
else
T2.setText("No");
}
}
}