ActionListener只计算鼠标左键

时间:2014-08-17 07:33:09

标签: java actionlistener mouselistener

我从一些教程中学到了这个代码,但它只计算鼠标左键。我尝试使用MouseListener,但它在计时器变为0时继续计数。而使用ActionListener,它不计算鼠标右键单击。有什么建议?也许这是一个愚蠢的问题,但我是新来的。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class game extends JFrame
{
    private static final int SwingConstants=0;
    Timer timer;
    int timercounter;
    int clickcounter;
    JLabel directions,entertime,clicklabel,timeleft,label;
    JButton startbutton,clickbutton;
    JTextField tf;
    JMenuBar menubar;
    JMenu file,help;
    JMenuItem reset,exit,mhelp;
    JFrame frame=new JFrame();

    public game()
    {
        Container pane=this.getContentPane();
        pane.setLayout(new GridLayout(3,1,2,2));

        menubar=new JMenuBar();
        setJMenuBar(menubar);

        file=new JMenu("File");
        menubar.add(file);

        help=new JMenu("Help");
        menubar.add(help);

        reset=new JMenuItem("Reset");
        file.add(reset);

        exit=new JMenuItem("Quit");
        file.add(exit);

        mhelp=new JMenuItem("More Help!!");
        help.add(mhelp);

        ResetClass rc=new ResetClass();
        reset.addActionListener(rc);

        ExitClass ec=new ExitClass();
        exit.addActionListener(ec);

        MhelpClass mc=new MhelpClass();
        mhelp.addActionListener(mc);

        JPanel top=new JPanel();
        top.setLayout(new GridLayout(1,1));

        directions=new JLabel("Enter time & press <Click Here> REPEATEDLY!!");
        top.add(directions);
        pane.add(top);

        JPanel middle=new JPanel();
        middle.setLayout(new GridLayout(1,3));

        entertime=new JLabel("Enter Time (sec):");
        middle.add(entertime);

        tf=new JTextField();
        middle.add(tf);

        startbutton=new JButton("Click Here");
        middle.add(startbutton);
        pane.add(middle);

        JPanel bottom=new JPanel();
        bottom.setLayout(new GridLayout(1,3));

        clickbutton=new JButton("Click Here!");
        clickbutton.setEnabled(false);
        bottom.add(clickbutton);

        clicklabel=new JLabel("Clicks: 0");
        bottom.add(clicklabel);

        timeleft=new JLabel("Time left: ?");
        bottom.add(timeleft);
        pane.add(bottom);

        StartButtonClass sbc=new StartButtonClass();
        startbutton.addActionListener(sbc);

        ClickButtonClass cbc=new ClickButtonClass();
        clickbutton.addActionListener(cbc);
    }

    public class StartButtonClass implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent sbc)
        {
            try
            {
                int timeCount=(int)(Double.parseDouble(tf.getText()));
                if(timeCount<=0)
                {
                    tf.setText("Positive number!");
                    //startbutton.setEnabled(false);
                }
                else
                {
                    timeleft.setText("Time left: "+timeCount);
                    TimeClass tc=new TimeClass(timeCount);
                    timer=new Timer(1000,tc);
                    timer.start();
                    startbutton.setEnabled(false);
                    clickbutton.setEnabled(true);
                }
            }
            catch(NumberFormatException ex)
            {
                tf.setText("Number only!");
            }
        }
    }

    public class ClickButtonClass implements MouseListener
    {
        public void mouseReleased(MouseEvent cbc)
        {
            clickcounter++;
            clicklabel.setText("Clicks: "+clickcounter);
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            // TODO Auto-generated method stub      
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            // TODO Auto-generated method stub      
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            // TODO Auto-generated method stub      
        }
    }

    public class TimeClass implements ActionListener
    {
        int timerCounter;

        public TimeClass(int timerCounter)
        {
            this.timerCounter=timerCounter;
        }

        public void actionPerformed(ActionEvent tc)
        {
            timerCounter--;

            if(timerCounter>=1)
            {
                timeleft.setText("Time left: "+timerCounter);
            }
            else
            {
                timer.stop();
                timeleft.setText("Done!");
                clickbutton.setEnabled(false);
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }

    public class ResetClass implements ActionListener
    {
        public void actionPerformed(ActionEvent rc)
        {
            clickbutton.setEnabled(false);
            startbutton.setEnabled(true);
            clickcounter=0;
            clicklabel.setText("Clicks: 0");
            tf.setText("");
            timeleft.setText("Time left: ?");
        }
    }

    public class ExitClass implements ActionListener
    {
        public void actionPerformed(ActionEvent ec)
        {
            System.exit(0);
        }
    }

    public class MhelpClass implements ActionListener
    {
        public void actionPerformed(ActionEvent mc)
        {
            JOptionPane.showMessageDialog(null, "Read the Readme file carefully!!", "Help!!", JOptionPane.PLAIN_MESSAGE);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用它可以让你看到鼠标右键点击

class MyMouseListener implements MouseListener{
@Override
public void mouseReleased(MouseEvent arg0) {
    if(SwingUtilities.isRightMouseButton(arg0)&&clickButton.isEnabled()){
        //my code
    }
}