/******************************************************************************************
*该类实现了对SWING组件进行事件监听的不同方法 *
* 1 通过类自己实现相应的接口,在类中重写接口中的方法来对事件进行处理 *
* 2 通过匿名类来对事件进行处理(当有多个事件源存在时用) *
* 3 通过内部类对事件进行处理 *
* *
*如果有多个事件源存在可以通过两个方法来得到是那个事件源 *
*getActionCommand() getSource()建议使用第二个方法 *
* *
*如果对应的监听器接口有多个方法可以使用继承对应的适配器来实现对事件的监听 *
******************************************************************************************/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
//public class ChenJi extends JFrame implements ActionListener
public class ChenJi extends JFrame
{
private JPanel jPanel;
private JPanel jPanel1;
private JTextField nameText;
private JTextField sexText;
private JTextField addText;
private JTextField chinaText;
private JTextField mathText;
private JTextField englishText;
private JTextField zhengzhiText;
private JTextField test;
private JLabel title;
private JLabel name;
private JLabel sex;
private JLabel add;
private JLabel china;
private JLabel math;
private JLabel english;
private JLabel zhengzhi;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
public ChenJi()
{
setSize(400,500);
setTitle("成绩登记表");
setDefaultCloseOperation(EXIT_ON_CLOSE);
fram1();
}
public void fram1()
{
jPanel = (JPanel)getContentPane();
jPanel.setLayout(null);
title = new JLabel("成绩登记表");
title.setBounds(100,5,150,20);
jPanel.add(title);
name = new JLabel("姓名");
name.setBounds(10,40,50,20);
jPanel.add(name);
nameText = new JTextField("");
nameText.setBounds(80,40,50,20);
jPanel.add(nameText);
sex = new JLabel("性别");
sex.setBounds(170,40,50,20);
jPanel.add(sex);
sexText = new JTextField("");
sexText.setBounds(240,40,50,20);
jPanel.add(sexText);
add = new JLabel("籍贯");
add.setBounds(10,90,50,20);
jPanel.add(add);
addText = new JTextField("");
addText.setBounds(80,90,150,20);
jPanel.add(addText);
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBorder(new TitledBorder("成绩"));
jPanel1.setBounds(10,120,370,150);
china = new JLabel("语文");
china.setBounds(10,20,50,20);
jPanel1.add(china);
chinaText = new JTextField("");
chinaText.setBounds(90,20,50,20);
jPanel1.add(chinaText);
math = new JLabel("数学");
math.setBounds(170,20,50,20);
jPanel1.add(math);
mathText = new JTextField("");
mathText.setBounds(240,20,50,20);
jPanel1.add(mathText);
zhengzhi = new JLabel("政治");
zhengzhi.setBounds(10,60,50,20);
jPanel1.add(zhengzhi);
zhengzhiText = new JTextField("");
zhengzhiText.setBounds(90,60,50,20);
jPanel1.add(zhengzhiText);
jPanel.add(jPanel1);
jButton1 = new JButton("总成绩");
jButton1.addActionListener(new ActionListener()//通过匿名内部类来实现事件的处理
{
public void actionPerformed(ActionEvent e)//用getActionCommand()来获取事件源
{
String ss = e.getActionCommand();
try{
if(ss.equals("总成绩"))
zongchengji();
else
pingjun();
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
/* JButton jb = (JButton)e.getSource();//getSource()来获取事件源
try{
if(jb == jButton1)
zongchengji();
else
pingjun();
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
*/
}
});
jButton1.setBounds(100,380,50,20);
jPanel.add(jButton1);
jButton2 = new JButton("平均成绩");
jButton2.addActionListener(new EventTest());//通过内部类来实现事件的处理
jButton2.setBounds(100,280,50,20);
jPanel.add(jButton2);
jButton3 = new JButton("清空文本框");
// jButton3.addActionListener(this);//通过类自己来实现对应的监听接口来实现事件的处理
jButton3.setBounds(180,380,50,20);
jPanel.add(jButton3);
test = new JTextField();
test.setBounds(180,280,80,20);
jPanel.add(test);
}
public void actionPerformed(ActionEvent e)
{
String ss = e.getActionCommand();
try{
if(ss.equals("总成绩"))
zongchengji();
else if(ss.equals("清空文本框"))
test.setText("");
else
pingjun();
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
/* JButton jb = (JButton)e.getSource();
try{
if(jb == jButton1)
zongchengji();
else
pingjun();
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
*/
}
public void zongchengji()
{
Float china = Float.parseFloat(chinaText.getText());
Float math = Float.parseFloat(mathText.getText());
Float zhengzhi = Float.parseFloat(zhengzhiText.getText());
String zong = new Float(china+math+zhengzhi).toString();
test.setText(zong);
}
public void pingjun()
{
Float china = Float.parseFloat(chinaText.getText());
Float math = Float.parseFloat(mathText.getText());
Float zhengzhi = Float.parseFloat(zhengzhiText.getText());
String pingjun = new Float((china+math+zhengzhi)/3).toString();
test.setText(pingjun);
}
public static void main(String [] args)
{
ChenJi chenJi = new ChenJi();
chenJi.setVisible(true);
}
class EventTest implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss = e.getActionCommand();
try{
if(ss.equals("总成绩"))
zongchengji();
else
pingjun();
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
/* JButton jb = (JButton)e.getSource();
try{
if(jb == jButton1)
cj.zongchengji();
else
cj.pingjun();
}catch(Exception ex)
{
//ex.printStackTrace();
System.out.println("你输入的成绩错误");
}
*/
}
}
}