feray218 2017-04-28 03:27 采纳率: 66.7%
浏览 2488

java 如何将按钮事件改成鼠标事件监听器

请求各位大神,关于下面这个简单计算器,如何对按钮实现鼠标监听,想不通鼠标监听和按钮监听有什么区别

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();

    }
}
  • 写回答

1条回答 默认 最新

  • feray218 2017-04-28 04:38
    关注

    自己解决啦:

     //filename:Count.java
    //计算器@鼠标事件
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    class Count extends JFrame //implements MouseListener
    {
        static JLabel lab1,lab2,lab3;
        static JTextField text1,text2,text3;
        static JRadioButton radio1,radio2,radio3,radio4;
    
        static JButton button1,button2;
        static JPanel pane1,pane2,pane3;
    
        public static void main(String args[])
        {
            Count c=new Count();
            c.setTitle("简单的算术计算");//标题
            c.setSize(560,150);//界面大小   
            c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面
            c.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);
    
             c.add(pane1,BorderLayout.NORTH);
             c.add(pane2,BorderLayout.CENTER);
             c.add(pane3,BorderLayout.SOUTH);//布局
    
            button1.addMouseListener(new MyMouseList());//设置坚听者
            button2.addMouseListener(new ClearMouseList());//设置监听者
             c.setVisible(true);//显示
        }
          static class MyMouseList extends MouseAdapter
        {
            public void mouseClicked(MouseEvent e)//单击鼠标按钮时发生
            {
                double x,y,z=0;
                String s;
                 x=Double.parseDouble(text1.getText());
                 y=Double.parseDouble(text2.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);//将结果添加到文本框中
                     }     
                 }
            }
        }
    
        static class ClearMouseList extends MouseAdapter//清除
        {
            public void mouseClicked(MouseEvent e)
            { 
                    text1.setText(null);
                    text2.setText(null);
                    text3.setText(null);        
            }    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献