/******************************************************************************************
*該類實現了對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("你輸入的成績錯誤");
}
*/
}
}
}