在Java开发中,对于事件的处理非常重要,比如响应键盘的输入、鼠标的点击、窗口的移动等等都要涉及到Java事件的应用。
Java事件由事件类和
监听接口组成,
自定义一个事件前,必须提供一个事件的监听接口以及一个事件类。JAVA中监听接口是继承EventListener的类,事件类继承EventObject的类。很多基本的事件系统已经为我们定义好了,我们只要学会调用即可,但是为了更加灵活地满足特定的需求,我们有时候也需要自定义事件。
对常用的事件监听器是:MouseListener,ActionListener,MouseMotionListener
1、MouseListener接口中,存在mouseClicked、mousePressed、mouseReleased、 mouseEntered、mouseExited这
五个方法,分别对应鼠标的点击,按下,释放,进入组件和离开组件。
2、ActionListener接口中,只有actionPerformed方法,用来获取被监听组件的状态,例如用e.getActionCommand()获取按钮上的文字。
3、MouseMotionListener接口中则存在mouseDragged和mouseMove两个方法,分别用于监听鼠标的拖动与移动。
class="java">
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
public void initCalculator(){
JFrame jf = new JFrame();
jf.set
DefaultClose
Operation(3);
jf.setSize(315, 350);
BorderLayout b = new BorderLayout();
jf.setLayout(b);
//设置北面面板相关内容
JPanel northpanel = new JPanel();
northpanel.setPreferredSize(new Dimension(0, 30));
jf.add(northpanel, BorderLayout.NORTH);
northpanel.setLayout(null);
//设置南面面板相关内容
JPanel southpanel = new JPanel();
southpanel.setPreferredSize(new Dimension(0, 240));
jf.add(southpanel, BorderLayout.SOUTH);
southpanel.setLayout(null);
javax.swing.JButton btn4 = new JButton("MC");
btn4.setBounds(0, 0, 55, 35);
southpanel.add(btn4);
javax.swing.JButton btn5 = new JButton("MR");
btn5.setBounds(60, 0, 55, 35);
southpanel.add(btn5);
javax.swing.JButton btn6 = new JButton("MS");
btn6.setBounds(120, 0, 55, 35);
southpanel.add(btn6);
javax.swing.JButton btn7 = new JButton("M+");
btn7.setBounds(180, 0, 55, 35);
southpanel.add(btn7);
javax.swing.JButton btn8 = new JButton("M-");
btn8.setBounds(240, 0, 55, 35);
southpanel.add(btn8);
javax.swing.JButton btn9 = new JButton("@");
btn9.setBounds(0, 40, 55, 35);
southpanel.add(btn9);
javax.swing.JButton btn10 = new JButton("CE");
btn10.setBounds(60, 40, 55, 35);
southpanel.add(btn10);
javax.swing.JButton btn11 = new JButton("C");
btn11.setBounds(120, 40, 55, 35);
southpanel.add(btn11);
javax.swing.JButton btn12 = new JButton("+_");
btn12.setBounds(180, 40, 55, 35);
southpanel.add(btn12);
javax.swing.JButton btn13 = new JButton("#");
btn13.setBounds(240, 40, 55, 35);
southpanel.add(btn13);
javax.swing.JButton btn14 = new JButton("7");
btn14.setBounds(0, 80, 55, 35);
southpanel.add(btn14);
javax.swing.JButton btn15 = new JButton("8");
btn15.setBounds(60, 80, 55, 35);
southpanel.add(btn15);
javax.swing.JButton btn16 = new JButton("9");
btn16.setBounds(120, 80, 55, 35);
southpanel.add(btn16);
javax.swing.JButton btn17 = new JButton("/");
btn17.setBounds(180, 80, 55, 35);
southpanel.add(btn17);
javax.swing.JButton btn18 = new JButton("%");
btn18.setBounds(240, 80, 55, 35);
southpanel.add(btn18);
javax.swing.JButton btn19 = new JButton("4");
btn19.setBounds(0, 120, 55, 35);
southpanel.add(btn19);
javax.swing.JButton btn20 = new JButton("5");
btn20.setBounds(60, 120, 55, 35);
southpanel.add(btn20);
javax.swing.JButton btn21 = new JButton("6");
btn21.setBounds(120, 120, 55, 35);
southpanel.add(btn21);
javax.swing.JButton btn22 = new JButton("*");
btn22.setBounds(180, 120, 55, 35);
southpanel.add(btn22);
javax.swing.JButton btn23 = new JButton("1/x");
btn23.setBounds(240, 120, 55, 35);
southpanel.add(btn23);
javax.swing.JButton btn24 = new JButton("1");
btn24.setBounds(0, 160, 55, 35);
southpanel.add(btn24);
javax.swing.JButton btn25 = new JButton("2");
btn25.setBounds(60, 160, 55, 35);
southpanel.add(btn25);
javax.swing.JButton btn26 = new JButton("3");
btn26.setBounds(120, 160, 55, 35);
southpanel.add(btn26);
javax.swing.JButton btn27 = new JButton("-");
btn27.setBounds(180, 160, 55, 35);
southpanel.add(btn27);
javax.swing.JButton btn28 = new JButton("=");
btn28.setBounds(240, 160, 55, 75);
southpanel.add(btn28);
javax.swing.JButton btn29 = new JButton("0");
btn29.setBounds(0, 200, 115, 35);
southpanel.add(btn29);
javax.swing.JButton btn30 = new JButton(".");
btn30.setBounds(120, 200, 55, 35);
southpanel.add(btn30);
javax.swing.JButton btn31 = new JButton("+");
btn31.setBounds(180, 200, 55, 35);
southpanel.add(btn31);
//设置中心面板相关内容
JPanel centerpanel = new JPanel();
centerpanel.setBackground(Color.lightGray);
jf.add(centerpanel, BorderLayout.CENTER);
centerpanel.setLayout(new BorderLayout());
JTextField input = new JTextField(20);
centerpanel.add(input);
//将0-9十个数字对应的按钮放入事件监听器中
MyActionListener2 l = new MyActionListener2(input);
btn14.addActionListener(l);
btn15.addActionListener(l);
btn16.addActionListener(l);
btn19.addActionListener(l);
btn20.addActionListener(l);
btn21.addActionListener(l);
btn24.addActionListener(l);
btn25.addActionListener(l);
btn26.addActionListener(l);
btn29.addActionListener(l);
btn28.addActionListener(l);
btn27.addActionListener(l);
btn30.addActionListener(l);
btn31.addActionListener(l);
btn22.addActionListener(l);
btn17.addActionListener(l);
jf.setVisible(true);
}
public static void main(String[] args) {
Calculator c = new Calculator();
c.initCalculator();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class MyActionListener2 implements ActionListener {
private JTextField input;
private String s1;
private String s2;
private String oper;
public MyActionListener2(JTextField input){
this.input = input;
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
System.out.println("cmd="+cmd);
String str = input.getText();
if(cmd.equals("+")||cmd.equals("-") || cmd.equals("*") || cmd.equals("/")){
//运算处理
s1 = input.getText();
oper = cmd;
input.setText("");
}else if (cmd.equals("=")){
//计算结果
s2 = input.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double d = 0;
if(oper.equals("+")){
d = d1 + d2;
}else if(oper.equals("-")){
d = d1 - d2;
}else if(oper.equals("*")){
d = d1 * d2;
}else if(oper.equals("/")){
d = d1 / d2;
}
input.setText(""+d);
}else{
input.setText(str+cmd);
}
}
}