请求各位大神,关于下面这个简单计算器,如何对按钮实现鼠标监听,想不通鼠标监听和按钮监听有什么区别
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Guide extends JFrame implements ActionListener
{
private JLabel lab1,lab2,lab3;
private JTextField text1,text2,text3;
private JRadioButton radio1,radio2,radio3,radio4;
private JButton button1,button2;
private JPanel pane1,pane2,pane3;
public Guide()
{
setTitle("简单的算术计算");//标题
setSize(560,150);//界面大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面
setResizable(true);
lab1=new JLabel("操作数1:");
lab2=new JLabel("操作数2:");
lab3=new JLabel("计算结果:");
text1=new JTextField(20);
text2=new JTextField(20);
text3=new JTextField(20);
radio1=new JRadioButton("+");
radio1.setSelected(true);//默认按钮1选中
radio2=new JRadioButton("-");
radio3=new JRadioButton("*");
radio4=new JRadioButton("/");
button1=new JButton("计算");
button1.setForeground(Color.red);//字体颜色
button1.setBackground(Color.yellow);
button2=new JButton("清除");
button2.setForeground(Color.red);//字体颜色
button2.setBackground(Color.yellow);
pane1=new JPanel();
pane2=new JPanel();
pane3=new JPanel();
pane1.add(lab1);pane1.add(text1);
pane1.add(lab2);pane1.add(text2);
ButtonGroup group=new ButtonGroup();
group.add(radio1); group.add(radio2);
group.add(radio3); group.add(radio4);
pane2.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
pane2.add(radio1);pane2.add(radio2);
pane2.add(radio3);pane2.add(radio4);
pane3.add(lab3);pane3.add(text3);
pane3.add(button1);pane3.add(button2);
add(pane1,BorderLayout.NORTH);
add(pane2,BorderLayout.CENTER);
add(pane3,BorderLayout.SOUTH);//布局
button1.addActionListener(this);
button2.addActionListener(this);//将按钮添加到事件监听器
setVisible(true);//显示
}
public void actionPerformed(ActionEvent e)
{
double x,y,z=0;
String s;
x=Double.parseDouble(text1.getText());
y=Double.parseDouble(text2.getText());
JButton but=(JButton)e.getSource(); //获取事件源
if(but.getText()=="计算")
{
if(radio1.isSelected()==true)
{
z=x+y;
s=Double.toString(z);
text3.setText(s);//将结果添加到文本框中
}
if(radio2.isSelected()==true)
{
z=x-y;
s=Double.toString(z);
text3.setText(s);//将结果添加到文本框中
}
if(radio3.isSelected()==true)
{
z=x*y;
s=Double.toString(z);
text3.setText(s);//将结果添加到文本框中
}
if(radio4.isSelected()==true)
{//需要异常判断处理
if(y==0){
text3.setText("除数不能为0!");
}
else {
z=x/y;
s=Double.toString(z);
text3.setText(s);//将结果添加到文本框中
}
}
}
if(but.getText()=="清除")//清除
{
text1.setText(null);
text2.setText(null);
text3.setText(null);
}
}
public static void main(String args[])
{
new Guide();
}
}